|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mikemirten\Bundle\JsonApiBundle\Routing; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\Routing\ |
|
7
|
|
|
{ |
|
8
|
|
|
Generator\UrlGeneratorInterface, |
|
9
|
|
|
Generator\UrlGenerator, |
|
10
|
|
|
RouteCollection, |
|
11
|
|
|
RequestContext, |
|
12
|
|
|
Route |
|
13
|
|
|
}; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Repository of routes |
|
17
|
|
|
* |
|
18
|
|
|
* @package Springfield\Component\HttpClient |
|
19
|
|
|
*/ |
|
20
|
|
|
class RouteRepository |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Prepared URL generator |
|
24
|
|
|
* |
|
25
|
|
|
* @var UrlGeneratorInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $urlGenerator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Route definitions |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $routes; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Base url |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $baseUrl; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* RouteRepository constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $baseUrl |
|
47
|
|
|
* @param array $routes [name => definition] |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(string $baseUrl, array $routes) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->baseUrl = $baseUrl; |
|
52
|
|
|
$this->routes = $routes; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Generate URL by route |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $name Route name |
|
59
|
|
|
* @param array $parameters Request parameters |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function generate(string $name, array $parameters = []): string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->getUrlGenerator()->generate($name, $parameters); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get URL generator |
|
69
|
|
|
* |
|
70
|
|
|
* @return UrlGeneratorInterface |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getUrlGenerator(): UrlGeneratorInterface |
|
73
|
|
|
{ |
|
74
|
|
|
if ($this->urlGenerator === null) { |
|
75
|
|
|
$this->urlGenerator = new UrlGenerator( |
|
|
|
|
|
|
76
|
|
|
$this->assembleRoutesCollection(), |
|
77
|
|
|
new RequestContext($this->baseUrl) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->urlGenerator; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Assemble routes collection |
|
86
|
|
|
* |
|
87
|
|
|
* @return RouteCollection |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function assembleRoutesCollection(): RouteCollection |
|
90
|
|
|
{ |
|
91
|
|
|
$collection = new RouteCollection(); |
|
92
|
|
|
|
|
93
|
|
|
foreach ($this->routes as $name => $definition) |
|
94
|
|
|
{ |
|
95
|
|
|
$route = new Route($definition['path']); |
|
96
|
|
|
$route->setMethods($definition['methods']); |
|
97
|
|
|
|
|
98
|
|
|
$collection->add($name, $route); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $collection; |
|
102
|
|
|
} |
|
103
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..