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->setEntityClasses();
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 setEntityClasses(): void {
153  elgg_set_entity_class('user', 'user', \ElggUser::class);
154  elgg_set_entity_class('group', 'group', \ElggGroup::class);
155  elgg_set_entity_class('site', 'site', \ElggSite::class);
156  elgg_set_entity_class('object', 'plugin', \ElggPlugin::class);
157  elgg_set_entity_class('object', 'file', \ElggFile::class);
158  elgg_set_entity_class('object', 'widget', \ElggWidget::class);
159  elgg_set_entity_class('object', 'comment', \ElggComment::class);
160  elgg_set_entity_class('object', 'elgg_upgrade', \ElggUpgrade::class);
161  elgg_set_entity_class('object', 'admin_notice', \ElggAdminNotice::class);
162  }
163 
169  protected function registerEvents(): void {
170  $conf = \Elgg\Project\Paths::elgg() . 'engine/events.php';
171  $spec = \Elgg\Includer::includeFile($conf);
172 
173  $events = $this->app->internal_services->events;
174 
175  foreach ($spec as $name => $types) {
176  foreach ($types as $type => $callbacks) {
177  foreach ($callbacks as $callback => $event_spec) {
178  if (!is_array($event_spec)) {
179  continue;
180  }
181 
182  $unregister = (bool) elgg_extract('unregister', $event_spec, false);
183 
184  if ($unregister) {
185  $events->unregisterHandler($name, $type, $callback);
186  } else {
187  $priority = (int) elgg_extract('priority', $event_spec, 500);
188 
189  $events->registerHandler($name, $type, $callback, $priority);
190  }
191  }
192  }
193  }
194  }
195 
201  protected function registerRoutes(): void {
202  $conf = \Elgg\Project\Paths::elgg() . 'engine/routes.php';
203  $routes = \Elgg\Includer::includeFile($conf);
204 
205  foreach ($routes as $name => $def) {
206  $this->app->internal_services->routes->register($name, $def);
207  }
208  }
209 
215  protected function registerActions(): void {
216  $conf = \Elgg\Project\Paths::elgg() . 'engine/actions.php';
218 
219  $root_path = \Elgg\Project\Paths::elgg();
220 
221  foreach ($actions as $action => $action_spec) {
222  if (!is_array($action_spec)) {
223  continue;
224  }
225 
226  $access = elgg_extract('access', $action_spec, 'logged_in');
227  $handler = elgg_extract('controller', $action_spec);
228  if (!$handler) {
229  $handler = elgg_extract('filename', $action_spec) ?: "{$root_path}/actions/{$action}.php";
230  }
231 
232  // unset handled action specs, pass the rest to the action service
233  unset($action_spec['access']);
234  unset($action_spec['controller']);
235  unset($action_spec['filename']);
236 
237  $this->app->internal_services->actions->register($action, $handler, $access, $action_spec);
238  }
239  }
240 }
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:1306
bootPlugins()
Boot plugins.
Definition: BootHandler.php:84
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
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:43
$type
Definition: delete.php:21
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:28
setEntityClasses()
Set core entity classes.
__construct(protected Application $app)
Constructor.
Definition: BootHandler.php:19
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:47
elgg_set_entity_class(string $type, string $subtype, string $class= '')
Sets class constructor name for entities with given type and subtype.
Definition: entities.php:44
$handler
Definition: add.php:7
Handles application boot sequence.
Definition: BootHandler.php:12
$priority