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
458 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,110 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\FileRouter; | ||
|
||
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 57 in src/FileRouter.php GitHub Actions / psalm / PHP 8.1-ubuntu-latestPossiblyNullArgument
Check failure on line 57 in src/FileRouter.php GitHub Actions / psalm / PHP 8.2-ubuntu-latestPossiblyNullArgument
Check failure on line 57 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 | ||
{ | ||
return match ($request->getMethod()) { | ||
'HEAD', 'GET' => 'index', | ||
'POST' => 'create', | ||
'PUT' => 'update', | ||
'DELETE' => 'delete', | ||
default => throw new \Exception('Not implemented.'), | ||
}; | ||
} | ||
|
||
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]), | ||
$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; | ||
} | ||
|
||
return null; | ||
} | ||
} |
Empty file.
Oops, something went wrong.