4242
4343class WaterPipe
4444{
45+ /**
46+ * Stores the only running instance
47+ * of WaterPipe.
48+ *
49+ * @var WaterPipe
50+ */
51+ private static $ _runningInstance = null ;
52+
4553 /**
4654 * Defines if the pipe is running or not.
4755 *
@@ -74,6 +82,40 @@ class WaterPipe
7482
7583 private $ _pipesRegistry ;
7684
85+ /**
86+ * @return WaterPipe
87+ */
88+ public static function getRunningInstance (): WaterPipe
89+ {
90+ return self ::$ _runningInstance ;
91+ }
92+
93+ /**
94+ * Triggers all middlewares with before send event
95+ * defined on the running pipe.
96+ *
97+ * @param Response $response The response on which trigger the event.
98+ */
99+ public static function triggerBeforeSendEvent (Response &$ response )
100+ {
101+ foreach (self ::$ _runningInstance ->_middlewareRegistry as $ middleware ) {
102+ $ middleware ->beforeSend ($ response );
103+ }
104+ }
105+
106+ /**
107+ * Triggers all middlewares with before execute event
108+ * defined on the running pipe.
109+ *
110+ * @param Request $request The request to execute.
111+ */
112+ public static function triggerBeforeExecuteEvent (Request &$ request )
113+ {
114+ foreach (self ::$ _runningInstance ->_middlewareRegistry as $ middleware ) {
115+ $ middleware ->beforeExecute ($ request );
116+ }
117+ }
118+
77119 public function __construct ()
78120 {
79121 $ this ->_isRunning = false ;
@@ -154,6 +196,8 @@ public function run()
154196 {
155197 $ this ->_isRunning = true ;
156198
199+ self ::$ _runningInstance = $ this ;
200+
157201 Request::capture ();
158202
159203 $ pipe = $ this ->_findSubPipe ();
@@ -165,12 +209,19 @@ public function run()
165209 }
166210 }
167211
212+ /**
213+ * @param string $baseUri
214+ * @throws \Exception
215+ */
168216 private function _runBase (string $ baseUri )
169217 {
170218 $ this ->_baseUri = $ baseUri ;
171219 $ this ->run ();
172220 }
173221
222+ /**
223+ * @return array|null
224+ */
174225 private function _findSubPipe ()
175226 {
176227 foreach ($ this ->_pipesRegistry as $ baseUri => $ pipe ) {
@@ -223,22 +274,28 @@ private function _executeRequest()
223274 throw new \Exception ("404 Error " );
224275 }
225276
226- // NOTE: No code will normally not be executed after this block...
277+ if (!is_callable ($ runner ) && !is_array ($ runner ) && !is_subclass_of ($ runner , RouteAction::class)) {
278+ // TODO: Proper exception
279+ throw new \Exception ("Malformed route action " );
280+ }
281+
282+ // Execute middleware
283+ self ::triggerBeforeExecuteEvent (Request::getInstance ());
284+
285+ // NOTE: No code will be executed after this block...
227286 if (is_callable ($ runner ) || is_array ($ runner )) {
228287 call_user_func_array ($ runner , array (Request::getInstance (), new Response ()));
229288 } elseif (is_subclass_of ($ runner , RouteAction::class)) {
230289 if (is_string ($ runner )) {
231290 $ runner = new $ runner ;
232291 }
233292 $ runner ->execute ();
234- } else {
235- throw new \Exception ("Malformed route action " );
236293 }
237294 }
238295
239296 /**
240297 * @param array $routes
241- * @return callable
298+ * @return callable|RouteAction
242299 * @throws Exceptions\RequestUriBuilderException
243300 */
244301 private function _getActionForRoutes (array $ routes )
0 commit comments