Elgg  Version 5.1
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->allowPathRewrite();
137 
138  // Complete the boot process for both engine and plugins
139  $events->triggerSequence('init', 'system');
140 
141  $this->app->setBootStatus('full_boot_completed', true);
142 
143  // Tell the access functions the system has booted, plugins are loaded,
144  // and the user is logged in so it can start caching
145  $this->app->internal_services->accessCollections->markInitComplete();
146 
147  // System loaded and ready
148  $events->triggerSequence('ready', 'system');
149 
150  $this->app->setBootStatus('application_boot_completed', true);
151  }
152 
158  public function setEntityClasses(): void {
159  elgg_set_entity_class('user', 'user', \ElggUser::class);
160  elgg_set_entity_class('group', 'group', \ElggGroup::class);
161  elgg_set_entity_class('site', 'site', \ElggSite::class);
162  elgg_set_entity_class('object', 'plugin', \ElggPlugin::class);
163  elgg_set_entity_class('object', 'file', \ElggFile::class);
164  elgg_set_entity_class('object', 'widget', \ElggWidget::class);
165  elgg_set_entity_class('object', 'comment', \ElggComment::class);
166  elgg_set_entity_class('object', 'elgg_upgrade', \ElggUpgrade::class);
167  elgg_set_entity_class('object', 'admin_notice', \ElggAdminNotice::class);
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 }
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:25
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