@@ -59,9 +59,10 @@ final class Router implements MiddlewareInterface, RequestHandlerInterface, Rout
5959 public function __construct (
6060 protected string $ basePath ,
6161 private ResponseFactoryInterface &StreamFactoryInterface $ factory ,
62+ NamespaceLookup |null $ routineLookup = null ,
6263 ) {
6364 $ this ->basePath = rtrim ($ basePath , '/ ' );
64- $ this ->routineLookup = new NamespaceLookup (
65+ $ this ->routineLookup = $ routineLookup ?? new NamespaceLookup (
6566 new Ucfirst (),
6667 Routinable::class,
6768 'Respect \\Rest \\Routines ' ,
@@ -81,18 +82,10 @@ public function always(string $routineName, mixed ...$params): static
8182 return $ this ;
8283 }
8384
84- public function withRoutineNamespace (string $ namespace ): static
85- {
86- $ this ->routineLookup = $ this ->routineLookup ->withNamespace ($ namespace );
87-
88- return $ this ;
89- }
90-
9185 public function appendRoute (AbstractRoute $ route ): static
9286 {
9387 $ this ->routes [] = $ route ;
94- $ route ->basePath = $ this ->basePath ;
95- $ route ->setRoutineLookup ($ this ->routineLookup );
88+ $ route ->setBasePath ($ this ->basePath );
9689
9790 foreach ($ this ->globalRoutines as $ routine ) {
9891 $ route ->appendRoutine ($ routine );
@@ -106,7 +99,6 @@ public function appendRoute(AbstractRoute $route): static
10699 public function appendHandler (AbstractRoute $ handler ): static
107100 {
108101 $ this ->handlers [] = $ handler ;
109- $ handler ->setRoutineLookup ($ this ->routineLookup );
110102
111103 foreach ($ this ->globalRoutines as $ routine ) {
112104 $ handler ->appendRoutine ($ routine );
@@ -122,7 +114,7 @@ public function callbackRoute(
122114 callable $ callback ,
123115 array $ arguments = [],
124116 ): Routes \Callback {
125- $ route = new Routes \Callback ($ method , $ path , $ callback , $ arguments );
117+ $ route = new Routes \Callback ($ this -> routineLookup , $ method , $ path , $ callback , $ arguments );
126118 $ this ->appendRoute ($ route );
127119
128120 return $ route ;
@@ -131,7 +123,7 @@ public function callbackRoute(
131123 /** @param array<int, mixed> $arguments */
132124 public function classRoute (string $ method , string $ path , string $ class , array $ arguments = []): Routes \ClassName
133125 {
134- $ route = new Routes \ClassName ($ method , $ path , $ class , $ arguments );
126+ $ route = new Routes \ClassName ($ this -> routineLookup , $ method , $ path , $ class , $ arguments );
135127 $ this ->appendRoute ($ route );
136128
137129 return $ route ;
@@ -151,10 +143,6 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
151143 {
152144 $ context = $ this ->dispatch ($ request );
153145
154- if ($ context ->route === null && !$ context ->hasPreparedResponse ()) {
155- return $ handler ->handle ($ request );
156- }
157-
158146 if ($ context ->route === null ) {
159147 $ response = $ context ->response ();
160148 if ($ response !== null && $ response ->getStatusCode () === 404 ) {
@@ -185,23 +173,23 @@ public function dispatchContext(DispatchContext $context): DispatchContext
185173
186174 public function onException (string $ className , callable $ callback ): Handlers \ExceptionHandler
187175 {
188- $ handler = new Handlers \ExceptionHandler ($ className , $ callback );
176+ $ handler = new Handlers \ExceptionHandler ($ this -> routineLookup , $ className , $ callback );
189177 $ this ->appendHandler ($ handler );
190178
191179 return $ handler ;
192180 }
193181
194182 public function onError (callable $ callback ): Handlers \ErrorHandler
195183 {
196- $ handler = new Handlers \ErrorHandler ($ callback );
184+ $ handler = new Handlers \ErrorHandler ($ this -> routineLookup , $ callback );
197185 $ this ->appendHandler ($ handler );
198186
199187 return $ handler ;
200188 }
201189
202190 public function onStatus (int |null $ statusCode , callable $ callback ): Handlers \StatusHandler
203191 {
204- $ handler = new Handlers \StatusHandler ($ statusCode , $ callback );
192+ $ handler = new Handlers \StatusHandler ($ this -> routineLookup , $ statusCode , $ callback );
205193 $ this ->appendHandler ($ handler );
206194
207195 return $ handler ;
@@ -215,23 +203,23 @@ public function getHandlers(): array
215203
216204 public function factoryRoute (string $ method , string $ path , string $ className , callable $ factory ): Routes \Factory
217205 {
218- $ route = new Routes \Factory ($ method , $ path , $ className , $ factory );
206+ $ route = new Routes \Factory ($ this -> routineLookup , $ method , $ path , $ className , $ factory );
219207 $ this ->appendRoute ($ route );
220208
221209 return $ route ;
222210 }
223211
224212 public function instanceRoute (string $ method , string $ path , object $ instance ): Routes \Instance
225213 {
226- $ route = new Routes \Instance ($ method , $ path , $ instance );
214+ $ route = new Routes \Instance ($ this -> routineLookup , $ method , $ path , $ instance );
227215 $ this ->appendRoute ($ route );
228216
229217 return $ route ;
230218 }
231219
232220 public function staticRoute (string $ method , string $ path , mixed $ staticValue ): Routes \StaticValue
233221 {
234- $ route = new Routes \StaticValue ($ method , $ path , $ staticValue );
222+ $ route = new Routes \StaticValue ($ this -> routineLookup , $ method , $ path , $ staticValue );
235223 $ this ->appendRoute ($ route );
236224
237225 return $ route ;
@@ -256,11 +244,6 @@ public function dispatchEngine(): DispatchEngine
256244 );
257245 }
258246
259- public static function compareOcurrences (string $ patternA , string $ patternB , string $ sub ): bool
260- {
261- return substr_count ($ patternA , $ sub ) < substr_count ($ patternB , $ sub );
262- }
263-
264247 protected function sortRoutesByComplexity (): void
265248 {
266249 usort (
@@ -273,7 +256,7 @@ static function (AbstractRoute $a, AbstractRoute $b): int {
273256 return 0 ;
274257 }
275258
276- $ slashCount = Router::compareOcurrences ($ pa , $ pb , '/ ' );
259+ $ slashCount = Router::compareOccurrences ($ pa , $ pb , '/ ' );
277260
278261 $ aCatchall = preg_match ('#/\*\*$# ' , $ pa );
279262 $ bCatchall = preg_match ('#/\*\*$# ' , $ pb );
@@ -285,7 +268,7 @@ static function (AbstractRoute $a, AbstractRoute $b): int {
285268 return $ slashCount ? 1 : -1 ;
286269 }
287270
288- if (Router::compareOcurrences ($ pa , $ pb , AbstractRoute::PARAM_IDENTIFIER )) {
271+ if (Router::compareOccurrences ($ pa , $ pb , AbstractRoute::PARAM_IDENTIFIER )) {
289272 return -1 ;
290273 }
291274
@@ -294,6 +277,11 @@ static function (AbstractRoute $a, AbstractRoute $b): int {
294277 );
295278 }
296279
280+ private static function compareOccurrences (string $ patternA , string $ patternB , string $ sub ): bool
281+ {
282+ return substr_count ($ patternA , $ sub ) < substr_count ($ patternB , $ sub );
283+ }
284+
297285 /** @param array<int, mixed> $args */
298286 public function __call (string $ method , array $ args ): AbstractRoute
299287 {
@@ -315,11 +303,7 @@ public function __call(string $method, array $args): AbstractRoute
315303 }
316304
317305 if (is_callable ($ routeTarget )) {
318- if (!isset ($ args [2 ])) {
319- return $ this ->callbackRoute ($ method , $ path , $ routeTarget );
320- }
321-
322- return $ this ->callbackRoute ($ method , $ path , $ routeTarget , $ args [2 ]);
306+ return $ this ->callbackRoute ($ method , $ path , $ routeTarget , $ args [2 ] ?? []);
323307 }
324308
325309 if ($ routeTarget instanceof Routable) {
0 commit comments