3939use ElementaryFramework \WaterPipe \Routing \Middleware \Middleware ;
4040use ElementaryFramework \WaterPipe \Routing \Route ;
4141use ElementaryFramework \WaterPipe \Routing \RouteAction ;
42+ use ElementaryFramework \WaterPipe \Exceptions \NotFoundErrorException ;
43+ use ElementaryFramework \WaterPipe \Exceptions \InternalServerErrorException ;
4244
45+ // TODO: Errors registry
4346class WaterPipe
4447{
4548 /**
@@ -52,7 +55,7 @@ class WaterPipe
5255
5356 /**
5457 * Defines if the pipe is running or not.
55- *
58+ *
5659 * @var bool
5760 */
5861 private $ _isRunning ;
@@ -71,15 +74,54 @@ class WaterPipe
7174 */
7275 private $ _middlewareRegistry ;
7376
77+ /**
78+ * The array of registered get requests.
79+ *
80+ * @var callable[]
81+ */
7482 private $ _getRequestRegistry ;
83+
84+ /**
85+ * The array of registered post requests.
86+ *
87+ * @var callable[]
88+ */
7589 private $ _postRequestRegistry ;
90+
91+ /**
92+ * The array of registered put requests.
93+ *
94+ * @var callable[]
95+ */
7696 private $ _putRequestRegistry ;
97+
98+ /**
99+ * The array of registered delete requests.
100+ *
101+ * @var callable[]
102+ */
77103 private $ _deleteRequestRegistry ;
78104
105+ /**
106+ * The array of registered requests.
107+ *
108+ * @var callable[]
109+ */
79110 private $ _requestRegistry ;
80111
112+ /**
113+ * The array of registered error
114+ * handlers.
115+ *
116+ * @var callable[]
117+ */
81118 private $ _errorsRegistry ;
82119
120+ /**
121+ * The array of sub pipes.
122+ *
123+ * @var callable[]
124+ */
83125 private $ _pipesRegistry ;
84126
85127 /**
@@ -116,6 +158,9 @@ public static function triggerBeforeExecuteEvent(Request &$request)
116158 }
117159 }
118160
161+ /**
162+ * WaterPipe __constructor
163+ */
119164 public function __construct ()
120165 {
121166 $ this ->_isRunning = false ;
@@ -129,6 +174,12 @@ public function __construct()
129174 $ this ->_pipesRegistry = array ();
130175 }
131176
177+ /**
178+ * Register a plugin in this pipe.
179+ *
180+ * @param MiddleWare|Route|WaterPipe $plugin The plugin to use (Middleware,
181+ * Route or another WaterPipe).
182+ */
132183 public function use ($ plugin )
133184 {
134185 if ($ plugin instanceof Middleware) {
@@ -153,36 +204,89 @@ public function use($plugin)
153204 }
154205 }
155206
207+ /**
208+ * Register a request handled by this pipe.
209+ *
210+ * @param string $uri The request URI.
211+ * @param callable $action The action to call when the request
212+ * correspond to the given URI.
213+ */
156214 public function request (string $ uri , $ action )
157215 {
158216 $ this ->_requestRegistry [$ uri ] = $ action ;
159217 }
160218
219+ /**
220+ * Register a GET request handled by this pipe.
221+ *
222+ * @param string $uri The request URI.
223+ * @param callable $action The action to call when the request
224+ * correspond to the given URI.
225+ */
161226 public function get (string $ uri , $ action )
162227 {
163228 $ this ->_getRequestRegistry [$ uri ] = $ action ;
164229 }
165230
231+ /**
232+ * Register a POST request handled by this pipe.
233+ *
234+ * @param string $uri The request URI.
235+ * @param callable $action The action to call when the request
236+ * correspond to the given URI.
237+ */
166238 public function post (string $ uri , $ action )
167239 {
168240 $ this ->_postRequestRegistry [$ uri ] = $ action ;
169241 }
170242
243+ /**
244+ * Register a PUT request handled by this pipe.
245+ *
246+ * @param string $uri The request URI.
247+ * @param callable $action The action to call when the request
248+ * correspond to the given URI.
249+ */
171250 public function put (string $ uri , $ action )
172251 {
173252 $ this ->_putRequestRegistry [$ uri ] = $ action ;
174253 }
175254
255+ /**
256+ * Register a DELETE request handled by this pipe.
257+ *
258+ * @param string $uri The request URI.
259+ * @param callable $action The action to call when the request
260+ * correspond to the given URI.
261+ */
176262 public function delete (string $ uri , $ action )
177263 {
178264 $ this ->_deleteRequestRegistry [$ uri ] = $ action ;
179265 }
180266
267+ /**
268+ * Register a sub pipe managed by this pipe.
269+ *
270+ * @param string $uri The request URI.
271+ * @param callable $action The action to call when the request
272+ * correspond to the given URI.
273+ */
181274 public function pipe (string $ baseUri , WaterPipe $ pipe )
182275 {
183276 $ this ->_pipesRegistry [$ baseUri ] = $ pipe ;
184277 }
185278
279+ /**
280+ * Register a error handler for this pipe.
281+ *
282+ * @param int $code The error code.
283+ * @param callable $action The action to call when an error of this code appear.
284+ */
285+ public function error (int $ code , $ action )
286+ {
287+ $ this ->_errorsRegistry [$ code ] = $ action ;
288+ }
289+
186290 /**
187291 * Run the water pipe.
188292 *
@@ -239,8 +343,9 @@ private function _executeRequest()
239343
240344 switch (Request::getInstance ()->getMethod ()) {
241345 case RequestMethod::UNKNOWN :
242- // TODO: 500 Internal Server Error. Unable to determine the request method.
243- throw new \Exception ("500 Error " );
346+ if (isset ($ this ->_errorsRegistry [500 ]))
347+ return $ this ->_executeAction ($ this ->_errorsRegistry [500 ]);
348+ else throw new InternalServerErrorException ();
244349
245350 case RequestMethod::GET :
246351 $ registry = $ this ->_getRequestRegistry ;
@@ -270,8 +375,9 @@ private function _executeRequest()
270375 }
271376
272377 if ($ runner === null ) {
273- // TODO: 404 Not Found Error. Unable to find a route for this URI.
274- throw new \Exception ("404 Error " );
378+ if (isset ($ this ->_errorsRegistry [404 ]))
379+ return $ this ->_executeAction ($ this ->_errorsRegistry [404 ]);
380+ else throw new NotFoundErrorException ();
275381 }
276382
277383 if (!is_callable ($ runner ) && !is_array ($ runner ) && !is_subclass_of ($ runner , RouteAction::class)) {
@@ -282,7 +388,12 @@ private function _executeRequest()
282388 // Execute middleware
283389 self ::triggerBeforeExecuteEvent (Request::getInstance ());
284390
285- // NOTE: No code will be executed after this block...
391+ // NOTE: No code will be executed after this call...
392+ $ this ->_executeAction ($ runner );
393+ }
394+
395+ private function _executeAction ($ runner )
396+ {
286397 if (is_callable ($ runner ) || is_array ($ runner )) {
287398 call_user_func_array ($ runner , array (Request::getInstance (), new Response ()));
288399 } elseif (is_subclass_of ($ runner , RouteAction::class)) {
0 commit comments