6
6
use Symfony \Component \Routing \RouterInterface ;
7
7
use Symfony \Component \Routing \RequestContext ;
8
8
use Symfony \Component \Routing \Exception \RouteNotFoundException ;
9
- use Doctrine \Common \Persistence \ObjectManager ;
9
+ use Symfony \Component \Routing \Exception \ResourceNotFoundException ;
10
+ use Symfony \Component \Routing \RouteCollection ;
11
+
10
12
use Symfony \Cmf \Bundle \ChainRoutingBundle \Resolver \ControllerResolverInterface ;
11
13
14
+ use Doctrine \Common \Persistence \ObjectManager ;
15
+
12
16
/**
13
17
* A router that reads entries from a Object-Document Mapper store.
14
18
*
@@ -30,11 +34,13 @@ class DoctrineRouter implements RouterInterface
30
34
* route has one associated
31
35
*/
32
36
const CONTENT_KEY = 'contentDocument ' ;
37
+
33
38
/**
34
39
* key for the request attribute that contains the template this document
35
40
* wants to use
36
41
*/
37
42
const CONTENT_TEMPLATE = 'contentTemplate ' ;
43
+
38
44
protected $ om ;
39
45
protected $ resolvers ;
40
46
protected $ routeClass ;
@@ -70,12 +76,17 @@ public function addControllerResolver(ControllerResolverInterface $resolver)
70
76
$ this ->resolvers [] = $ resolver ;
71
77
}
72
78
73
- // inherit doc
79
+ /**
80
+ * {@inheritDoc}
81
+ */
74
82
public function setContext (RequestContext $ context )
75
83
{
76
84
$ this ->context = $ context ;
77
85
}
78
- // inherit doc
86
+
87
+ /**
88
+ * {@inheritDoc}
89
+ */
79
90
public function getContext ()
80
91
{
81
92
return $ this ->context ;
@@ -130,11 +141,13 @@ public function generate($name, $parameters = array(), $absolute = false)
130
141
public function getRouteCollection ()
131
142
{
132
143
/* TODO */
133
- return new \ Symfony \ Component \ Routing \ RouteCollection ();
144
+ return new RouteCollection ();
134
145
}
135
146
136
147
/**
137
148
* Set the doctrine entity or document manager that will know the urls
149
+ *
150
+ * @param ObjectManager $om
138
151
*/
139
152
public function setObjectManager (ObjectManager $ om )
140
153
{
@@ -154,19 +167,19 @@ public function setObjectManager(ObjectManager $om)
154
167
* cases, the action to call on that controller is appended, separated with
155
168
* two colons.
156
169
*
157
- * @throws ResourceNotFoundException If the requested url does not exist in the ODM
158
- * @throws MethodNotAllowedException If the resource was found but the request method is not allowed
159
- *
160
170
* @param string $url the full requested url. TODO: is locale eaten away or kept too?
161
171
*
162
172
* @return array as described above
173
+ *
174
+ * @throws ResourceNotFoundException If the requested url does not exist in the ODM
175
+ * @throws MethodNotAllowedException If the resource was found but the request method is not allowed
163
176
*/
164
177
public function match ($ url )
165
178
{
166
179
$ route = $ this ->findRouteForUrl ($ url );
167
180
168
181
if (! $ route instanceof RouteObjectInterface) {
169
- throw new \ Symfony \ Component \ Routing \ Exception \ ResourceNotFoundException ("No entry or not a route at ' $ url' " );
182
+ throw new ResourceNotFoundException ("No entry or not a route at ' $ url' " );
170
183
}
171
184
172
185
$ defaults = $ route ->getRouteDefaults ();
@@ -178,14 +191,14 @@ public function match($url)
178
191
if ($ controller !== false ) break ;
179
192
}
180
193
if (false === $ controller ) {
181
- throw new \ Symfony \ Component \ Routing \ Exception \ ResourceNotFoundException ("The resolver was not able to determine a controller for ' $ url' " );;
194
+ throw new ResourceNotFoundException ("The resolver was not able to determine a controller for ' $ url' " );;
182
195
}
183
196
$ defaults ['_controller ' ] = $ controller ;
184
197
}
185
198
186
199
$ defaults [self ::CONTENT_KEY ] = $ route ->getRouteContent ();
187
200
$ defaults ['path ' ] = $ url ; // TODO: get rid of this
188
- $ defaults ['_route ' ] = 'whatever ' ; //FIXME: what is this? without, we get an undefined index in RouterListener::onKernelRequest
201
+ $ defaults ['_route ' ] = 'chain_router_doctrine_route ' . str_replace ( ' / ' , ' _ ' , $ url );
189
202
190
203
return $ defaults ;
191
204
}
@@ -196,7 +209,7 @@ public function match($url)
196
209
*
197
210
* Overwrite this method for other ODM or ORM repositories.
198
211
*
199
- * @param $url The url to find
212
+ * @param string $url The url to find
200
213
*
201
214
* @return the RouteObjectInterface object for this url or null if none is found
202
215
*/
@@ -215,15 +228,18 @@ protected function findRouteForUrl($url)
215
228
/**
216
229
* Called in generate when there is no route given in the parameters
217
230
*
218
- * @param $parameters which should contain a content field containing a RouteAwareInterface object
231
+ * @param array $parameters which should contain a content field containing a RouteAwareInterface object
232
+ *
219
233
* @return the route instance
234
+ *
220
235
* @throws RouteNotFoundException
221
236
*/
222
237
protected function getRouteFromContent ($ parameters )
223
238
{
224
239
if (! isset ($ parameters ['content ' ])) {
225
240
throw new RouteNotFoundException ;
226
241
}
242
+
227
243
if (! $ parameters ['content ' ] instanceof RouteAwareInterface) {
228
244
$ hint = is_object ($ parameters ['content ' ]) ? get_class ($ parameters ['content ' ]) : gettype ($ parameters ['content ' ]);
229
245
throw new RouteNotFoundException ('The content does not implement RouteAwareInterface: ' . $ hint );
@@ -237,5 +253,4 @@ protected function getRouteFromContent($parameters)
237
253
238
254
return reset ($ routes );
239
255
}
240
-
241
256
}
0 commit comments