Elgg  Version 3.0
ShutdownHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Application;
4 
7 use Elgg\Loggable;
8 use Exception;
13 
20 
21  use Loggable;
22 
26  protected $app;
27 
33  public function __construct(Application $app) {
34  $this->app = $app;
35  }
36 
40  public function __invoke() {
41  try {
42  $this->shutdownDatabase();
43  $this->shutdownApplication();
44  $this->persistCaches();
45  } catch (\Throwable $e) {
46  $this->log(LogLevel::CRITICAL, $e);
47  }
48  }
49 
54  public function shutdownDatabase() {
55  $services = $this->app->_services;
56 
57  if ($services->db) {
58  $services->db->executeDelayedQueries();
59 
60  $db_calls = $services->db->getQueryCount();
61  $this->log(LogLevel::INFO, "DB Queries for this page: $db_calls");
62  }
63  }
64 
80  public function shutdownApplication() {
81  $services = $this->app->_services;
82 
83  if (!$services->events->triggerBefore('shutdown', 'system')) {
84  return;
85  }
86 
87  $services->events->trigger('shutdown', 'system');
88 
89  global $GLOBALS;
90  if (isset($GLOBALS['START_MICROTIME'])) {
91  $time = (float) (microtime(true) - $GLOBALS['START_MICROTIME']);
92  $uri = $services->request->server->get('REQUEST_URI', 'CLI');
93  $this->log(LogLevel::INFO, "Page {$uri} generated in $time seconds");
94  }
95 
96  $services->events->triggerAfter('shutdown', 'system');
97  }
98 
103  public function persistCaches() {
104  $this->app->_services->autoloadManager->saveCache();
105  }
106 }
shutdownApplication()
Emits a shutdown:system event upon PHP shutdown, but before database connections are dropped...
trait Loggable
Enables adding a logger.
Definition: Loggable.php:12
persistCaches()
Persist some of the core caches.
if(!$entity instanceof ElggEntity) $time
Definition: time.php:21
elgg global
Pointer to the global context.
Definition: elgglib.js:12
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:55
Load, boot, and implement a front controller for an Elgg application.
Definition: Application.php:50
shutdownDatabase()
Shutdown the database, execute delayed queries and do some logging.
__construct(Application $app)
Constructor.