Elgg  Version 4.3
BootHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Application;
4 
6 
12 class BootHandler {
13 
17  protected $app;
18 
24  public function __construct(Application $app) {
25  $this->app = $app;
26  }
27 
34  public function __invoke(): void {
35  if ($this->app->getBootStatus('full_boot_completed')) {
36  return;
37  }
38 
39  $this->bootServices();
40  $this->bootPlugins();
41  $this->bootApplication();
42  }
43 
49  public function bootServices(): void {
50  if ($this->app->getBootStatus('service_boot_completed')) {
51  return;
52  }
53 
54  // in case not loaded already
55  $this->app->loadCore();
56 
57  if (!$this->app->internal_services->db) {
58  // no database boot!
60  $this->app->internal_services->session->start();
61  $this->app->internal_services->translator->bootTranslations();
62 
63  \Elgg\Application\SystemEventHandlers::init();
64 
65  $this->app->setBootStatus('full_boot_completed', true);
66 
67  return;
68  }
69 
70  $this->setEntityClasses();
71 
72  // need to be registered as part of services, because partial boots do at least include the services
73  // the system relies on the existence of some of the event/hooks
74  $this->registerHooks();
75  $this->registerEvents();
76 
77  // Connect to database, load language files, load configuration, init session
78  $this->app->internal_services->boot->boot($this->app->internal_services);
79 
80  // we don't store langs in boot data because it varies by user
81  $this->app->internal_services->translator->bootTranslations();
82 
83  $this->app->setBootStatus('service_boot_completed', true);
84  }
85 
91  public function bootPlugins(): void {
92  if ($this->app->getBootStatus('plugins_boot_completed') || !$this->app->internal_services->db) {
93  return;
94  }
95 
96  $events = $this->app->internal_services->events;
97 
98  $events->registerHandler('plugins_load:before', 'system', 'elgg_views_boot');
99  $events->registerHandler('plugins_load:after', 'system', function() {
100  $this->app->internal_services->session->boot();
101  });
102 
103  $events->registerHandler('plugins_boot', 'system', function() {
104  $this->registerRoutes();
105  });
106  $events->registerHandler('plugins_boot', 'system', function() {
107  $this->registerActions();
108  });
109 
110  // Setup all boot sequence handlers for active plugins
111  $this->app->internal_services->plugins->build();
112 
113  // Register plugin classes, entities etc
114  // Call PluginBootstrap::load()
115  // After event completes, Elgg session is booted
116  $events->triggerSequence('plugins_load', 'system');
117 
118  // Boot plugin, setup languages and views
119  // Call PluginBootstrap::boot()
120  $events->triggerSequence('plugins_boot', 'system');
121 
122  $this->app->setBootStatus('plugins_boot_completed', true);
123  }
124 
130  public function bootApplication(): void {
131  if ($this->app->getBootStatus('application_boot_completed') || !$this->app->internal_services->db) {
132  return;
133  }
134 
135  $events = $this->app->internal_services->events;
136 
137  $this->app->internal_services->views->clampViewtypeToPopulatedViews();
138  $this->app->allowPathRewrite();
139 
140  // Complete the boot process for both engine and plugins
141  $events->triggerSequence('init', 'system');
142 
143  $this->app->setBootStatus('full_boot_completed', true);
144 
145  // Tell the access functions the system has booted, plugins are loaded,
146  // and the user is logged in so it can start caching
147  $this->app->internal_services->accessCollections->markInitComplete();
148 
149  // System loaded and ready
150  $events->triggerSequence('ready', 'system');
151 
152  $this->app->setBootStatus('application_boot_completed', true);
153  }
154 
160  public function setEntityClasses(): void {
161  elgg_set_entity_class('user', 'user', \ElggUser::class);
162  elgg_set_entity_class('group', 'group', \ElggGroup::class);
163  elgg_set_entity_class('site', 'site', \ElggSite::class);
164  elgg_set_entity_class('object', 'plugin', \ElggPlugin::class);
165  elgg_set_entity_class('object', 'file', \ElggFile::class);
166  elgg_set_entity_class('object', 'widget', \ElggWidget::class);
167  elgg_set_entity_class('object', 'comment', \ElggComment::class);
168  elgg_set_entity_class('object', 'elgg_upgrade', \ElggUpgrade::class);
169  elgg_set_entity_class('object', 'admin_notice', \ElggAdminNotice::class);
170  }
171 
177  protected function registerHooks(): void {
178  $conf = \Elgg\Project\Paths::elgg() . 'engine/hooks.php';
179  $spec = \Elgg\Includer::includeFile($conf);
180 
181  $hooks = $this->app->internal_services->hooks;
182 
183  foreach ($spec as $name => $types) {
184  foreach ($types as $type => $callbacks) {
185  foreach ($callbacks as $callback => $hook_spec) {
186  if (!is_array($hook_spec)) {
187  continue;
188  }
189 
190  $unregister = (bool) elgg_extract('unregister', $hook_spec, false);
191 
192  if ($unregister) {
193  $hooks->unregisterHandler($name, $type, $callback);
194  } else {
195  $priority = (int) elgg_extract('priority', $hook_spec, 500);
196 
197  $hooks->registerHandler($name, $type, $callback, $priority);
198  }
199  }
200  }
201  }
202  }
203 
209  protected function registerEvents(): void {
210  $conf = \Elgg\Project\Paths::elgg() . 'engine/events.php';
211  $spec = \Elgg\Includer::includeFile($conf);
212 
213  $events = $this->app->internal_services->events;
214 
215  foreach ($spec as $name => $types) {
216  foreach ($types as $type => $callbacks) {
217  foreach ($callbacks as $callback => $hook_spec) {
218  if (!is_array($hook_spec)) {
219  continue;
220  }
221 
222  $unregister = (bool) elgg_extract('unregister', $hook_spec, false);
223 
224  if ($unregister) {
225  $events->unregisterHandler($name, $type, $callback);
226  } else {
227  $priority = (int) elgg_extract('priority', $hook_spec, 500);
228 
229  $events->registerHandler($name, $type, $callback, $priority);
230  }
231  }
232  }
233  }
234  }
235 
241  protected function registerRoutes(): void {
242  $conf = \Elgg\Project\Paths::elgg() . 'engine/routes.php';
243  $routes = \Elgg\Includer::includeFile($conf);
244 
245  foreach ($routes as $name => $def) {
246  $this->app->internal_services->routes->register($name, $def);
247  }
248  }
249 
255  protected function registerActions(): void {
256  $conf = \Elgg\Project\Paths::elgg() . 'engine/actions.php';
258 
259  $root_path = \Elgg\Project\Paths::elgg();
260 
261  foreach ($actions as $action => $action_spec) {
262  if (!is_array($action_spec)) {
263  continue;
264  }
265 
266  $access = elgg_extract('access', $action_spec, 'logged_in');
267  $handler = elgg_extract('controller', $action_spec);
268  if (!$handler) {
269  $handler = elgg_extract('filename', $action_spec) ?: "{$root_path}/actions/{$action}.php";
270  }
271 
272  $this->app->internal_services->actions->register($action, $handler, $access);
273  }
274  }
275 }
registerHooks()
Register core hooks.
static includeFile($file)
Include a file with as little context as possible.
Definition: Includer.php:18
elgg_views_boot()
Initialize viewtypes on system boot event This ensures simplecache is cleared during upgrades...
Definition: views.php:1355
bootPlugins()
Boot plugins.
Definition: BootHandler.php:91
static elgg()
Get the Elgg codebase path with "/".
Definition: Paths.php:44
registerActions()
Register core actions.
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
__construct(Application $app)
Constructor.
Definition: BootHandler.php:24
if(elgg_view_exists("widgets/{$widget->handler}/edit")) $access
Definition: save.php:19
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
bootServices()
Boot core services.
Definition: BootHandler.php:49
$type
Definition: delete.php:21
registerRoutes()
Register core routes.
__invoke()
Full application boot Boots services, plugins and trigger init/ready events.
Definition: BootHandler.php:34
setEntityClasses()
Set core entity classes.
bootApplication()
Finish bootstrapping the application.
registerEvents()
Register core events.
if(!$menu instanceof\Elgg\Menu\PreparedMenu) $actions
Definition: user_hover.php:16
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:547
$action
Definition: subscribe.php:11
elgg_set_entity_class($type, $subtype, $class="")
Sets class constructor name for entities with given type and subtype.
Definition: entities.php:43
Load, boot, and implement a front controller for an Elgg application.
Definition: Application.php:48
$handler
Definition: add.php:7
Handles application boot sequence.
Definition: BootHandler.php:12
$priority