Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 49d2fa7

Browse files
committed
Adapter now converts Generic in AutoRoute if needed
1 parent 8f8d3aa commit 49d2fa7

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

Adapter/PhpcrOdmAdapter.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function removeAutoRoute(AutoRouteInterface $autoRoute)
115115
public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoRouteTag)
116116
{
117117
$path = $this->baseRoutePath;
118+
$routeType = AutoRouteInterface::TYPE_PRIMARY;
118119
$document = $parentDocument = $this->dm->find(null, $path);
119120
if (null === $parentDocument) {
120121
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
137138
$parentDocument = $document;
138139
}
139140

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+
}
140153
$headRoute = new $this->autoRouteFqcn();
141154
$headRoute->setContent($contentDocument);
142155
$headRoute->setName($headName);
143156
$headRoute->setParent($document);
144157
$headRoute->setAutoRouteTag($autoRouteTag);
145-
$headRoute->setType(AutoRouteInterface::TYPE_PRIMARY);
158+
$headRoute->setType($routeType);
146159

147160
return $headRoute;
148161
}
@@ -201,4 +214,31 @@ private function getPathFromUri($uri)
201214
{
202215
return $this->baseRoutePath . $uri;
203216
}
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+
}
204244
}

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,4 +552,40 @@ public function testConflictResolverDefaultThrowException()
552552
$this->getDm()->persist($blog);
553553
$this->getDm()->flush();
554554
}
555+
556+
public function testGenericNodeShouldBeConvertedInAnAutoRouteNode()
557+
{
558+
$this->assertNull($this->getDm()->find(null, '/test/auto-route/blog'));
559+
$this->assertNull($this->getDm()->find(null, '/test/auto-route/blog/my-post'));
560+
561+
$blog = new Blog;
562+
$blog->path = '/test/my-post';
563+
$blog->title = 'My Post';
564+
$this->getDm()->persist($blog);
565+
$this->getDm()->flush();
566+
567+
$this->assertInstanceOf(
568+
'Doctrine\ODM\PHPCR\Document\Generic',
569+
$this->getDm()->find(null, '/test/auto-route/blog')
570+
);
571+
$blogRoute = $this->getDm()->find(null, '/test/auto-route/blog/my-post');
572+
$this->assertInstanceOf('Symfony\Cmf\Component\RoutingAuto\Model\AutoRouteInterface', $blogRoute);
573+
$this->assertSame($blog, $blogRoute->getContent());
574+
575+
$page = new Page;
576+
$page->path = '/test/blog';
577+
$page->title = 'Blog';
578+
579+
$this->getDm()->persist($page);
580+
$this->getDm()->flush();
581+
582+
$this->assertInstanceOf(
583+
'Symfony\Cmf\Component\RoutingAuto\Model\AutoRouteInterface',
584+
$this->getDm()->find(null, '/test/auto-route/blog')
585+
);
586+
$this->assertInstanceOf(
587+
'Symfony\Cmf\Component\RoutingAuto\Model\AutoRouteInterface',
588+
$this->getDm()->find(null, '/test/auto-route/blog/my-post')
589+
);
590+
}
555591
}

Tests/Unit/Adapter/PhpcrOdmAdapterTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ public function testCreateAutoRoute($path, $expectedParentPath, $expectedName, $
109109
if ($parentPathExists) {
110110
$this->dm->find(null, $expectedParentPath)
111111
->willReturn($this->parentRoute);
112+
$this->dm->find(null, $expectedParentPath . '/' . $expectedName)
113+
->willReturn(null);
112114
} else {
113115
$this->dm->find(null, $expectedParentPath)
114116
->willReturn(null);

0 commit comments

Comments
 (0)