Elgg  Version master
BootHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Application;
4 
6 
12 class BootHandler {
13 
19  public function __construct(protected Application $app) {
20  }
21 
28  public function __invoke(): void {
29  if ($this->app->getBootStatus('full_boot_completed')) {
30  return;
31  }
32 
33  $this->bootServices();
34  $this->bootPlugins();
35  $this->bootApplication();
36  }
37 
43  public function bootServices(): void {
44  if ($this->app->getBootStatus('service_boot_completed')) {
45  return;
46  }
47 
48  // in case not loaded already
49  $this->app->loadCore();
50 
51  if (!$this->app->internal_services->db) {
52  // no database boot!
54  $this->app->internal_services->session->start();
55  $this->app->internal_services->translator->bootTranslations();
56 
57  \Elgg\Application\SystemEventHandlers::init();
58 
59  $this->app->setBootStatus('full_boot_completed', true);
60 
61  return;
62  }
63 
64  $this->registerEntities();
65 
66  // need to be registered as part of services, because partial boots do at least include the services
67  // the system relies on the existence of some of the event
68  $this->registerEvents();
69 
70  // Connect to database, load language files, load configuration, init session
71  $this->app->internal_services->boot->boot($this->app->internal_services);
72 
73  // we don't store langs in boot data because it varies by user
74  $this->app->internal_services->translator->bootTranslations();
75 
76  $this->app->setBootStatus('service_boot_completed', true);
77  }
78 
84  public function bootPlugins(): void {
85  if ($this->app->getBootStatus('plugins_boot_completed') || !$this->app->internal_services->db) {
86  return;
87  }
88 
89  $events = $this->app->internal_services->events;
90 
91  $events->registerHandler('plugins_load:before', 'system', 'elgg_views_boot');
92  $events->registerHandler('plugins_load:after', 'system', function() {
93  $this->app->internal_services->session->boot();
94  });
95 
96  $events->registerHandler('plugins_boot', 'system', function() {
97  $this->registerRoutes();
98  });
99  $events->registerHandler('plugins_boot', 'system', function() {
100  $this->registerActions();
101  });
102 
103  // Setup all boot sequence handlers for active plugins
104  $this->app->internal_services->plugins->build();
105 
106  // Register plugin classes, entities etc
107  // Call PluginBootstrap::load()
108  // After event completes, Elgg session is booted
109  $events->triggerSequence('plugins_load', 'system');
110 
111  // Boot plugin, setup languages and views
112  // Call PluginBootstrap::boot()
113  $events->triggerSequence('plugins_boot', 'system');
114 
115  $this->app->setBootStatus('plugins_boot_completed', true);
116  }
117 
123  public function bootApplication(): void {
124  if ($this->app->getBootStatus('application_boot_completed') || !$this->app->internal_services->db) {
125  return;
126  }
127 
128  $events = $this->app->internal_services->events;
129 
130  $this->app->allowPathRewrite();
131 
132  // Complete the boot process for both engine and plugins
133  $events->triggerSequence('init', 'system');
134 
135  $this->app->setBootStatus('full_boot_completed', true);
136 
137  // Tell the access functions the system has booted, plugins are loaded,
138  // and the user is logged in so it can start caching
139  $this->app->internal_services->accessCollections->markInitComplete();
140 
141  // System loaded and ready
142  $events->triggerSequence('ready', 'system');
143 
144  $this->app->setBootStatus('application_boot_completed', true);
145  }
146 
152  public function registerEntities(): void {
153  $conf = \Elgg\Project\Paths::elgg() . 'engine/entities.php';
154  $spec = \Elgg\Includer::includeFile($conf);
155 
156  foreach ($spec as $entity) {
157  if (!isset($entity['type'], $entity['subtype'], $entity['class'])) {
158  continue;
159  }
160 
161  $this->app->internal_services->entityTable->setEntityClass($entity['type'], $entity['subtype'], $entity['class']);
162 
163  $capabilities = elgg_extract('capabilities', $entity, []);
164  foreach ($capabilities as $capability => $value) {
165  $this->app->internal_services->entity_capabilities->setCapability($entity['type'], $entity['subtype'], $capability, $value);
166  }
167  }
168  }
169 
175  protected function registerEvents(): void {
176  $conf = \Elgg\Project\Paths::elgg() . 'engine/events.php';
177  $spec = \Elgg\Includer::includeFile($conf);
178 
179  $events = $this->app->internal_services->events;
180 
181  foreach ($spec as $name => $types) {
182  foreach ($types as $type => $callbacks) {
183  foreach ($callbacks as $callback => $event_spec) {
184  if (!is_array($event_spec)) {
185  continue;
186  }
187 
188  $unregister = (bool) elgg_extract('unregister', $event_spec, false);
189 
190  if ($unregister) {
191  $events->unregisterHandler($name, $type, $callback);
192  } else {
193  $priority = (int) elgg_extract('priority', $event_spec, 500);
194 
195  $events->registerHandler($name, $type, $callback, $priority);
196  }
197  }
198  }
199  }
200  }
201 
207  protected function registerRoutes(): void {
208  $conf = \Elgg\Project\Paths::elgg() . 'engine/routes.php';
209  $routes = \Elgg\Includer::includeFile($conf);
210 
211  foreach ($routes as $name => $def) {
212  $this->app->internal_services->routes->register($name, $def);
213  }
214  }
215 
221  protected function registerActions(): void {
222  $conf = \Elgg\Project\Paths::elgg() . 'engine/actions.php';
224 
225  $root_path = \Elgg\Project\Paths::elgg();
226 
227  foreach ($actions as $action => $action_spec) {
228  if (!is_array($action_spec)) {
229  continue;
230  }
231 
232  $access = elgg_extract('access', $action_spec, 'logged_in');
233  $handler = elgg_extract('controller', $action_spec);
234  if (!$handler) {
235  $handler = elgg_extract('filename', $action_spec) ?: "{$root_path}/actions/{$action}.php";
236  }
237 
238  // unset handled action specs, pass the rest to the action service
239  unset($action_spec['access']);
240  unset($action_spec['controller']);
241  unset($action_spec['filename']);
242 
243  $this->app->internal_services->actions->register($action, $handler, $access, $action_spec);
244  }
245  }
246 }
$entity
Definition: reset.php:8
if(! $user||! $user->canDelete()) $name
Definition: delete.php:22
$type
Definition: delete.php:21
$handler
Definition: add.php:7
Handles application boot sequence.
Definition: BootHandler.php:12
registerActions()
Register core actions.
bootApplication()
Finish bootstrapping the application.
__construct(protected Application $app)
Constructor.
Definition: BootHandler.php:19
registerEvents()
Register core events.
__invoke()
Full application boot Boots services, plugins and trigger init/ready events.
Definition: BootHandler.php:28
bootServices()
Boot core services.
Definition: BootHandler.php:43
registerEntities()
Registers core entities.
registerRoutes()
Register core routes.
Load, boot, and implement a front controller for an Elgg application.
Definition: Application.php:48
getBootStatus(string $type)
Retrieve the boot status of the application.
static includeFile($file)
Include a file with as little context as possible.
Definition: Includer.php:18
Find Elgg and project paths.
Definition: Paths.php:8
static elgg()
Get the Elgg codebase path with "/".
Definition: Paths.php:44
elgg()
Bootstrapping and helper procedural code available for use in Elgg core and plugins.
Definition: elgglib.php:12
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:246
$value
Definition: generic.php:51
elgg_views_boot()
Initialize viewtypes on system boot event This ensures simplecache is cleared during upgrades.
Definition: views.php:1300
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
Definition: robots.php:10
$priority
$action
Definition: subscribe.php:11
if(! $menu instanceof \Elgg\Menu\PreparedMenu) $actions
Definition: user_hover.php:21
if(elgg_view_exists("widgets/{$widget->handler}/edit")) $access
Definition: save.php:19