11<?php
2+
23/**
34 * @Package: Router - simple router class for php
45 * @Class : Router
@@ -396,7 +397,8 @@ public function controller(string $route, string $controller, array $options = [
396397 $ methodVar = strtolower (preg_replace ('%([a-z]|[0-9])([A-Z])% ' , '\1-\2 ' , $ methodVar ));
397398
398399 if ((!empty ($ only ) && !in_array ($ methodVar , $ only ))
399- || (!empty ($ except ) && in_array ($ methodVar , $ except ))) {
400+ || (!empty ($ except ) && in_array ($ methodVar , $ except ))
401+ ) {
400402 continue ;
401403 }
402404
@@ -591,8 +593,11 @@ protected function exception(string $message = '', int $statusCode = Response::H
591593 protected function routerCommand (): RouterCommand
592594 {
593595 return RouterCommand::getInstance (
594- $ this ->baseFolder , $ this ->paths , $ this ->namespaces ,
595- $ this ->request (), $ this ->response (),
596+ $ this ->baseFolder ,
597+ $ this ->paths ,
598+ $ this ->namespaces ,
599+ $ this ->request (),
600+ $ this ->response (),
596601 $ this ->getMiddlewares ()
597602 );
598603 }
@@ -646,6 +651,25 @@ protected function setPaths(array $params): void
646651 $ this ->cacheFile = $ params ['cache ' ] ?? realpath (__DIR__ . '/../cache.php ' );
647652 }
648653
654+ protected function controllerPaths ($ dir )
655+ {
656+ $ root = scandir ($ dir , 1 );
657+ foreach ($ root as $ value ) {
658+
659+ if ($ value === '. ' || $ value === '.. ' ) {
660+ continue ;
661+ }
662+ if (is_file ("$ dir/ $ value " )) {
663+ $ result [] = "$ dir/ $ value " ;
664+ continue ;
665+ }
666+ foreach ($ this ->controllerPaths ("$ dir/ $ value " ) as $ value ) {
667+ $ result [] = $ value ;
668+ }
669+ }
670+ return $ result ;
671+ }
672+
649673 /**
650674 * @param string $controller
651675 *
@@ -667,7 +691,17 @@ protected function resolveClassName(string $controller)
667691
668692 $ file = realpath ("{$ this ->paths ['controllers ' ]}/ {$ controller }.php " );
669693 if (!file_exists ($ file )) {
670- $ this ->exception ("{$ controller } class is not found! Please check the file. " );
694+ // Search for Controller Path
695+ $ controllerPaths = preg_grep (
696+ "/ $ controller/ " ,
697+ $ this ->controllerPaths ($ this ->paths ['controllers ' ])
698+ );
699+ $ controllerPath = $ controllerPaths ? array_values ($ controllerPaths )[0 ] : null ;
700+ $ file = $ controllerPath ? realpath ($ controllerPath ) : $ file ;
701+
702+ if (!file_exists ($ file )) {
703+ $ this ->exception ("{$ controller } class is not found! Please check the file. " );
704+ }
671705 }
672706
673707 $ controller = $ this ->namespaces ['controllers ' ] . str_replace ('/ ' , '\\' , $ controller );
@@ -725,7 +759,9 @@ protected function addRoute(string $uri, string $method, $callback, ?array $opti
725759 $ callback = is_array ($ callback ) ? implode ('@ ' , $ callback ) : $ callback ;
726760 $ routeName = is_string ($ callback )
727761 ? strtolower (preg_replace (
728- '/[^\w]/i ' , '. ' , str_replace ($ this ->namespaces ['controllers ' ], '' , $ callback )
762+ '/[^\w]/i ' ,
763+ '. ' ,
764+ str_replace ($ this ->namespaces ['controllers ' ], '' , $ callback )
729765 ))
730766 : null ;
731767 $ data = [
0 commit comments