Elgg  Version master
ShutdownHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Application;
4 
8 
15 
16  use Loggable;
17 
23  public function __construct(protected Application $app) {
24  }
25 
29  public function __invoke() {
30  try {
31  $this->shutdownDatabase();
32  $this->shutdownApplication();
33  $this->persistCaches();
34  } catch (\Throwable $e) {
35  $this->log(LogLevel::CRITICAL, $e);
36  }
37  }
38 
43  public function shutdownDatabase() {
44  $services = $this->app->internal_services;
45 
46  if ($services->db) {
47  $services->db->executeDelayedQueries();
48 
49  $db_calls = $services->db->getQueryCount();
50  $this->log(LogLevel::INFO, "DB Queries for this page: $db_calls");
51  }
52  }
53 
69  public function shutdownApplication() {
70  $services = $this->app->internal_services;
71 
72  if (!$services->events->triggerBefore('shutdown', 'system')) {
73  return;
74  }
75 
76  $services->events->trigger('shutdown', 'system');
77 
78  global $GLOBALS;
79  if (isset($GLOBALS['START_MICROTIME'])) {
80  $time = (float) (microtime(true) - $GLOBALS['START_MICROTIME']);
81  $uri = $services->request->server->get('REQUEST_URI', 'CLI');
82  $this->log(LogLevel::INFO, "Page {$uri} generated in $time seconds");
83  }
84 
85  $services->events->triggerAfter('shutdown', 'system');
86  }
87 
92  public function persistCaches() {
93  $this->app->internal_services->autoloadManager->saveCache();
94  }
95 }
shutdownApplication()
Emits a shutdown:system event upon PHP shutdown, but before database connections are dropped...
if(!$annotation instanceof ElggAnnotation) $time
Definition: time.php:20
persistCaches()
Persist some of the core caches.
trait Loggable
Enables adding a logger.
Definition: Loggable.php:14
__construct(protected Application $app)
Constructor.
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:58
Load, boot, and implement a front controller for an Elgg application.
Definition: Application.php:47
shutdownDatabase()
Shutdown the database, execute delayed queries and do some logging.