1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Route; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
17
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
18
|
|
|
use Symfony\Component\Routing\RouterInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @final since sonata-project/admin-bundle 3.52 |
22
|
|
|
* |
23
|
|
|
* @author Thomas Rabaix <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class DefaultRouteGenerator implements RouteGeneratorInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var RouterInterface |
29
|
|
|
*/ |
30
|
|
|
private $router; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var RoutesCache |
34
|
|
|
*/ |
35
|
|
|
private $cache; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $caches = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string[] |
44
|
|
|
*/ |
45
|
|
|
private $loaded = []; |
46
|
|
|
|
47
|
|
|
public function __construct(RouterInterface $router, RoutesCache $cache) |
48
|
|
|
{ |
49
|
|
|
$this->router = $router; |
50
|
|
|
$this->cache = $cache; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function generate($name, array $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
54
|
|
|
{ |
55
|
|
|
return $this->router->generate($name, $parameters, $referenceType); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function generateUrl( |
59
|
|
|
AdminInterface $admin, |
60
|
|
|
$name, |
61
|
|
|
array $parameters = [], |
62
|
|
|
$referenceType = UrlGeneratorInterface::ABSOLUTE_PATH |
63
|
|
|
) { |
64
|
|
|
$arrayRoute = $this->generateMenuUrl($admin, $name, $parameters, $referenceType); |
65
|
|
|
|
66
|
|
|
return $this->router->generate( |
67
|
|
|
$arrayRoute['route'], |
68
|
|
|
$arrayRoute['routeParameters'], |
69
|
|
|
$arrayRoute['routeAbsolute'] ? UrlGeneratorInterface::ABSOLUTE_URL : UrlGeneratorInterface::ABSOLUTE_PATH |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function generateMenuUrl( |
74
|
|
|
AdminInterface $admin, |
75
|
|
|
$name, |
76
|
|
|
array $parameters = [], |
77
|
|
|
$referenceType = UrlGeneratorInterface::ABSOLUTE_PATH |
78
|
|
|
) { |
79
|
|
|
// if the admin is a child we automatically append the parent's id |
80
|
|
|
if ($admin->isChild() && $admin->hasRequest()) { |
81
|
|
|
// twig template does not accept variable hash key ... so cannot use admin.idparameter ... |
82
|
|
|
// switch value |
83
|
|
|
if (isset($parameters['id'])) { |
84
|
|
|
$parameters[$admin->getIdParameter()] = $parameters['id']; |
85
|
|
|
unset($parameters['id']); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$parentAdmin = $admin->getParent(); |
89
|
|
|
while (null !== $parentAdmin) { |
90
|
|
|
$parameters[$parentAdmin->getIdParameter()] = $admin->getRequest()->attributes->get($parentAdmin->getIdParameter()); |
91
|
|
|
$parentAdmin = $parentAdmin->isChild() ? $parentAdmin->getParent() : null; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// if the admin is linked to a parent FieldDescription (ie, embedded widget) |
96
|
|
|
if ($admin->hasParentFieldDescription()) { |
97
|
|
|
// merge link parameter if any provided by the parent field |
98
|
|
|
$parameters = array_merge($parameters, $admin->getParentFieldDescription()->getOption('link_parameters', [])); |
99
|
|
|
|
100
|
|
|
$parameters['uniqid'] = $admin->getUniqid(); |
101
|
|
|
$parameters['code'] = $admin->getCode(); |
102
|
|
|
$parameters['pcode'] = $admin->getParentFieldDescription()->getAdmin()->getCode(); |
103
|
|
|
$parameters['puniqid'] = $admin->getParentFieldDescription()->getAdmin()->getUniqid(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ('update' === $name || '|update' === substr($name, -7)) { |
107
|
|
|
$parameters['uniqid'] = $admin->getUniqid(); |
108
|
|
|
$parameters['code'] = $admin->getCode(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// allows to define persistent parameters |
112
|
|
|
if ($admin->hasRequest()) { |
113
|
|
|
$parameters = array_merge($admin->getPersistentParameters(), $parameters); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$code = $this->getCode($admin, $name); |
117
|
|
|
|
118
|
|
|
if (!\array_key_exists($code, $this->caches)) { |
119
|
|
|
throw new \RuntimeException(sprintf('unable to find the route `%s`', $code)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return [ |
123
|
|
|
'route' => $this->caches[$code], |
124
|
|
|
'routeParameters' => $parameters, |
125
|
|
|
'routeAbsolute' => UrlGeneratorInterface::ABSOLUTE_URL === $referenceType, |
126
|
|
|
]; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function hasAdminRoute(AdminInterface $admin, $name) |
130
|
|
|
{ |
131
|
|
|
return \array_key_exists($this->getCode($admin, $name), $this->caches); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function getCode(AdminInterface $admin, string $name): string |
135
|
|
|
{ |
136
|
|
|
$this->loadCache($admin); |
137
|
|
|
|
138
|
|
|
// someone provide the fullname |
139
|
|
|
if (!$admin->isChild() && \array_key_exists($name, $this->caches)) { |
140
|
|
|
return $name; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// NEXT_MAJOR: Uncomment the following line. |
144
|
|
|
// $codePrefix = $admin->getBaseCodeRoute(); |
145
|
|
|
|
146
|
|
|
// NEXT_MAJOR: Remove next 5 lines. |
147
|
|
|
$codePrefix = $admin->getCode(); |
148
|
|
|
|
149
|
|
|
if ($admin->isChild()) { |
150
|
|
|
$codePrefix = $admin->getBaseCodeRoute(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// someone provide a code, so it is a child |
154
|
|
|
if (strpos($name, '.')) { |
155
|
|
|
return $codePrefix.'|'.$name; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $codePrefix.'.'.$name; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function loadCache(AdminInterface $admin): void |
162
|
|
|
{ |
163
|
|
|
if ($admin->isChild()) { |
164
|
|
|
$this->loadCache($admin->getParent()); |
|
|
|
|
165
|
|
|
|
166
|
|
|
return; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (\in_array($admin->getCode(), $this->loaded, true)) { |
170
|
|
|
return; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->caches = array_merge($this->cache->load($admin), $this->caches); |
174
|
|
|
|
175
|
|
|
$this->loaded[] = $admin->getCode(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: