3737use ElementaryFramework \WaterPipe \HTTP \Request \RequestUri ;
3838use ElementaryFramework \WaterPipe \HTTP \Response \Response ;
3939use ElementaryFramework \WaterPipe \Routing \Middleware \Middleware ;
40+ use ElementaryFramework \WaterPipe \Routing \Route ;
4041use ElementaryFramework \WaterPipe \Routing \RouteAction ;
4142
4243class WaterPipe
@@ -76,9 +77,15 @@ public function __construct()
7677 $ this ->_errorsRegistry = array ();
7778 }
7879
79- public function use (Middleware $ middleware )
80+ public function use ($ plugin )
8081 {
81- array_push ($ this ->_middlewareRegistry , $ middleware );
82+ if ($ plugin instanceof Middleware) {
83+ array_push ($ this ->_middlewareRegistry , $ plugin );
84+ } elseif ($ plugin instanceof Route) {
85+ foreach (array ("request " , "get " , "post " , "put " , "delete " ) as $ method ) {
86+ call_user_func_array (array ($ this , $ method ), array ($ plugin ->getUri (), array ($ plugin , $ method )));
87+ }
88+ }
8289 }
8390
8491 public function request (string $ uri , $ action )
@@ -116,33 +123,17 @@ public function delete(string $uri, $action)
116123 * @throws \Exception
117124 */
118125 public function run ()
119- {
120- $ this ->exec (Request::capture ());
121- }
122-
123- /**
124- * Execute a request with the current water pipe.
125- *
126- * This method have to be called AFTER the
127- * definition of all routes. No routes will
128- * be considered after the call of this method.
129- *
130- * @param Request $request The request to execute.
131- *
132- * @throws \Exception
133- */
134- public function exec (Request $ request )
135126 {
136127 $ this ->_isRunning = true ;
137128
138- $ this ->_executeRequest ($ request );
129+ $ this ->_executeRequest ();
139130 }
140131
141- private function _executeRequest (Request $ request )
132+ private function _executeRequest ()
142133 {
143134 $ registry = null ;
144135
145- switch ($ request ->getMethod ()) {
136+ switch (Request:: getInstance () ->getMethod ()) {
146137 case RequestMethod::UNKNOWN :
147138 // TODO: 500 Internal Server Error. Unable to determine the request method.
148139 throw new \Exception ("500 Error " );
@@ -168,10 +159,10 @@ private function _executeRequest(Request $request)
168159 throw new \Exception ("Cannot handle a request of this type " );
169160 }
170161
171- $ runner = $ this ->_getActionForRoutes ($ registry, $ request );
162+ $ runner = $ this ->_getActionForRoutes ($ registry );
172163
173164 if ($ runner === null ) {
174- $ runner = $ this ->_getActionForRoutes ($ this ->_requestRegistry , $ request );
165+ $ runner = $ this ->_getActionForRoutes ($ this ->_requestRegistry );
175166 }
176167
177168 if ($ runner === null ) {
@@ -181,24 +172,29 @@ private function _executeRequest(Request $request)
181172
182173 // NOTE: No code will normally not be executed after this block...
183174 if (is_callable ($ runner ) || is_array ($ runner )) {
184- call_user_func_array ($ runner , array ($ request , new Response ()));
175+ call_user_func_array ($ runner , array (Request:: getInstance () , new Response ()));
185176 } elseif (is_subclass_of ($ runner , RouteAction::class)) {
186177 if (is_string ($ runner )) {
187- $ runner = new $ runner( $ request ) ;
178+ $ runner = new $ runner ;
188179 }
189180 $ runner ->execute ();
190181 } else {
191182 throw new \Exception ("Malformed route action " );
192183 }
193184 }
194185
195- private function _getActionForRoutes (array $ routes , Request $ request )
186+ /**
187+ * @param array $routes
188+ * @return callable
189+ * @throws Exceptions\RequestUriBuilderException
190+ */
191+ private function _getActionForRoutes (array $ routes )
196192 {
197193 $ runner = null ;
198194
199195 foreach ($ routes as $ pattern => $ action ) {
200- if (RequestUri::isMatch ($ pattern , $ request ->uri ->getUri ())) {
201- $ request ->uri ->setPattern ($ pattern )->build ();
196+ if (RequestUri::isMatch ($ pattern , Request:: getInstance () ->uri ->getUri ())) {
197+ Request:: getInstance () ->uri ->setPattern ($ pattern )->build ();
202198 $ runner = $ action ;
203199 break ;
204200 }
0 commit comments