4141use ElementaryFramework \WaterPipe \Routing \Middleware \Middleware ;
4242use ElementaryFramework \WaterPipe \Routing \Route ;
4343use ElementaryFramework \WaterPipe \Routing \RouteAction ;
44+ use ElementaryFramework \WaterPipe \Routing \RouteMap ;
4445
4546class WaterPipe
4647{
@@ -101,6 +102,20 @@ class WaterPipe
101102 */
102103 private $ _deleteRequestRegistry ;
103104
105+ /**
106+ * The array of registered head requests.
107+ *
108+ * @var callable[]
109+ */
110+ private $ _headRequestRegistry ;
111+
112+ /**
113+ * The array of registered patch requests.
114+ *
115+ * @var callable[]
116+ */
117+ private $ _patchRequestRegistry ;
118+
104119 /**
105120 * The array of registered requests.
106121 *
@@ -123,6 +138,13 @@ class WaterPipe
123138 */
124139 private $ _pipesRegistry ;
125140
141+ /**
142+ * The array of route maps.
143+ *
144+ * @var RouteMap[]
145+ */
146+ private $ _mapRegistry ;
147+
126148 /**
127149 * @return WaterPipe
128150 */
@@ -168,9 +190,12 @@ public function __construct()
168190 $ this ->_postRequestRegistry = array ();
169191 $ this ->_putRequestRegistry = array ();
170192 $ this ->_deleteRequestRegistry = array ();
193+ $ this ->_headRequestRegistry = array ();
194+ $ this ->_patchRequestRegistry = array ();
171195 $ this ->_requestRegistry = array ();
172196 $ this ->_errorsRegistry = array ();
173197 $ this ->_pipesRegistry = array ();
198+ $ this ->_mapRegistry = array ();
174199 }
175200
176201 /**
@@ -184,7 +209,7 @@ public function use($plugin)
184209 if ($ plugin instanceof Middleware) {
185210 array_push ($ this ->_middlewareRegistry , $ plugin );
186211 } elseif ($ plugin instanceof Route) {
187- foreach (array ("request " , "get " , "post " , "put " , "delete " ) as $ method ) {
212+ foreach (array ("request " , "get " , "post " , "put " , "delete " , " head " , " patch " ) as $ method ) {
188213 $ this ->$ method ($ plugin ->getUri (), array ($ plugin , $ method ));
189214 }
190215 } elseif ($ plugin instanceof WaterPipe) {
@@ -194,7 +219,9 @@ public function use($plugin)
194219 "post " => $ plugin ->_postRequestRegistry ,
195220 "put " => $ plugin ->_putRequestRegistry ,
196221 "error " => $ plugin ->_errorsRegistry ,
197- "delete " => $ plugin ->_deleteRequestRegistry ) as $ method => $ registry ) {
222+ "delete " => $ plugin ->_deleteRequestRegistry ,
223+ "head " => $ plugin ->_headRequestRegistry ,
224+ "patch " => $ plugin ->_patchRequestRegistry ) as $ method => $ registry ) {
198225
199226 foreach ($ registry as $ uri => $ action ) {
200227 $ this ->$ method ($ uri , $ action );
@@ -204,6 +231,11 @@ public function use($plugin)
204231 }
205232 }
206233
234+ public function map (string $ baseUri , RouteMap $ routeMap )
235+ {
236+ $ this ->_mapRegistry [$ baseUri ] = $ routeMap ;
237+ }
238+
207239 /**
208240 * Register a request handled by this pipe.
209241 *
@@ -264,6 +296,30 @@ public function delete(string $uri, $action)
264296 $ this ->_deleteRequestRegistry [$ uri ] = $ action ;
265297 }
266298
299+ /**
300+ * Register a HEAD request handled by this pipe.
301+ *
302+ * @param string $uri The request URI.
303+ * @param callable $action The action to call when the request
304+ * correspond to the given URI.
305+ */
306+ public function head (string $ uri , $ action )
307+ {
308+ $ this ->_headRequestRegistry [$ uri ] = $ action ;
309+ }
310+
311+ /**
312+ * Register a PATCH request handled by this pipe.
313+ *
314+ * @param string $uri The request URI.
315+ * @param callable $action The action to call when the request
316+ * correspond to the given URI.
317+ */
318+ public function patch (string $ uri , $ action )
319+ {
320+ $ this ->_patchRequestRegistry [$ uri ] = $ action ;
321+ }
322+
267323 /**
268324 * Register a sub pipe managed by this pipe.
269325 *
@@ -346,6 +402,20 @@ private function _findSubPipe()
346402 return null ;
347403 }
348404
405+ /**
406+ * @return array|null
407+ */
408+ private function _findRouteMap ()
409+ {
410+ foreach ($ this ->_mapRegistry as $ baseUri => &$ map ) {
411+ if (preg_match ("#^ " . RequestUri::makeUri ($ this ->_baseUri , RequestUri::pattern2regex ($ baseUri )) . "# " , Request::capture ()->uri ->getUri ())) {
412+ return array (RequestUri::makeUri ($ this ->_baseUri , $ baseUri ), $ map );
413+ }
414+ }
415+
416+ return null ;
417+ }
418+
349419 /**
350420 * Executes the request.
351421 */
@@ -355,6 +425,13 @@ private function _executeRequest()
355425 // Execute middleware
356426 self ::triggerBeforeExecuteEvent (Request::capture ());
357427
428+ $ map = $ this ->_findRouteMap ();
429+
430+ if ($ map !== null ) {
431+ $ uri = str_replace ('/ ' . trim ($ map [0 ], "/ " ) . '/ ' , '' , '/ ' . ltrim (Request::capture ()->uri ->getUri (), "/ " ));
432+ $ map [1 ]->map ($ uri );
433+ }
434+
358435 $ registry = null ;
359436
360437 switch (Request::capture ()->getMethod ()) {
@@ -378,6 +455,14 @@ private function _executeRequest()
378455 case RequestMethod::DELETE :
379456 $ registry = $ this ->_deleteRequestRegistry ;
380457 break ;
458+
459+ case RequestMethod::HEAD :
460+ $ registry = $ this ->_headRequestRegistry ;
461+ break ;
462+
463+ case RequestMethod::PATCH :
464+ $ registry = $ this ->_patchRequestRegistry ;
465+ break ;
381466 }
382467
383468 if ($ registry === null ) {
0 commit comments