divineniiquaye /
php-routers-benchmark
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of DivineNii opensource projects. |
||
| 7 | * |
||
| 8 | * PHP version 7.4 and above required |
||
| 9 | * |
||
| 10 | * @author Divine Niiquaye Ibok <[email protected]> |
||
| 11 | * @copyright 2019 DivineNii (https://divinenii.com/) |
||
| 12 | * @license https://opensource.org/licenses/BSD-3-Clause License |
||
| 13 | * |
||
| 14 | * For the full copyright and license information, please view the LICENSE |
||
| 15 | * file that was distributed with this source code. |
||
| 16 | */ |
||
| 17 | |||
| 18 | namespace App\BenchMark\Routers; |
||
| 19 | |||
| 20 | use App\BenchMark\AbstractRouter; |
||
| 21 | use Laminas\Diactoros\ResponseFactory; |
||
| 22 | use Laminas\Diactoros\ServerRequest; |
||
| 23 | use Laminas\Diactoros\UriFactory; |
||
| 24 | use Psr\Http\Message\ResponseFactoryInterface; |
||
| 25 | use Spiral\Core\Container; |
||
| 26 | use Spiral\Router\Exception\RouteNotFoundException; |
||
| 27 | use Spiral\Router\Route; |
||
| 28 | use Spiral\Router\Router; |
||
| 29 | use Spiral\Router\RouterInterface; |
||
| 30 | use Spiral\Router\UriHandler; |
||
| 31 | |||
| 32 | class SpiralRouter extends AbstractRouter |
||
| 33 | { |
||
| 34 | public const PATH = '<world>'; |
||
| 35 | |||
| 36 | protected RouterInterface $router; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | */ |
||
| 41 | public function testStatic(): bool |
||
| 42 | { |
||
| 43 | $methods = $this->generator->getMethods(); |
||
| 44 | |||
| 45 | foreach ($methods as $method) { |
||
| 46 | $path = ($this->strategy)($method); |
||
| 47 | |||
| 48 | try { |
||
| 49 | $this->router->handle(new ServerRequest([], [], $path, $method)); |
||
| 50 | } catch (RouteNotFoundException $e) { |
||
| 51 | return false; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | return true; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | public function testPath(): bool |
||
| 62 | { |
||
| 63 | $methods = $this->generator->getMethods(); |
||
| 64 | |||
| 65 | foreach ($methods as $method) { |
||
| 66 | $path = ($this->strategy)($method); |
||
| 67 | |||
| 68 | try { |
||
| 69 | $this->router->handle(new ServerRequest([], [], $path . 'spiral_router', $method)); |
||
| 70 | } catch (RouteNotFoundException $e) { |
||
| 71 | return false; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | return true; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | public function buildRoutes(array $routes): void |
||
| 82 | { |
||
| 83 | $container = new Container(); |
||
| 84 | $container->bind(ResponseFactoryInterface::class, new ResponseFactory()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 85 | |||
| 86 | $router = new Router('/', new UriHandler(new UriFactory()), $container); |
||
| 87 | |||
| 88 | foreach ($routes as $route) { |
||
| 89 | $spRoute = new Route($route['pattern'], fn () => 'Hello World'); |
||
| 90 | $spRoute->withVerbs(...$route['methods']); |
||
| 91 | |||
| 92 | $router->setRoute($route['name'], $spRoute); |
||
| 93 | } |
||
| 94 | |||
| 95 | $this->router = $router; |
||
| 96 | } |
||
| 97 | } |
||
| 98 |