diff --git a/src/Router.php b/src/Router.php index fedd660..ee85998 100644 --- a/src/Router.php +++ b/src/Router.php @@ -1,4 +1,5 @@ baseFolder, $this->paths, $this->namespaces, - $this->request(), $this->response(), + $this->baseFolder, + $this->paths, + $this->namespaces, + $this->request(), + $this->response(), $this->getMiddlewares() ); } @@ -646,6 +651,25 @@ protected function setPaths(array $params): void $this->cacheFile = $params['cache'] ?? realpath(__DIR__ . '/../cache.php'); } + protected function controllerPaths($dir) + { + $root = scandir($dir, 1); + foreach ($root as $value) { + + if ($value === '.' || $value === '..') { + continue; + } + if (is_file("$dir/$value")) { + $result[] = "$dir/$value"; + continue; + } + foreach ($this->controllerPaths("$dir/$value") as $value) { + $result[] = $value; + } + } + return $result; + } + /** * @param string $controller * @@ -667,7 +691,17 @@ protected function resolveClassName(string $controller) $file = realpath("{$this->paths['controllers']}/{$controller}.php"); if (!file_exists($file)) { - $this->exception("{$controller} class is not found! Please check the file."); + // Search for Controller Path + $controllerPaths = preg_grep( + "/$controller/", + $this->controllerPaths($this->paths['controllers']) + ); + $controllerPath = $controllerPaths ? array_values($controllerPaths)[0] : null; + $file = $controllerPath ? realpath($controllerPath) : $file; + + if (!file_exists($file)) { + $this->exception("{$controller} class is not found! Please check the file."); + } } $controller = $this->namespaces['controllers'] . str_replace('/', '\\', $controller); @@ -725,7 +759,9 @@ protected function addRoute(string $uri, string $method, $callback, ?array $opti $callback = is_array($callback) ? implode('@', $callback) : $callback; $routeName = is_string($callback) ? strtolower(preg_replace( - '/[^\w]/i', '.', str_replace($this->namespaces['controllers'], '', $callback) + '/[^\w]/i', + '.', + str_replace($this->namespaces['controllers'], '', $callback) )) : null; $data = [ diff --git a/src/RouterCommand.php b/src/RouterCommand.php index 82faf18..daa584e 100644 --- a/src/RouterCommand.php +++ b/src/RouterCommand.php @@ -55,8 +55,7 @@ public function __construct( Request $request, Response $response, array $middlewares - ) - { + ) { $this->baseFolder = $baseFolder; $this->paths = $paths; $this->namespaces = $namespaces; @@ -68,7 +67,6 @@ public function __construct( foreach ($this->middlewares['middlewares'] as $middleware) { $this->beforeAfter($middleware); } - } /** @@ -110,12 +108,15 @@ public static function getInstance( Request $request, Response $response, array $middlewares - ): ?RouterCommand - { + ): ?RouterCommand { if (null === self::$instance) { self::$instance = new static( - $baseFolder, $paths, $namespaces, - $request, $response, $middlewares + $baseFolder, + $paths, + $namespaces, + $request, + $response, + $middlewares ); } @@ -214,6 +215,26 @@ public function runRoute(string|Closure $command, array $params = []): mixed return $this->runMethodWithParams($command, $params); } + + protected function controllerPaths($dir) + { + $root = scandir($dir, 1); + foreach ($root as $value) { + + if ($value === '.' || $value === '..') { + continue; + } + if (is_file("$dir/$value")) { + $result[] = "$dir/$value"; + continue; + } + foreach ($this->controllerPaths("$dir/$value") as $value) { + $result[] = $value; + } + } + return $result; + } + /** * Resolve Controller or Middleware class. * @@ -229,7 +250,17 @@ protected function resolveClass(string $class, string $path, string $namespace): $class = str_replace([$namespace, '\\'], ['', '/'], $class); $file = realpath("{$path}/{$class}.php"); if (!file_exists($file)) { - $this->exception("{$class} class is not found. Please check the file."); + // Search for Controller Path + $controllerPaths = preg_grep( + "/$class/", + $this->controllerPaths($path) + ); + $controllerPath = $controllerPaths ? array_values($controllerPaths)[0] : null; + $file = $controllerPath ? realpath($controllerPath) : $file; + + if (!file_exists($file)) { + $this->exception("{$class} class is not found. Please check the file."); + } } $class = $namespace . str_replace('/', '\\', $class);