Skip to content

Commit

Permalink
Ensure teardown is always called, use a shutdown function
Browse files Browse the repository at this point in the history
Also add a flag in Media to be able to distinguish request types,
which is useful when logging.
  • Loading branch information
gerryd committed Nov 21, 2024
1 parent ef93c8b commit f9d6d86
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Skeleton/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', []);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions lib/Skeleton/Core/Http/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ class Media {
],
];

/**
* Flag to register whether media was served or not
*
* @access protected
*/
protected static bool $served = false;

/**
* Constructor
*
Expand Down Expand Up @@ -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();
Expand All @@ -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
*
Expand Down

0 comments on commit f9d6d86

Please sign in to comment.