generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
484 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,4 @@ phpunit.phar | |
/phpunit.xml | ||
# phpunit cache | ||
.phpunit.result.cache | ||
/phpunit.cache | ||
.phpunit.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Yiisoft\FileRouter\FileRouter; | ||
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher; | ||
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory; | ||
|
||
return [ | ||
FileRouter::class => function (ContainerInterface $container) { | ||
$eventDispatcher = $container->has(EventDispatcherInterface::class) | ||
? $container->get(EventDispatcherInterface::class) | ||
: null; | ||
|
||
$middlewareFactory = $container->get(MiddlewareFactory::class); | ||
|
||
return new FileRouter(new MiddlewareDispatcher($middlewareFactory, $eventDispatcher)); | ||
}, | ||
]; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\FileRouter; | ||
|
||
use PHPUnit\Logging\Exception; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher; | ||
use Yiisoft\Strings\StringHelper; | ||
|
||
final class FileRouter implements MiddlewareInterface | ||
{ | ||
public string $baseControllerDirectory = 'Controller'; | ||
public string $classPostfix = 'Controller'; | ||
public string $namespace = 'App'; | ||
|
||
public function __construct( | ||
private readonly MiddlewareDispatcher $middlewareDispatcher, | ||
) { | ||
} | ||
|
||
public function withBaseControllerDirectory(string $directory): self | ||
{ | ||
$new = clone $this; | ||
$new->baseControllerDirectory = $directory; | ||
|
||
return $new; | ||
} | ||
|
||
public function withClassPostfix(string $postfix): self | ||
{ | ||
$new = clone $this; | ||
$new->classPostfix = $postfix; | ||
|
||
return $new; | ||
} | ||
|
||
public function withNamespace(string $namespace): self | ||
{ | ||
$new = clone $this; | ||
$new->namespace = $namespace; | ||
|
||
return $new; | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$controllerClass = $this->parseController($request); | ||
if ($controllerClass === null) { | ||
return $handler->handle($request); | ||
} | ||
$action = $this->parseAction($request); | ||
|
||
if (!method_exists($controllerClass, $action)) { | ||
Check failure on line 58 in src/FileRouter.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestPossiblyNullArgument
Check failure on line 58 in src/FileRouter.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestPossiblyNullArgument
Check failure on line 58 in src/FileRouter.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestPossiblyNullArgument
|
||
return $handler->handle($request); | ||
} | ||
|
||
$middlewares = $controllerClass::$middlewares[$action] ?? []; | ||
$middlewares[] = [$controllerClass, $action]; | ||
|
||
$middlewareDispatcher = $this->middlewareDispatcher->withMiddlewares($middlewares); | ||
|
||
return $middlewareDispatcher->dispatch($request, $handler); | ||
} | ||
|
||
private function parseAction(ServerRequestInterface $request): ?string | ||
{ | ||
switch ($request->getMethod()) { | ||
case 'HEAD': | ||
case 'GET': | ||
$action = 'index'; | ||
break; | ||
case 'POST': | ||
$action = 'create'; | ||
break; | ||
case 'PUT': | ||
$action = 'update'; | ||
break; | ||
case 'DELETE': | ||
$action = 'delete'; | ||
break; | ||
default: | ||
throw new Exception('Not implemented.'); | ||
Check failure on line 87 in src/FileRouter.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestInternalClass
Check failure on line 87 in src/FileRouter.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestInternalClass
Check failure on line 87 in src/FileRouter.php GitHub Actions / psalm / PHP 8.3-ubuntu-latestInternalClass
|
||
} | ||
return $action; | ||
} | ||
|
||
private function parseController(ServerRequestInterface $request): mixed | ||
{ | ||
$path = $request->getUri()->getPath(); | ||
if ($path === '/') { | ||
$controllerName = 'Index'; | ||
$directoryPath = ''; | ||
} else { | ||
$controllerName = preg_replace_callback( | ||
'#(/.)#', | ||
fn(array $matches) => strtoupper($matches[1]), | ||
str_replace('/', DIRECTORY_SEPARATOR, $path) | ||
); | ||
$directoryPath = StringHelper::directoryName($controllerName); | ||
|
||
$controllerName = StringHelper::basename($controllerName); | ||
} | ||
|
||
$controller = $controllerName . $this->classPostfix; | ||
$className = str_replace( | ||
['/', '\\\\'], | ||
['\\', '\\'], | ||
$this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $directoryPath . '\\' . $controller | ||
); | ||
|
||
if (class_exists($className)) { | ||
return $className; | ||
} | ||
|
||
// alternative version finding namespace by file | ||
|
||
|
||
//$controllerDirectory = $this->aliases->get('src') . DIRECTORY_SEPARATOR . $this->baseControllerDirectory; | ||
//$classPath = $controllerDirectory . DIRECTORY_SEPARATOR . $directoryPath . DIRECTORY_SEPARATOR . $controller; | ||
//$filename = $classPath . '.php'; | ||
//if (file_exists($filename)) { | ||
// $content = file_get_contents($filename); | ||
// $namespace = preg_match('#namespace\s+(.+?);#', $content, $matches) ? $matches[1] : ''; | ||
// if (class_exists($namespace . '\\' . $controller)) { | ||
// return $namespace . '\\' . $controller; | ||
// } | ||
//} | ||
|
||
return null; | ||
} | ||
} |
Empty file.
Oops, something went wrong.