Skip to content

Commit

Permalink
php 8.0 min version
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Feb 28, 2022
1 parent c30ca04 commit 0060585
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 161 deletions.
11 changes: 0 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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"
},
Expand All @@ -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": {
Expand Down
9 changes: 6 additions & 3 deletions infection.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
258 changes: 124 additions & 134 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, RouteInterface> $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<string, string> $attributes
* @param array<string, mixed> $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<int, RouteInterface> $routes
*/
public function __construct(array $routes, string $basePath = '')
{
$this->router = $this->createRouter($routes);
$this->basePath = $basePath;
/**
* @param array<string, string> $attributes
* @param array<string, mixed> $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<string, string> $attributes
* @param array<string, mixed> $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<string, string> $attributes
* @param array<string, mixed> $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<RouteInterface> $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<RouteInterface> $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;
}
}

0 comments on commit 0060585

Please sign in to comment.