1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Core Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2015 Sourcefabric z.u. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2015 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\CoreBundle\Routing; |
16
|
|
|
|
17
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleInterface; |
18
|
|
|
use SWP\Bundle\ContentBundle\Model\RouteInterface; |
19
|
|
|
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter; |
20
|
|
|
use SWP\Component\TemplatesSystem\Gimme\Meta\Meta; |
21
|
|
|
use Symfony\Component\Routing\Exception\RouteNotFoundException; |
22
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
23
|
|
|
|
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) |
88
|
|
|
{ |
89
|
84 |
|
return ($name instanceof Meta && ( |
90
|
4 |
|
$name->getValues() instanceof ArticleInterface || |
91
|
4 |
|
$name->getValues() instanceof RouteInterface |
92
|
84 |
|
)) || $name instanceof RouteInterface || (is_string($name) && $name !== 'homepage'); |
93
|
|
|
} |
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.