@@ -114,20 +114,19 @@ public function removeAutoRoute(AutoRouteInterface $autoRoute)
114
114
*/
115
115
public function createAutoRoute (UriContext $ uriContext , $ contentDocument , $ autoRouteTag )
116
116
{
117
- $ path = $ this ->baseRoutePath ;
118
- $ routeType = AutoRouteInterface::TYPE_PRIMARY ;
119
- $ document = $ parentDocument = $ this ->dm ->find (null , $ path );
117
+ $ basePath = $ this ->baseRoutePath ;
118
+ $ document = $ parentDocument = $ this ->dm ->find (null , $ basePath );
120
119
if (null === $ parentDocument ) {
121
120
throw new \RuntimeException (sprintf ('The "route_basepath" configuration points to a non-existant path "%s". ' ,
122
- $ path
121
+ $ basePath
123
122
));
124
123
}
125
124
126
125
$ segments = preg_split ('#/# ' , $ uriContext ->getUri (), null , PREG_SPLIT_NO_EMPTY );
127
126
$ headName = array_pop ($ segments );
128
127
foreach ($ segments as $ segment ) {
129
- $ path .= '/ ' . $ segment ;
130
- $ document = $ this ->dm ->find (null , $ path );
128
+ $ basePath .= '/ ' . $ segment ;
129
+ $ document = $ this ->dm ->find (null , $ basePath );
131
130
132
131
if (null === $ document ) {
133
132
$ document = new Generic ();
@@ -138,24 +137,34 @@ public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoR
138
137
$ parentDocument = $ document ;
139
138
}
140
139
141
- $ finalAutoRoutePath = $ path . '/ ' . $ headName ;
142
- $ node = $ this ->dm ->find (null , $ finalAutoRoutePath );
143
- if ($ node ) {
144
- if ($ node instanceof Generic) {
145
- return $ this ->convertGenericNodeInAutoRouteNode ($ node , $ contentDocument , $ autoRouteTag , $ routeType );
140
+ $ path = $ basePath . '/ ' . $ headName ;
141
+ $ existingDocument = $ this ->dm ->find (null , $ path );
142
+
143
+ if ($ existingDocument ) {
144
+ if ($ existingDocument instanceof Generic) {
145
+ return $ this ->migrateGenericToAutoRoute (
146
+ $ existingDocument ,
147
+ $ contentDocument ,
148
+ $ autoRouteTag ,
149
+ AutoRouteInterface::TYPE_PRIMARY
150
+ );
146
151
}
147
- $ nodeClass = get_class ($ node );
148
- $ genericFqcn = 'Doctrine\ODM\PHPCR\Document\Generic ' ;
152
+
149
153
throw new \RuntimeException (
150
- "Unexpected node class ' $ nodeClass' at path ' $ finalAutoRoutePath'. Only ' $ genericFqcn' expected. "
154
+ sprintf (
155
+ 'Encountered existing PHPCR-ODM document at path "%s" of class "%s", the route tree should ' .
156
+ 'contain only instances of AutoRouteInterface. ' ,
157
+ $ path ,
158
+ get_class ($ existingDocument )
159
+ )
151
160
);
152
161
}
153
162
$ headRoute = new $ this ->autoRouteFqcn ();
154
163
$ headRoute ->setContent ($ contentDocument );
155
164
$ headRoute ->setName ($ headName );
156
165
$ headRoute ->setParent ($ document );
157
166
$ headRoute ->setAutoRouteTag ($ autoRouteTag );
158
- $ headRoute ->setType ($ routeType );
167
+ $ headRoute ->setType (AutoRouteInterface:: TYPE_PRIMARY );
159
168
160
169
return $ headRoute ;
161
170
}
@@ -203,11 +212,13 @@ public function getReferringAutoRoutes($contentDocument)
203
212
public function findRouteForUri ($ uri , UriContext $ uriContext )
204
213
{
205
214
$ path = $ this ->getPathFromUri ($ uri );
206
- $ node = $ this ->dm ->find (null , $ path );
207
- if ($ node instanceof AutoRouteInterface) {
208
- return $ node ;
215
+ $ document = $ this ->dm ->find (null , $ path );
216
+
217
+ if ($ document instanceof AutoRouteInterface) {
218
+ return $ document ;
209
219
}
210
- return false ;
220
+
221
+ return null ;
211
222
}
212
223
213
224
private function getPathFromUri ($ uri )
@@ -216,29 +227,34 @@ private function getPathFromUri($uri)
216
227
}
217
228
218
229
/**
219
- * @param Generic $node
230
+ * Convert the given generic document to an auto route document, migrating any children which it may have.
231
+ *
232
+ * @param Generic $document
220
233
* @param object $contentDocument
221
234
* @param string $autoRouteTag
222
235
* @param string $routeType
223
236
* @return AutoRouteInterface
224
237
*/
225
- private function convertGenericNodeInAutoRouteNode (Generic $ node , $ contentDocument , $ autoRouteTag , $ routeType )
238
+ private function migrateGenericToAutoRoute (Generic $ document , $ contentDocument , $ autoRouteTag , $ routeType )
226
239
{
227
240
$ autoRouteClassName = $ this ->autoRouteFqcn ;
228
241
$ mapper = $ this ->dm ->getConfiguration ()->getDocumentClassMapper ();
229
- $ mapper ->writeMetadata ($ this ->dm , $ node ->getNode (), $ autoRouteClassName );
242
+ $ mapper ->writeMetadata ($ this ->dm , $ document ->getNode (), $ autoRouteClassName );
230
243
$ this ->dm ->getPhpcrSession ()->save ();
231
244
// Detach is needed to force Doctrine to re-load the node
232
- $ this ->dm ->detach ($ node );
233
- $ autoRoute = $ this ->dm ->find (null , $ node ->getId ());
245
+ $ this ->dm ->detach ($ document );
246
+ $ autoRoute = $ this ->dm ->find (null , $ document ->getId ());
247
+
234
248
if (!$ autoRoute instanceof $ autoRouteClassName ) {
235
249
throw new \RuntimeException (
236
- " Something went wrong converting Generic node into an AutoRouteInterface node. "
250
+ ' Something went wrong converting Generic node into an AutoRouteInterface node. '
237
251
);
238
252
}
253
+
239
254
$ autoRoute ->setContent ($ contentDocument );
240
255
$ autoRoute ->setAutoRouteTag ($ autoRouteTag );
241
256
$ autoRoute ->setType ($ routeType );
257
+
242
258
return $ autoRoute ;
243
259
}
244
260
}
0 commit comments