1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace WShafer\Expressive\Symfony\Router; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
8
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException; |
9
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
10
|
|
|
use Symfony\Component\Routing\Generator\UrlGenerator; |
11
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
12
|
|
|
use Symfony\Component\Routing\RouteCollection; |
13
|
|
|
use Symfony\Component\Routing\Route as SymfonyRoute; |
14
|
|
|
use WShafer\Expressive\Symfony\Router\Cache\Cache; |
15
|
|
|
use Zend\Expressive\Router\Route; |
16
|
|
|
use Zend\Expressive\Router\RouteResult; |
17
|
|
|
use Zend\Expressive\Router\RouterInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @SuppressWarnings(PHPMD.LongVariable) |
21
|
|
|
*/ |
22
|
|
|
class SymfonyRouteRouter implements RouterInterface |
23
|
|
|
{ |
24
|
|
|
/** @var RouteCollection */ |
25
|
|
|
protected $collection; |
26
|
|
|
|
27
|
|
|
/** @var UrlMatcher */ |
28
|
|
|
protected $urlMatcher; |
29
|
|
|
|
30
|
|
|
/** @var UrlGenerator */ |
31
|
|
|
protected $generator; |
32
|
|
|
|
33
|
|
|
protected $cache; |
34
|
|
|
|
35
|
|
|
protected $routes = []; |
36
|
6 |
|
|
37
|
|
|
public function __construct( |
38
|
|
|
RouteCollection $collection, |
39
|
|
|
UrlMatcher $urlMatcher, |
40
|
|
|
UrlGenerator $generator, |
41
|
|
|
Cache $cache |
42
|
6 |
|
) { |
43
|
6 |
|
$this->collection = $collection; |
44
|
6 |
|
$this->urlMatcher = $urlMatcher; |
45
|
6 |
|
$this->generator = $generator; |
46
|
|
|
$this->cache = $cache; |
47
|
6 |
|
|
48
|
6 |
|
$this->cache->populateCollectionFromCache($collection); |
49
|
|
|
} |
50
|
1 |
|
|
51
|
|
|
public function addRoute(Route $route): void |
52
|
1 |
|
{ |
53
|
1 |
|
$path = $route->getPath(); |
54
|
|
|
$name = $route->getName(); |
55
|
1 |
|
|
56
|
|
|
$this->routes[$name] = $route; |
57
|
|
|
|
58
|
|
|
if ($this->cache->has($name)) { |
59
|
1 |
|
return; |
60
|
1 |
|
} |
61
|
1 |
|
|
62
|
1 |
|
$symfonyRoute = new SymfonyRoute( |
63
|
1 |
|
$path, |
64
|
1 |
|
['route' => $name], |
65
|
1 |
|
[], |
66
|
1 |
|
$route->getOptions(), |
67
|
1 |
|
null, |
68
|
|
|
[], |
69
|
|
|
$route->getAllowedMethods(), |
|
|
|
|
70
|
1 |
|
null |
71
|
1 |
|
); |
72
|
1 |
|
|
73
|
|
|
$this->cache->add($name, $symfonyRoute); |
74
|
|
|
$this->collection->add($name, $symfonyRoute); |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
/** |
78
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
79
|
3 |
|
*/ |
80
|
|
|
public function match(Request $request): RouteResult |
81
|
|
|
{ |
82
|
3 |
|
$this->cache->writeCache(); |
83
|
2 |
|
|
84
|
1 |
|
try { |
85
|
1 |
|
$match = $this->urlMatcher->match($request->getUri()->getPath()); |
86
|
1 |
|
} catch (MethodNotAllowedException $e) { |
87
|
|
|
return RouteResult::fromRouteFailure($e->getAllowedMethods()); |
88
|
|
|
} catch (ResourceNotFoundException $e) { |
89
|
1 |
|
return RouteResult::fromRouteFailure(null); |
90
|
1 |
|
} |
91
|
1 |
|
|
92
|
|
|
$route = $match['route']; |
93
|
|
|
|
94
|
|
|
if (!$this->routes[$route]) { |
95
|
|
|
$this->cache->invalidateCacheFile(); |
96
|
|
|
return RouteResult::fromRouteFailure(null); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
1 |
|
unset($match['route'], $match['_route']); |
100
|
|
|
return RouteResult::fromRoute($this->routes[$route], $match); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
105
|
|
|
*/ |
106
|
|
|
public function generateUri(string $name, array $substitutions = [], array $options = []): string |
107
|
|
|
{ |
108
|
|
|
return $this->generator->generate($name, $substitutions); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.