@@ -115,6 +115,7 @@ public function removeAutoRoute(AutoRouteInterface $autoRoute)
115
115
public function createAutoRoute (UriContext $ uriContext , $ contentDocument , $ autoRouteTag )
116
116
{
117
117
$ path = $ this ->baseRoutePath ;
118
+ $ routeType = AutoRouteInterface::TYPE_PRIMARY ;
118
119
$ document = $ parentDocument = $ this ->dm ->find (null , $ path );
119
120
if (null === $ parentDocument ) {
120
121
throw new \RuntimeException (sprintf ('The "route_basepath" configuration points to a non-existant path "%s". ' ,
@@ -137,12 +138,24 @@ public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoR
137
138
$ parentDocument = $ document ;
138
139
}
139
140
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 );
146
+ }
147
+ $ nodeClass = get_class ($ node );
148
+ $ genericFqcn = 'Doctrine\ODM\PHPCR\Document\Generic ' ;
149
+ throw new \RuntimeException (
150
+ "Unexpected node class ' $ nodeClass' at path ' $ finalAutoRoutePath'. Only ' $ genericFqcn' expected. "
151
+ );
152
+ }
140
153
$ headRoute = new $ this ->autoRouteFqcn ();
141
154
$ headRoute ->setContent ($ contentDocument );
142
155
$ headRoute ->setName ($ headName );
143
156
$ headRoute ->setParent ($ document );
144
157
$ headRoute ->setAutoRouteTag ($ autoRouteTag );
145
- $ headRoute ->setType (AutoRouteInterface:: TYPE_PRIMARY );
158
+ $ headRoute ->setType ($ routeType );
146
159
147
160
return $ headRoute ;
148
161
}
@@ -201,4 +214,31 @@ private function getPathFromUri($uri)
201
214
{
202
215
return $ this ->baseRoutePath . $ uri ;
203
216
}
217
+
218
+ /**
219
+ * @param Generic $node
220
+ * @param object $contentDocument
221
+ * @param string $autoRouteTag
222
+ * @param string $routeType
223
+ * @return AutoRouteInterface
224
+ */
225
+ private function convertGenericNodeInAutoRouteNode (Generic $ node , $ contentDocument , $ autoRouteTag , $ routeType )
226
+ {
227
+ $ autoRouteClassName = $ this ->autoRouteFqcn ;
228
+ $ mapper = $ this ->dm ->getConfiguration ()->getDocumentClassMapper ();
229
+ $ mapper ->writeMetadata ($ this ->dm , $ node ->getNode (), $ autoRouteClassName );
230
+ $ this ->dm ->getPhpcrSession ()->save ();
231
+ // Detach is needed to force Doctrine to re-load the node
232
+ $ this ->dm ->detach ($ node );
233
+ $ autoRoute = $ this ->dm ->find (null , $ node ->getId ());
234
+ if (!$ autoRoute instanceof $ autoRouteClassName ) {
235
+ throw new \RuntimeException (
236
+ "Something went wrong converting Generic node into an AutoRouteInterface node. "
237
+ );
238
+ }
239
+ $ autoRoute ->setContent ($ contentDocument );
240
+ $ autoRoute ->setAutoRouteTag ($ autoRouteTag );
241
+ $ autoRoute ->setType ($ routeType );
242
+ return $ autoRoute ;
243
+ }
204
244
}
0 commit comments