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

Implement router #1

Merged
merged 7 commits into from
Dec 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ phpunit.phar
/phpunit.xml
# phpunit cache
.phpunit.result.cache
/phpunit.cache
.phpunit.cache
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
<a href="https://github.com/yiisoft" target="_blank">
<img src="https://yiisoft.github.io/docs/images/yii_logo.svg" height="100px">
</a>
<h1 align="center">Yii _____</h1>
<h1 align="center">File Router</h1>
<br>
</p>

[![Latest Stable Version](https://poser.pugx.org/yiisoft/_____/v/stable.png)](https://packagist.org/packages/yiisoft/_____)
[![Total Downloads](https://poser.pugx.org/yiisoft/_____/downloads.png)](https://packagist.org/packages/yiisoft/_____)
[![Build status](https://github.com/yiisoft/_____/workflows/build/badge.svg)](https://github.com/yiisoft/_____/actions?query=workflow%3Abuild)
[![Code Coverage](https://codecov.io/gh/yiisoft/_____/branch/master/graph/badge.svg)](https://codecov.io/gh/yiisoft/_____)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2F_____%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/_____/master)
[![static analysis](https://github.com/yiisoft/_____/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/_____/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/_____/coverage.svg)](https://shepherd.dev/github/yiisoft/_____)
[![psalm-level](https://shepherd.dev/github/yiisoft/_____/level.svg)](https://shepherd.dev/github/yiisoft/_____)
[![Latest Stable Version](https://poser.pugx.org/yiisoft/file-router/v/stable.png)](https://packagist.org/packages/yiisoft/file-router)
[![Total Downloads](https://poser.pugx.org/yiisoft/file-router/downloads.png)](https://packagist.org/packages/yiisoft/file-router)
[![Build status](https://github.com/yiisoft/file-router/workflows/build/badge.svg)](https://github.com/yiisoft/file-router/actions?query=workflow%3Abuild)
[![Code Coverage](https://codecov.io/gh/yiisoft/file-router/branch/master/graph/badge.svg)](https://codecov.io/gh/yiisoft/file-router)
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%fFFile-router%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/file-router/master)
[![static analysis](https://github.com/yiisoft/file-router/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/file-router/actions?query=workflow%3A%22static+analysis%22)
[![type-coverage](https://shepherd.dev/github/yiisoft/file-router/coverage.svg)](https://shepherd.dev/github/yiisoft/file-router)
[![psalm-level](https://shepherd.dev/github/yiisoft/file-router/level.svg)](https://shepherd.dev/github/yiisoft/file-router)

The package ...

Expand All @@ -26,7 +26,7 @@ The package ...
The package could be installed with composer:

```shell
composer require yiisoft/_____
composer require yiisoft/file-router
```

## General usage
Expand Down Expand Up @@ -74,7 +74,7 @@ Use [ComposerRequireChecker](https://github.com/maglnet/ComposerRequireChecker)

## License

The Yii _____ is free software. It is released under the terms of the BSD License.
The File Router is free software. It is released under the terms of the BSD License.
Please see [`LICENSE`](./LICENSE.md) for more information.

Maintained by [Yii Software](https://www.yiiframework.com/).
Expand Down
29 changes: 20 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
{
"name": "yiisoft/_____",
"name": "yiisoft/file-router",
"type": "library",
"description": "_____",
"description": "File based router",
"keywords": [
"_____"
"file-router",
"router",
"routing",
"convention-router",
"structure-router"
],
"homepage": "https://www.yiiframework.com/",
"license": "BSD-3-Clause",
"support": {
"issues": "https://github.com/yiisoft/_____/issues?state=open",
"issues": "https://github.com/yiisoft/file-router/issues?state=open",
"forum": "https://www.yiiframework.com/forum/",
"wiki": "https://www.yiiframework.com/wiki/",
"irc": "ircs://irc.libera.chat:6697/yii",
"chat": "https://t.me/yii3en",
"source": "https://github.com/yiisoft/_____"
"source": "https://github.com/yiisoft/file-router"
},
"funding": [
{
Expand All @@ -28,24 +32,31 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^8.1"
"php": "^8.1",
"psr/http-message": "^2.0",
"psr/http-server-middleware": "^1.0",
"yiisoft/middleware-dispatcher": "^5.2"
},
"require-dev": {
"httpsoft/http-message": "^1.1",
"httpsoft/http-response": "^1.1",
"maglnet/composer-require-checker": "^4.7",
"phpunit/phpunit": "^10.5",
"rector/rector": "^0.18.11",
"roave/infection-static-analysis-plugin": "^1.34",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.16"
"vimeo/psalm": "^5.16",
"yiisoft/strings": "^2.4",
"yiisoft/test-support": "^3.0"
},
"autoload": {
"psr-4": {
"Yiisoft\\_____\\": "src"
"Yiisoft\\FileRouter\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Yiisoft\\_____\\Tests\\": "tests"
"Yiisoft\\FileRouter\\Tests\\": "tests"
}
},
"config": {
Expand Down
21 changes: 21 additions & 0 deletions config/web.php
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 removed src/.gitkeep
Empty file.
110 changes: 110 additions & 0 deletions src/FileRouter.php
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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

PossiblyNullArgument

src/FileRouter.php:57:46: PossiblyNullArgument: Argument 2 of method_exists cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 57 in src/FileRouter.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

PossiblyNullArgument

src/FileRouter.php:57:46: PossiblyNullArgument: Argument 2 of method_exists cannot be null, possibly null value provided (see https://psalm.dev/078)

Check failure on line 57 in src/FileRouter.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

PossiblyNullArgument

src/FileRouter.php:57:46: PossiblyNullArgument: Argument 2 of method_exists cannot be null, possibly null value provided (see https://psalm.dev/078)
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 removed tests/.gitkeep
Empty file.
Loading
Loading