Skip to content

Commit

Permalink
Implement router
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Dec 27, 2023
1 parent 6a899f9 commit 5893cbb
Show file tree
Hide file tree
Showing 13 changed files with 484 additions and 21 deletions.
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%2FFile 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.
136 changes: 136 additions & 0 deletions src/FileRouter.php
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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

PossiblyNullArgument

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

Check failure on line 58 in src/FileRouter.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

PossiblyNullArgument

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

Check failure on line 58 in src/FileRouter.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

PossiblyNullArgument

src/FileRouter.php:58: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
{
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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InternalClass

src/FileRouter.php:87:23: InternalClass: PHPUnit\Logging\Exception is internal to PHPUnit but called from Yiisoft\FileRouter\FileRouter (see https://psalm.dev/174)

Check failure on line 87 in src/FileRouter.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InternalClass

src/FileRouter.php:87:23: InternalClass: PHPUnit\Logging\Exception is internal to PHPUnit but called from Yiisoft\FileRouter\FileRouter (see https://psalm.dev/174)

Check failure on line 87 in src/FileRouter.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.3-ubuntu-latest

InternalClass

src/FileRouter.php:87:23: InternalClass: PHPUnit\Logging\Exception is internal to PHPUnit but called from Yiisoft\FileRouter\FileRouter (see https://psalm.dev/174)
}
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 removed tests/.gitkeep
Empty file.
Loading

0 comments on commit 5893cbb

Please sign in to comment.