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