Skip to content

Commit 1acea20

Browse files
committed
Propagate errors registry n plugins and subpipes
1 parent 265b13d commit 1acea20

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/WaterPipe/WaterPipe.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,50 +76,50 @@ class WaterPipe
7676

7777
/**
7878
* The array of registered get requests.
79-
*
79+
*
8080
* @var callable[]
8181
*/
8282
private $_getRequestRegistry;
8383

8484
/**
8585
* The array of registered post requests.
86-
*
86+
*
8787
* @var callable[]
8888
*/
8989
private $_postRequestRegistry;
9090

9191
/**
9292
* The array of registered put requests.
93-
*
93+
*
9494
* @var callable[]
9595
*/
9696
private $_putRequestRegistry;
9797

9898
/**
9999
* The array of registered delete requests.
100-
*
100+
*
101101
* @var callable[]
102102
*/
103103
private $_deleteRequestRegistry;
104104

105105
/**
106106
* The array of registered requests.
107-
*
107+
*
108108
* @var callable[]
109109
*/
110110
private $_requestRegistry;
111111

112112
/**
113113
* The array of registered error
114114
* handlers.
115-
*
115+
*
116116
* @var callable[]
117117
*/
118118
private $_errorsRegistry;
119119

120120
/**
121121
* The array of sub pipes.
122-
*
122+
*
123123
* @var callable[]
124124
*/
125125
private $_pipesRegistry;
@@ -176,7 +176,7 @@ public function __construct()
176176

177177
/**
178178
* Register a plugin in this pipe.
179-
*
179+
*
180180
* @param MiddleWare|Route|WaterPipe $plugin The plugin to use (Middleware,
181181
* Route or another WaterPipe).
182182
*/
@@ -194,6 +194,7 @@ public function use($plugin)
194194
"get" => $plugin->_getRequestRegistry,
195195
"post" => $plugin->_postRequestRegistry,
196196
"put" => $plugin->_putRequestRegistry,
197+
"error" => $plugin->_errorsRegistry,
197198
"delete" => $plugin->_deleteRequestRegistry) as $method => $registry) {
198199

199200
foreach ($registry as $uri => $action) {
@@ -206,7 +207,7 @@ public function use($plugin)
206207

207208
/**
208209
* Register a request handled by this pipe.
209-
*
210+
*
210211
* @param string $uri The request URI.
211212
* @param callable $action The action to call when the request
212213
* correspond to the given URI.
@@ -218,7 +219,7 @@ public function request(string $uri, $action)
218219

219220
/**
220221
* Register a GET request handled by this pipe.
221-
*
222+
*
222223
* @param string $uri The request URI.
223224
* @param callable $action The action to call when the request
224225
* correspond to the given URI.
@@ -230,7 +231,7 @@ public function get(string $uri, $action)
230231

231232
/**
232233
* Register a POST request handled by this pipe.
233-
*
234+
*
234235
* @param string $uri The request URI.
235236
* @param callable $action The action to call when the request
236237
* correspond to the given URI.
@@ -242,7 +243,7 @@ public function post(string $uri, $action)
242243

243244
/**
244245
* Register a PUT request handled by this pipe.
245-
*
246+
*
246247
* @param string $uri The request URI.
247248
* @param callable $action The action to call when the request
248249
* correspond to the given URI.
@@ -254,7 +255,7 @@ public function put(string $uri, $action)
254255

255256
/**
256257
* Register a DELETE request handled by this pipe.
257-
*
258+
*
258259
* @param string $uri The request URI.
259260
* @param callable $action The action to call when the request
260261
* correspond to the given URI.
@@ -266,7 +267,7 @@ public function delete(string $uri, $action)
266267

267268
/**
268269
* Register a sub pipe managed by this pipe.
269-
*
270+
*
270271
* @param string $uri The request URI.
271272
* @param callable $action The action to call when the request
272273
* correspond to the given URI.
@@ -278,7 +279,7 @@ public function pipe(string $baseUri, WaterPipe $pipe)
278279

279280
/**
280281
* Register a error handler for this pipe.
281-
*
282+
*
282283
* @param int $code The error code.
283284
* @param callable $action The action to call when an error of this code appear.
284285
*/
@@ -309,6 +310,11 @@ public function run()
309310
if ($pipe === null) {
310311
$this->_executeRequest();
311312
} else {
313+
// Propagate errors registry only
314+
foreach ($this->_errorsRegistry as $code => $action)
315+
if (!isset($pipe[1]->_errorsRegistry[$code]))
316+
$pipe[1]->_errorsRegistry[$code] = $action;
317+
312318
$pipe[1]->_runBase($pipe[0]);
313319
}
314320
}
@@ -328,7 +334,7 @@ private function _runBase(string $baseUri)
328334
*/
329335
private function _findSubPipe()
330336
{
331-
foreach ($this->_pipesRegistry as $baseUri => $pipe) {
337+
foreach ($this->_pipesRegistry as $baseUri => &$pipe) {
332338
if (preg_match("#^" . RequestUri::makeUri($this->_baseUri, RequestUri::pattern2regex($baseUri)) . "#", Request::getInstance()->uri->getUri())) {
333339
return array(RequestUri::makeUri($this->_baseUri, $baseUri), $pipe);
334340
}
@@ -423,4 +429,4 @@ private function _getActionForRoutes(array $routes)
423429

424430
return $runner;
425431
}
426-
}
432+
}

0 commit comments

Comments
 (0)