From a2c74ddadd667d50400ddf0a048542e987ad6b83 Mon Sep 17 00:00:00 2001
From: n0nag0n <n0nag0n@sky-9.com>
Date: Fri, 14 Mar 2025 23:08:12 -0600
Subject: [PATCH] added event cache check and tweaked middleware event

---
 flight/Engine.php               | 18 +++++++++---------
 flight/core/EventDispatcher.php |  6 +++++-
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/flight/Engine.php b/flight/Engine.php
index 63fd05d..f459716 100644
--- a/flight/Engine.php
+++ b/flight/Engine.php
@@ -486,7 +486,7 @@ class Engine
             // Which loosely translates to $class->$method($params)
             $start = microtime(true);
             $middlewareResult = $middlewareObject($params);
-            $this->triggerEvent('flight.middleware.executed', $route, $middleware, microtime(true) - $start);
+            $this->triggerEvent('flight.middleware.executed', $route, $middleware, $eventName, microtime(true) - $start);
 
             if ($useV3OutputBuffering === true) {
                 $this->response()->write(ob_get_clean());
@@ -1002,10 +1002,10 @@ class Engine
 
         $this->response()->header('ETag', '"' . str_replace('"', '\"', $id) . '"');
 
-        if (
-            isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
-            $_SERVER['HTTP_IF_NONE_MATCH'] === $id
-        ) {
+        $hit = isset($_SERVER['HTTP_IF_NONE_MATCH']) && $_SERVER['HTTP_IF_NONE_MATCH'] === $id;
+        $this->triggerEvent('flight.cache.checked', 'etag', $hit, 0.0);
+
+        if ($hit === true) {
             $this->response()->clear();
             $this->halt(304, '', empty(getenv('PHPUNIT_TEST')));
         }
@@ -1020,10 +1020,10 @@ class Engine
     {
         $this->response()->header('Last-Modified', gmdate('D, d M Y H:i:s \G\M\T', $time));
 
-        if (
-            isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
-            strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $time
-        ) {
+        $hit = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) === $time;
+        $this->triggerEvent('flight.cache.checked', 'lastModified', $hit, 0.0);
+
+        if ($hit === true) {
             $this->response()->clear();
             $this->halt(304, '', empty(getenv('PHPUNIT_TEST')));
         }
diff --git a/flight/core/EventDispatcher.php b/flight/core/EventDispatcher.php
index ea809b2..8e60d1a 100644
--- a/flight/core/EventDispatcher.php
+++ b/flight/core/EventDispatcher.php
@@ -44,9 +44,12 @@ class EventDispatcher
      *
      * @param string $event Event name
      * @param mixed ...$args Arguments to pass to the callbacks
+     *
+     * @return mixed
      */
-    public function trigger(string $event, ...$args): void
+    public function trigger(string $event, ...$args)
     {
+        $result = null;
         if (isset($this->listeners[$event]) === true) {
             foreach ($this->listeners[$event] as $callback) {
                 $result = call_user_func_array($callback, $args);
@@ -57,6 +60,7 @@ class EventDispatcher
                 }
             }
         }
+        return $result;
     }
 
     /**