From 0060585b901514273830b9b9a67da026d8cfcaa2 Mon Sep 17 00:00:00 2001 From: Dominik Zogg Date: Mon, 28 Feb 2022 17:22:25 +0100 Subject: [PATCH] php 8.0 min version --- .github/workflows/ci.yml | 11 -- LICENSE | 2 +- README.md | 8 +- composer.json | 20 +-- infection.json | 9 +- src/Router.php | 258 +++++++++++++++++++-------------------- 6 files changed, 147 insertions(+), 161 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 94932cd..ac2b26a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,17 +7,6 @@ on: - cron: '0 0 * * *' jobs: - php74: - name: PHP 7.4 - runs-on: ubuntu-20.04 - steps: - - name: checkout - uses: actions/checkout@v2 - - name: composer test - uses: docker://chubbyphp/ci-php74:latest - env: - COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} - STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }} php80: name: PHP 8.0 runs-on: ubuntu-20.04 diff --git a/LICENSE b/LICENSE index bb61236..7b2b327 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2021 Dominik Zogg +Copyright (c) 2022 Dominik Zogg Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 7caa381..d298de4 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,8 @@ Sunrise Router implementation for [chubbyphp-framework][1]. ## Requirements - * php: ^7.4|^8.0 - * [chubbyphp/chubbyphp-framework][1]: ^3.6.2|^4.0@dev + * php: ^8.0 + * [chubbyphp/chubbyphp-framework][1]: ^4.0 * [psr/http-message][2]: ^1.0.1 * [sunrise/http-router][3]: ^2.6 @@ -35,7 +35,7 @@ Sunrise Router implementation for [chubbyphp-framework][1]. Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-framework-router-sunrise][10]. ```bash -composer require chubbyphp/chubbyphp-framework-router-sunrise "^1.2" +composer require chubbyphp/chubbyphp-framework-router-sunrise "^1.3" ``` ## Usage @@ -81,7 +81,7 @@ $app->emit($app->handle((new ServerRequestFactory())->createFromGlobals())); ## Copyright -Dominik Zogg 2021 +Dominik Zogg 2022 [1]: https://packagist.org/packages/chubbyphp/chubbyphp-framework [2]: https://packagist.org/packages/psr/http-message diff --git a/composer.json b/composer.json index 1167d69..3602b90 100644 --- a/composer.json +++ b/composer.json @@ -14,8 +14,8 @@ } ], "require": { - "php": "^7.4|^8.0", - "chubbyphp/chubbyphp-framework": "^3.6.2|^4.0@dev", + "php": "^8.0", + "chubbyphp/chubbyphp-framework": "^4.0", "psr/http-message": "^1.0.1", "sunrise/http-router": "^2.6" }, @@ -25,14 +25,14 @@ "chubbyphp/chubbyphp-mock": "^1.6.1", "guzzlehttp/psr7": "^1.4.2", "http-interop/http-factory-guzzle": "^1.0", - "infection/infection": "^0.25.3", + "infection/infection": "^0.26.5", "laminas/laminas-diactoros": "^2.0", "nyholm/psr7": "^1.0", - "php-coveralls/php-coveralls": "^2.5.1", + "php-coveralls/php-coveralls": "^2.5.2", "phploc/phploc": "^7.0.2", "phpstan/extension-installer": "^1.1", - "phpstan/phpstan": "^1.1.2", - "phpunit/phpunit": "^9.5.10", + "phpstan/phpstan": "^1.4.6", + "phpunit/phpunit": "^9.5.16", "slim/psr7": "^0.5|^1.0", "sunrise/http-factory": "^1.0.4" }, @@ -43,11 +43,15 @@ "psr-4": { "Chubbyphp\\Tests\\Framework\\Router\\Sunrise\\": "tests/" } }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "phpstan/extension-installer": true, + "infection/extension-installer": true + } }, "extra": { "branch-alias": { - "dev-master": "1.2-dev" + "dev-master": "1.3-dev" } }, "scripts": { diff --git a/infection.json b/infection.json index 70dba91..96e37a6 100644 --- a/infection.json +++ b/infection.json @@ -6,9 +6,12 @@ }, "timeout": 10, "logs": { - "text": "build/phpinfection.log", - "badge": { - "branch": "master" + "text": "build/phpinfection/infection.log", + "html": "build/phpinfection/infection.html", + "json": "build/phpinfection/infection.json", + "summary": "build/phpinfection/summary.log", + "stryker": { + "report": "master" } }, "mutators": { diff --git a/src/Router.php b/src/Router.php index 1a0f51f..3a0db55 100644 --- a/src/Router.php +++ b/src/Router.php @@ -2,151 +2,141 @@ declare(strict_types=1); -namespace Chubbyphp\Framework\Router { - if (!interface_exists('RouteInterface')) { - interface RouterInterface extends RouteMatcherInterface, UrlGeneratorInterface - { - } +namespace Chubbyphp\Framework\Router\Sunrise; + +use Chubbyphp\Framework\Router\Exceptions\MethodNotAllowedException; +use Chubbyphp\Framework\Router\Exceptions\MissingRouteByNameException; +use Chubbyphp\Framework\Router\Exceptions\NotFoundException; +use Chubbyphp\Framework\Router\Exceptions\RouteGenerationException; +use Chubbyphp\Framework\Router\Route; +use Chubbyphp\Framework\Router\RouteInterface; +use Chubbyphp\Framework\Router\RouteMatcherInterface; +use Chubbyphp\Framework\Router\UrlGeneratorInterface; +use Psr\Http\Message\ServerRequestInterface; +use Sunrise\Http\Router\Exception\InvalidAttributeValueException as SunriseInvalidAttributeValueException; +use Sunrise\Http\Router\Exception\MethodNotAllowedException as SunriseMethodNotAllowedException; +use Sunrise\Http\Router\Exception\MissingAttributeValueException as SunriseMissingAttributeValueException; +use Sunrise\Http\Router\Exception\RouteNotFoundException as SunriseRouteNotFoundException; +use Sunrise\Http\Router\RouteFactory; +use Sunrise\Http\Router\Router as SunriseRouter; + +final class Router implements RouteMatcherInterface, UrlGeneratorInterface +{ + private SunriseRouter $router; + + /** + * @param array $routes + */ + public function __construct(array $routes, private string $basePath = '') + { + $this->router = $this->createRouter($routes); } -} -namespace Chubbyphp\Framework\Router\Sunrise { - use Chubbyphp\Framework\Router\Exceptions\MethodNotAllowedException; - use Chubbyphp\Framework\Router\Exceptions\MissingRouteByNameException; - use Chubbyphp\Framework\Router\Exceptions\NotFoundException; - use Chubbyphp\Framework\Router\Exceptions\RouteGenerationException; - use Chubbyphp\Framework\Router\Route; - use Chubbyphp\Framework\Router\RouteInterface; - use Chubbyphp\Framework\Router\RouterInterface; - use Psr\Http\Message\ServerRequestInterface; - use Sunrise\Http\Router\Exception\InvalidAttributeValueException as SunriseInvalidAttributeValueException; - use Sunrise\Http\Router\Exception\MethodNotAllowedException as SunriseMethodNotAllowedException; - use Sunrise\Http\Router\Exception\MissingAttributeValueException as SunriseMissingAttributeValueException; - use Sunrise\Http\Router\Exception\RouteNotFoundException as SunriseRouteNotFoundException; - use Sunrise\Http\Router\RouteFactory; - use Sunrise\Http\Router\Router as SunriseRouter; - - final class Router implements RouterInterface + public function match(ServerRequestInterface $request): RouteInterface { - private SunriseRouter $router; + try { + $sunriseRoute = $this->router->match($request); + + return Route::create( + $request->getMethod(), + $sunriseRoute->getPath(), + $sunriseRoute->getName(), + $sunriseRoute->getRequestHandler(), + $sunriseRoute->getMiddlewares() + )->withAttributes($sunriseRoute->getAttributes()); + } catch (SunriseRouteNotFoundException $exception) { + throw NotFoundException::create($request->getRequestTarget()); + } catch (SunriseMethodNotAllowedException $exception) { + throw MethodNotAllowedException::create( + $request->getRequestTarget(), + $request->getMethod(), + $exception->getAllowedMethods() + ); + } + } - private string $basePath; + /** + * @param array $attributes + * @param array $queryParams + */ + public function generateUrl( + ServerRequestInterface $request, + string $name, + array $attributes = [], + array $queryParams = [] + ): string { + $uri = $request->getUri(); + $requestTarget = $this->generatePath($name, $attributes, $queryParams); + + return $uri->getScheme().'://'.$uri->getAuthority().$requestTarget; + } - /** - * @param array $routes - */ - public function __construct(array $routes, string $basePath = '') - { - $this->router = $this->createRouter($routes); - $this->basePath = $basePath; + /** + * @param array $attributes + * @param array $queryParams + */ + public function generatePath(string $name, array $attributes = [], array $queryParams = []): string + { + try { + $path = $this->router->generateUri($name, $attributes, true); + } catch (SunriseRouteNotFoundException $exception) { + throw MissingRouteByNameException::create($name); + } catch (SunriseMissingAttributeValueException $exception) { + $match = $exception->fromContext('match'); + + $route = $this->router->getRoute($name); + + throw RouteGenerationException::create( + $name, + $route->getPath(), + $attributes, + new \RuntimeException(sprintf('Missing attribute "%s"', $match['name'])) + ); + } catch (SunriseInvalidAttributeValueException $exception) { + $match = $exception->fromContext('match'); + $value = $exception->fromContext('value'); + + $route = $this->router->getRoute($name); + + throw RouteGenerationException::create( + $name, + $route->getPath(), + $attributes, + new \RuntimeException(sprintf( + 'Not matching value "%s" with pattern "%s" on attribute "%s"', + $value, + $match['pattern'], + $match['name'] + )) + ); } - public function match(ServerRequestInterface $request): RouteInterface - { - try { - $sunriseRoute = $this->router->match($request); - - return Route::create( - $request->getMethod(), - $sunriseRoute->getPath(), - $sunriseRoute->getName(), - $sunriseRoute->getRequestHandler(), - $sunriseRoute->getMiddlewares() - )->withAttributes($sunriseRoute->getAttributes()); - } catch (SunriseRouteNotFoundException $exception) { - throw NotFoundException::create($request->getRequestTarget()); - } catch (SunriseMethodNotAllowedException $exception) { - throw MethodNotAllowedException::create( - $request->getRequestTarget(), - $request->getMethod(), - $exception->getAllowedMethods() - ); - } + if ([] === $queryParams) { + return $this->basePath.$path; } - /** - * @param array $attributes - * @param array $queryParams - */ - public function generateUrl( - ServerRequestInterface $request, - string $name, - array $attributes = [], - array $queryParams = [] - ): string { - $uri = $request->getUri(); - $requestTarget = $this->generatePath($name, $attributes, $queryParams); - - return $uri->getScheme().'://'.$uri->getAuthority().$requestTarget; - } + return $this->basePath.$path.'?'.http_build_query($queryParams); + } - /** - * @param array $attributes - * @param array $queryParams - */ - public function generatePath(string $name, array $attributes = [], array $queryParams = []): string - { - try { - $path = $this->router->generateUri($name, $attributes, true); - } catch (SunriseRouteNotFoundException $exception) { - throw MissingRouteByNameException::create($name); - } catch (SunriseMissingAttributeValueException $exception) { - $match = $exception->fromContext('match'); - - $route = $this->router->getRoute($name); - - throw RouteGenerationException::create( - $name, - $route->getPath(), - $attributes, - new \RuntimeException(sprintf('Missing attribute "%s"', $match['name'])) - ); - } catch (SunriseInvalidAttributeValueException $exception) { - $match = $exception->fromContext('match'); - $value = $exception->fromContext('value'); - - $route = $this->router->getRoute($name); - - throw RouteGenerationException::create( - $name, - $route->getPath(), - $attributes, - new \RuntimeException(sprintf( - 'Not matching value "%s" with pattern "%s" on attribute "%s"', - $value, - $match['pattern'], - $match['name'] - )) - ); - } - - if ([] === $queryParams) { - return $this->basePath.$path; - } - - return $this->basePath.$path.'?'.http_build_query($queryParams); + /** + * @param array $routes + */ + private function createRouter(array $routes): SunriseRouter + { + $routeFactory = new RouteFactory(); + $router = new SunriseRouter(); + + foreach ($routes as $route) { + $router->addRoute($routeFactory->createRoute( + $route->getName(), + $route->getPath(), + [$route->getMethod()], + $route->getRequestHandler(), + $route->getMiddlewares(), + $route->getAttributes() + )); } - /** - * @param array $routes - */ - private function createRouter(array $routes): SunriseRouter - { - $routeFactory = new RouteFactory(); - $router = new SunriseRouter(); - - foreach ($routes as $route) { - $router->addRoute($routeFactory->createRoute( - $route->getName(), - $route->getPath(), - [$route->getMethod()], - $route->getRequestHandler(), - $route->getMiddlewares(), - $route->getAttributes() - )); - } - - return $router; - } + return $router; } }