Skip to content

Commit b73e069

Browse files
committed
Fixed Controller inside subfolder path
1 parent 57ebcfa commit b73e069

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

src/Router.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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 = [

src/RouterCommand.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ public function __construct(
5555
Request $request,
5656
Response $response,
5757
array $middlewares
58-
)
59-
{
58+
) {
6059
$this->baseFolder = $baseFolder;
6160
$this->paths = $paths;
6261
$this->namespaces = $namespaces;
@@ -68,7 +67,6 @@ public function __construct(
6867
foreach ($this->middlewares['middlewares'] as $middleware) {
6968
$this->beforeAfter($middleware);
7069
}
71-
7270
}
7371

7472
/**
@@ -110,12 +108,15 @@ public static function getInstance(
110108
Request $request,
111109
Response $response,
112110
array $middlewares
113-
): ?RouterCommand
114-
{
111+
): ?RouterCommand {
115112
if (null === self::$instance) {
116113
self::$instance = new static(
117-
$baseFolder, $paths, $namespaces,
118-
$request, $response, $middlewares
114+
$baseFolder,
115+
$paths,
116+
$namespaces,
117+
$request,
118+
$response,
119+
$middlewares
119120
);
120121
}
121122

@@ -214,6 +215,26 @@ public function runRoute(string|Closure $command, array $params = []): mixed
214215
return $this->runMethodWithParams($command, $params);
215216
}
216217

218+
219+
protected function controllerPaths($dir)
220+
{
221+
$root = scandir($dir, 1);
222+
foreach ($root as $value) {
223+
224+
if ($value === '.' || $value === '..') {
225+
continue;
226+
}
227+
if (is_file("$dir/$value")) {
228+
$result[] = "$dir/$value";
229+
continue;
230+
}
231+
foreach ($this->controllerPaths("$dir/$value") as $value) {
232+
$result[] = $value;
233+
}
234+
}
235+
return $result;
236+
}
237+
217238
/**
218239
* Resolve Controller or Middleware class.
219240
*
@@ -229,7 +250,17 @@ protected function resolveClass(string $class, string $path, string $namespace):
229250
$class = str_replace([$namespace, '\\'], ['', '/'], $class);
230251
$file = realpath("{$path}/{$class}.php");
231252
if (!file_exists($file)) {
232-
$this->exception("{$class} class is not found. Please check the file.");
253+
// Search for Controller Path
254+
$controllerPaths = preg_grep(
255+
"/$class/",
256+
$this->controllerPaths($path)
257+
);
258+
$controllerPath = $controllerPaths ? array_values($controllerPaths)[0] : null;
259+
$file = $controllerPath ? realpath($controllerPath) : $file;
260+
261+
if (!file_exists($file)) {
262+
$this->exception("{$class} class is not found. Please check the file.");
263+
}
233264
}
234265

235266
$class = $namespace . str_replace('/', '\\', $class);

0 commit comments

Comments
 (0)