Elgg  Version 5.1
ShutdownHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Application;
4 
8 
15 
16  use Loggable;
17 
21  protected $app;
22 
28  public function __construct(Application $app) {
29  $this->app = $app;
30  }
31 
35  public function __invoke() {
36  try {
37  $this->shutdownDatabase();
38  $this->shutdownApplication();
39  $this->persistCaches();
40  } catch (\Throwable $e) {
41  $this->log(LogLevel::CRITICAL, $e);
42  }
43  }
44 
49  public function shutdownDatabase() {
50  $services = $this->app->internal_services;
51 
52  if ($services->db) {
53  $services->db->executeDelayedQueries();
54 
55  $db_calls = $services->db->getQueryCount();
56  $this->log(LogLevel::INFO, "DB Queries for this page: $db_calls");
57  }
58  }
59 
75  public function shutdownApplication() {
76  $services = $this->app->internal_services;
77 
78  if (!$services->events->triggerBefore('shutdown', 'system')) {
79  return;
80  }
81 
82  $services->events->trigger('shutdown', 'system');
83 
84  global $GLOBALS;
85  if (isset($GLOBALS['START_MICROTIME'])) {
86  $time = (float) (microtime(true) - $GLOBALS['START_MICROTIME']);
87  $uri = $services->request->server->get('REQUEST_URI', 'CLI');
88  $this->log(LogLevel::INFO, "Page {$uri} generated in $time seconds");
89  }
90 
91  $services->events->triggerAfter('shutdown', 'system');
92  }
93 
98  public function persistCaches() {
99  $this->app->internal_services->autoloadManager->saveCache();
100  }
101 }
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
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:48
shutdownDatabase()
Shutdown the database, execute delayed queries and do some logging.
__construct(Application $app)
Constructor.