Skip to content

Commit 2ebc3d6

Browse files
committed
Execute middleware
1 parent bdd6e9e commit 2ebc3d6

File tree

3 files changed

+68
-21
lines changed

3 files changed

+68
-21
lines changed

src/WaterPipe/HTTP/Response/Response.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
namespace ElementaryFramework\WaterPipe\HTTP\Response;
3434

3535
use ElementaryFramework\WaterPipe\Exceptions\FileNotFoundException;
36+
use ElementaryFramework\WaterPipe\WaterPipe;
3637
use ElementaryFramework\WaterPipe\WaterPipeConfig;
3738

3839
class Response
@@ -69,6 +70,9 @@ public function __construct()
6970
*/
7071
public function send()
7172
{
73+
// Execute middleware
74+
WaterPipe::triggerBeforeSendEvent($this);
75+
7276
// Set status code
7377
$code = $this->_status->getCode();
7478
$text = $this->_status->getDescription();

src/WaterPipe/Routing/Middleware/Middleware.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,11 @@
3333
namespace ElementaryFramework\WaterPipe\Routing\Middleware;
3434

3535
use ElementaryFramework\WaterPipe\HTTP\Request\Request;
36+
use ElementaryFramework\WaterPipe\HTTP\Response\Response;
3637

3738
abstract class Middleware
3839
{
39-
/**
40-
* @var Request
41-
*/
42-
protected $_request;
40+
public abstract function beforeExecute(Request &$request);
4341

44-
/**
45-
* RouteAction constructor.
46-
*
47-
* @throws \Exception
48-
*/
49-
public function __construct()
50-
{
51-
$this->_request =& Request::getInstance();
52-
}
53-
54-
public abstract function beforeExecute();
55-
56-
public abstract function afterExecute();
42+
public abstract function beforeSend(Response &$response);
5743
}

src/WaterPipe/WaterPipe.php

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242

4343
class 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

Comments
 (0)