1 | <?php |
||
24 | class MetaRouter extends DynamicRouter |
||
25 | { |
||
26 | protected $internalRoutesCache = []; |
||
27 | |||
28 | /** |
||
29 | * @param string|Meta $name |
||
30 | * @param array $parameters |
||
31 | * @param bool|int|string $referenceType |
||
32 | * |
||
33 | * @return mixed|string |
||
34 | * |
||
35 | * @throws RouteNotFoundException |
||
36 | */ |
||
37 | 84 | public function generate($name, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
|
38 | { |
||
39 | 84 | $cacheKey = $this->getCacheKey($name, $parameters, $referenceType); |
|
40 | 84 | if (array_key_exists($cacheKey, $this->internalRoutesCache)) { |
|
41 | 2 | return $this->internalRoutesCache[$cacheKey]; |
|
42 | } |
||
43 | |||
44 | 84 | if ($name instanceof Meta && $name->getValues() instanceof ArticleInterface) { |
|
45 | 5 | $parameters['slug'] = $name->getValues()->getSlug(); |
|
46 | 5 | $route = $name->getValues()->getRoute(); |
|
47 | |||
48 | 5 | if (null === $route && $name->getContext()->getCurrentPage()) { |
|
49 | 1 | $parameters['slug'] = null; |
|
50 | 5 | $route = $name->getContext()->getCurrentPage()->getValues(); |
|
51 | } |
||
52 | 80 | } elseif ($name instanceof Meta && $name->getValues() instanceof RouteInterface) { |
|
53 | $route = $name->getValues(); |
||
54 | } else { |
||
55 | 80 | $route = $name; |
|
56 | } |
||
57 | |||
58 | 84 | if (null === $route) { |
|
59 | throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', $name)); |
||
60 | } |
||
61 | |||
62 | 84 | $result = parent::generate($route, $parameters, $referenceType); |
|
|
|||
63 | 13 | $this->internalRoutesCache[$cacheKey] = $result; |
|
64 | 13 | unset($route, $name, $parameters); |
|
65 | |||
66 | 13 | return $result; |
|
67 | } |
||
68 | |||
69 | 84 | private function getCacheKey($route, $parameters, $type) |
|
70 | { |
||
71 | 84 | if ($route instanceof Meta && $route->getValues() instanceof ArticleInterface) { |
|
72 | 5 | $name = $route->getValues()->getId(); |
|
73 | 80 | } elseif ($route instanceof Meta && $route->getValues() instanceof RouteInterface) { |
|
74 | $name = $route->getValues()->getName(); |
||
75 | } elseif ($route instanceof RouteInterface) { |
||
76 | 2 | $name = $route->getName(); |
|
77 | } else { |
||
78 | 78 | $name = $route; |
|
79 | } |
||
80 | |||
81 | 84 | return md5($name.serialize($parameters).$type); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 84 | public function supports($name) |
|
94 | } |
||
95 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.