Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Controller inside subfolder path #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions src/Router.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @Package: Router - simple router class for php
* @Class : Router
Expand Down Expand Up @@ -396,7 +397,8 @@ public function controller(string $route, string $controller, array $options = [
$methodVar = strtolower(preg_replace('%([a-z]|[0-9])([A-Z])%', '\1-\2', $methodVar));

if ((!empty($only) && !in_array($methodVar, $only))
|| (!empty($except) && in_array($methodVar, $except))) {
|| (!empty($except) && in_array($methodVar, $except))
) {
continue;
}

Expand Down Expand Up @@ -591,8 +593,11 @@ protected function exception(string $message = '', int $statusCode = Response::H
protected function routerCommand(): RouterCommand
{
return RouterCommand::getInstance(
$this->baseFolder, $this->paths, $this->namespaces,
$this->request(), $this->response(),
$this->baseFolder,
$this->paths,
$this->namespaces,
$this->request(),
$this->response(),
$this->getMiddlewares()
);
}
Expand Down Expand Up @@ -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
*
Expand All @@ -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);
Expand Down Expand Up @@ -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 = [
Expand Down
47 changes: 39 additions & 8 deletions src/RouterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public function __construct(
Request $request,
Response $response,
array $middlewares
)
{
) {
$this->baseFolder = $baseFolder;
$this->paths = $paths;
$this->namespaces = $namespaces;
Expand All @@ -68,7 +67,6 @@ public function __construct(
foreach ($this->middlewares['middlewares'] as $middleware) {
$this->beforeAfter($middleware);
}

}

/**
Expand Down Expand Up @@ -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
);
}

Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
Expand Down
Loading