Skip to content

Commit a2c74dd

Browse files
committed
added event cache check and tweaked middleware event
1 parent 12725eb commit a2c74dd

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

flight/Engine.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ protected function processMiddleware(Route $route, string $eventName): bool
486486
// Which loosely translates to $class->$method($params)
487487
$start = microtime(true);
488488
$middlewareResult = $middlewareObject($params);
489-
$this->triggerEvent('flight.middleware.executed', $route, $middleware, microtime(true) - $start);
489+
$this->triggerEvent('flight.middleware.executed', $route, $middleware, $eventName, microtime(true) - $start);
490490

491491
if ($useV3OutputBuffering === true) {
492492
$this->response()->write(ob_get_clean());
@@ -1002,10 +1002,10 @@ public function _etag(string $id, string $type = 'strong'): void
10021002

10031003
$this->response()->header('ETag', '"' . str_replace('"', '\"', $id) . '"');
10041004

1005-
if (
1006-
isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
1007-
$_SERVER['HTTP_IF_NONE_MATCH'] === $id
1008-
) {
1005+
$hit = isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $id;
1006+
$this->triggerEvent('flight.cache.checked', 'etag', $hit, 0.0);
1007+
1008+
if ($hit === true) {
10091009
$this->response()->clear();
10101010
$this->halt(304, '', empty(getenv('PHPUNIT_TEST')));
10111011
}
@@ -1020,10 +1020,10 @@ public function _lastModified(int $time): void
10201020
{
10211021
$this->response()->header('Last-Modified', gmdate('D, d M Y H:i:s \G\M\T', $time));
10221022

1023-
if (
1024-
isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
1025-
strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $time
1026-
) {
1023+
$hit = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $time;
1024+
$this->triggerEvent('flight.cache.checked', 'lastModified', $hit, 0.0);
1025+
1026+
if ($hit === true) {
10271027
$this->response()->clear();
10281028
$this->halt(304, '', empty(getenv('PHPUNIT_TEST')));
10291029
}

flight/core/EventDispatcher.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ public function on(string $event, callable $callback): void
4444
*
4545
* @param string $event Event name
4646
* @param mixed ...$args Arguments to pass to the callbacks
47+
*
48+
* @return mixed
4749
*/
48-
public function trigger(string $event, ...$args): void
50+
public function trigger(string $event, ...$args)
4951
{
52+
$result = null;
5053
if (isset($this->listeners[$event]) === true) {
5154
foreach ($this->listeners[$event] as $callback) {
5255
$result = call_user_func_array($callback, $args);
@@ -57,6 +60,7 @@ public function trigger(string $event, ...$args): void
5760
}
5861
}
5962
}
63+
return $result;
6064
}
6165

6266
/**

0 commit comments

Comments
 (0)