|
9 | 9 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
10 | 10 | use Symfony\Component\Routing\Generator\UrlGenerator;
|
11 | 11 |
|
12 |
| -use Symfony\Cmf\Component\Routing\RouteProviderInterface; |
13 |
| - |
14 | 12 | /**
|
15 | 13 | * A generator that tries to generate routes from object, route names or
|
16 | 14 | * content objects or names.
|
@@ -46,8 +44,8 @@ public function setContentRepository(ContentRepositoryInterface $contentReposito
|
46 | 44 | *
|
47 | 45 | * @param string $name ignored
|
48 | 46 | * @param array $parameters must either contain the field 'route' with a
|
49 |
| - * RouteObjectInterface or the field 'content' with the document |
50 |
| - * instance to get the route for (implementing RouteAwareInterface) |
| 47 | + * RouteObjectInterface or the field 'content_id' with a document |
| 48 | + * id to get the route for (implementing RouteAwareInterface) |
51 | 49 | *
|
52 | 50 | * @throws RouteNotFoundException If there is no such route in the database
|
53 | 51 | */
|
@@ -120,51 +118,55 @@ protected function getBestLocaleRoute(SymfonyRoute $route, $parameters)
|
120 | 118 | }
|
121 | 119 |
|
122 | 120 | /**
|
123 |
| - * Get the route based on the content field in parameters |
| 121 | + * Get the route based on the $name that is a RouteAwareInterface or a |
| 122 | + * RouteAwareInterface content found in the content repository with the |
| 123 | + * content_id specified in parameters. |
124 | 124 | *
|
125 | 125 | * Called in generate when there is no route given in the parameters.
|
126 | 126 | *
|
127 | 127 | * If there is more than one route for the content, tries to find the
|
128 | 128 | * first one that matches the _locale (provided in $parameters or otherwise
|
129 | 129 | * defaulting to the request locale).
|
130 | 130 | *
|
131 |
| - * If none is found, falls back to just return the first route. |
| 131 | + * If no route with matching locale is found, falls back to just return the |
| 132 | + * first route. |
132 | 133 | *
|
133 | 134 | * @param mixed $name
|
134 | 135 | * @param array $parameters which should contain a content field containing a RouteAwareInterface object
|
135 | 136 | *
|
136 | 137 | * @return SymfonyRoute the route instance
|
137 | 138 | *
|
138 |
| - * @throws RouteNotFoundException if there is no content field in the |
139 |
| - * parameters or its not possible to build a route from that object |
| 139 | + * @throws RouteNotFoundException if no route can be determined |
140 | 140 | */
|
141 | 141 | protected function getRouteByContent($name, &$parameters)
|
142 | 142 | {
|
143 | 143 | if ($name instanceof RouteAwareInterface) {
|
144 | 144 | $content = $name;
|
145 |
| - } elseif (isset($parameters['content_id']) && null !== $this->contentRepository) { |
| 145 | + } elseif (isset($parameters['content_id']) |
| 146 | + && null !== $this->contentRepository |
| 147 | + ) { |
146 | 148 | $content = $this->contentRepository->findById($parameters['content_id']);
|
147 |
| - } elseif (isset($parameters['content'])) { |
148 |
| - $content = $parameters['content']; |
149 |
| - } |
150 |
| - |
151 |
| - unset($parameters['content'], $parameters['content_id']); |
152 |
| - |
153 |
| - if (empty($content)) { |
154 |
| - throw new RouteNotFoundException('Neither the route name, nor a parameter "content" or "content_id" could be resolved to an content instance'); |
155 |
| - } |
156 |
| - |
157 |
| - if (!$content instanceof RouteAwareInterface) { |
158 |
| - $hint = is_object($content) ? get_class($content) : gettype($content); |
159 |
| - throw new RouteNotFoundException('The content does not implement RouteAwareInterface: ' . $hint); |
| 149 | + if (empty($content)) { |
| 150 | + throw new RouteNotFoundException('The content repository found nothing at id ' . $parameters['content_id']); |
| 151 | + } |
| 152 | + if (!$content instanceof RouteAwareInterface) { |
| 153 | + throw new RouteNotFoundException('Content repository did not return a RouteAwareInterface for id ' . $parameters['content_id']); |
| 154 | + } |
| 155 | + } else { |
| 156 | + $hint = is_object($name) ? get_class($name) : gettype($name); |
| 157 | + throw new RouteNotFoundException("The route name argument '$hint' is not RouteAwareInterface and there is no 'content_id' parameter"); |
160 | 158 | }
|
161 | 159 |
|
162 | 160 | $routes = $content->getRoutes();
|
163 | 161 | if (empty($routes)) {
|
164 |
| - $hint = method_exists($content, 'getPath') ? $content->getPath() : get_class($content); |
165 |
| - throw new RouteNotFoundException('Document has no route: ' . $hint); |
| 162 | + $hint = ($this->contentRepository && $this->contentRepository->getContentId($content)) |
| 163 | + ? $this->contentRepository->getContentId($content) |
| 164 | + : get_class($content); |
| 165 | + throw new RouteNotFoundException('Content document has no route: ' . $hint); |
166 | 166 | }
|
167 | 167 |
|
| 168 | + unset($parameters['content_id']); |
| 169 | + |
168 | 170 | $route = $this->getRouteByLocale($routes, $this->getLocale($parameters));
|
169 | 171 | if ($route) {
|
170 | 172 | return $route;
|
@@ -233,4 +235,20 @@ public function supports($name)
|
233 | 235 | {
|
234 | 236 | return ! $name || parent::supports($name) || $name instanceof RouteAwareInterface;
|
235 | 237 | }
|
| 238 | + |
| 239 | + /** |
| 240 | + * {@inheritDoc} |
| 241 | + */ |
| 242 | + public function getRouteDebugMessage($name, array $parameters = array()) |
| 243 | + { |
| 244 | + if (empty($name) && isset($parameters['content_id'])) { |
| 245 | + return 'Content id ' . $parameters['content_id']; |
| 246 | + } |
| 247 | + |
| 248 | + if ($name instanceof RouteAwareInterface) { |
| 249 | + return 'Route aware content ' . $name; |
| 250 | + } |
| 251 | + |
| 252 | + return parent::getRouteDebugMessage($name, $parameters); |
| 253 | + } |
236 | 254 | }
|
0 commit comments