From f9d6d8610dad305ad539888a17f2e58862d2aada Mon Sep 17 00:00:00 2001 From: Gerry Demaret Date: Thu, 21 Nov 2024 11:35:34 -0400 Subject: [PATCH] Ensure teardown is always called, use a shutdown function Also add a flag in Media to be able to distinguish request types, which is useful when logging. --- lib/Skeleton/Core/Application.php | 4 +++- lib/Skeleton/Core/Http/Media.php | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/Skeleton/Core/Application.php b/lib/Skeleton/Core/Application.php index aad9486..42f1637 100644 --- a/lib/Skeleton/Core/Application.php +++ b/lib/Skeleton/Core/Application.php @@ -123,10 +123,12 @@ public function accept(): void { \Skeleton\Core\Application::set($this); $continue = $this->call_event('application', 'bootstrap', []); + + register_shutdown_function([$this, 'call_event'], 'application', 'teardown', []); + if ($continue) { $this->run(); } - $this->call_event('application', 'teardown', []); } /** diff --git a/lib/Skeleton/Core/Http/Media.php b/lib/Skeleton/Core/Http/Media.php index b431a70..7c4e61e 100644 --- a/lib/Skeleton/Core/Http/Media.php +++ b/lib/Skeleton/Core/Http/Media.php @@ -80,6 +80,13 @@ class Media { ], ]; + /** + * Flag to register whether media was served or not + * + * @access protected + */ + protected static bool $served = false; + /** * Constructor * @@ -116,6 +123,8 @@ public function has_known_extension(): bool { * @access public */ public function serve(): void { + self::$served = true; + // Send the Etag before potentially replying with 304 header('Etag: ' . crc32((string)$this->get_mtime()) . '-' . sha1($this->get_path())); $this->http_if_modified(); @@ -124,6 +133,15 @@ public function serve(): void { exit; } + /** + * Has media been served? + * + * @access public + */ + public static function is_served(): bool { + return self::$served; + } + /** * Detect if the request is a request for media *