Elgg  Version 5.0
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
74  $this->registerEvents();
75 
76  // Connect to database, load language files, load configuration, init session
77  $this->app->internal_services->boot->boot($this->app->internal_services);
78 
79  // we don't store langs in boot data because it varies by user
80  $this->app->internal_services->translator->bootTranslations();
81 
82  $this->app->setBootStatus('service_boot_completed', true);
83  }
84 
90  public function bootPlugins(): void {
91  if ($this->app->getBootStatus('plugins_boot_completed') || !$this->app->internal_services->db) {
92  return;
93  }
94 
95  $events = $this->app->internal_services->events;
96 
97  $events->registerHandler('plugins_load:before', 'system', 'elgg_views_boot');
98  $events->registerHandler('plugins_load:after', 'system', function() {
99  $this->app->internal_services->session->boot();
100  });
101 
102  $events->registerHandler('plugins_boot', 'system', function() {
103  $this->registerRoutes();
104  });
105  $events->registerHandler('plugins_boot', 'system', function() {
106  $this->registerActions();
107  });
108 
109  // Setup all boot sequence handlers for active plugins
110  $this->app->internal_services->plugins->build();
111 
112  // Register plugin classes, entities etc
113  // Call PluginBootstrap::load()
114  // After event completes, Elgg session is booted
115  $events->triggerSequence('plugins_load', 'system');
116 
117  // Boot plugin, setup languages and views
118  // Call PluginBootstrap::boot()
119  $events->triggerSequence('plugins_boot', 'system');
120 
121  $this->app->setBootStatus('plugins_boot_completed', true);
122  }
123 
129  public function bootApplication(): void {
130  if ($this->app->getBootStatus('application_boot_completed') || !$this->app->internal_services->db) {
131  return;
132  }
133 
134  $events = $this->app->internal_services->events;
135 
136  $this->app->internal_services->views->clampViewtypeToPopulatedViews();
137  $this->app->allowPathRewrite();
138 
139  // Complete the boot process for both engine and plugins
140  $events->triggerSequence('init', 'system');
141 
142  $this->app->setBootStatus('full_boot_completed', true);
143 
144  // Tell the access functions the system has booted, plugins are loaded,
145  // and the user is logged in so it can start caching
146  $this->app->internal_services->accessCollections->markInitComplete();
147 
148  // System loaded and ready
149  $events->triggerSequence('ready', 'system');
150 
151  $this->app->setBootStatus('application_boot_completed', true);
152  }
153 
159  public function setEntityClasses(): void {
160  elgg_set_entity_class('user', 'user', \ElggUser::class);
161  elgg_set_entity_class('group', 'group', \ElggGroup::class);
162  elgg_set_entity_class('site', 'site', \ElggSite::class);
163  elgg_set_entity_class('object', 'plugin', \ElggPlugin::class);
164  elgg_set_entity_class('object', 'file', \ElggFile::class);
165  elgg_set_entity_class('object', 'widget', \ElggWidget::class);
166  elgg_set_entity_class('object', 'comment', \ElggComment::class);
167  elgg_set_entity_class('object', 'elgg_upgrade', \ElggUpgrade::class);
168  elgg_set_entity_class('object', 'admin_notice', \ElggAdminNotice::class);
169  }
170 
176  protected function registerEvents(): void {
177  $conf = \Elgg\Project\Paths::elgg() . 'engine/events.php';
178  $spec = \Elgg\Includer::includeFile($conf);
179 
180  $events = $this->app->internal_services->events;
181 
182  foreach ($spec as $name => $types) {
183  foreach ($types as $type => $callbacks) {
184  foreach ($callbacks as $callback => $event_spec) {
185  if (!is_array($event_spec)) {
186  continue;
187  }
188 
189  $unregister = (bool) elgg_extract('unregister', $event_spec, false);
190 
191  if ($unregister) {
192  $events->unregisterHandler($name, $type, $callback);
193  } else {
194  $priority = (int) elgg_extract('priority', $event_spec, 500);
195 
196  $events->registerHandler($name, $type, $callback, $priority);
197  }
198  }
199  }
200  }
201  }
202 
208  protected function registerRoutes(): void {
209  $conf = \Elgg\Project\Paths::elgg() . 'engine/routes.php';
210  $routes = \Elgg\Includer::includeFile($conf);
211 
212  foreach ($routes as $name => $def) {
213  $this->app->internal_services->routes->register($name, $def);
214  }
215  }
216 
222  protected function registerActions(): void {
223  $conf = \Elgg\Project\Paths::elgg() . 'engine/actions.php';
225 
226  $root_path = \Elgg\Project\Paths::elgg();
227 
228  foreach ($actions as $action => $action_spec) {
229  if (!is_array($action_spec)) {
230  continue;
231  }
232 
233  $access = elgg_extract('access', $action_spec, 'logged_in');
234  $handler = elgg_extract('controller', $action_spec);
235  if (!$handler) {
236  $handler = elgg_extract('filename', $action_spec) ?: "{$root_path}/actions/{$action}.php";
237  }
238 
239  $this->app->internal_services->actions->register($action, $handler, $access);
240  }
241  }
242 }
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:1327
bootPlugins()
Boot plugins.
Definition: BootHandler.php:90
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:22
registerRoutes()
Register core routes.
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:254
__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
$action
Definition: subscribe.php:11
Load, boot, and implement a front controller for an Elgg application.
Definition: Application.php:48
elgg_set_entity_class(string $type, string $subtype, string $class= '')
Sets class constructor name for entities with given type and subtype.
Definition: entities.php:41
$handler
Definition: add.php:7
Handles application boot sequence.
Definition: BootHandler.php:12
$priority