Skip to content

Commit cb9124e

Browse files
committed
Merge pull request #218 from symfony-cmf/route-by-uuid
implement getting a route by uuid too
2 parents 546aa6a + c2d6bee commit cb9124e

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Changelog
22
=========
33

4+
* **2014-03-23**: When using PHPCR-ODM, routes can now be generated with their
5+
uuid as route name as well, in addition to the repository path.
6+
47
* **2013-11-28**: [BC BREAK] the alias attribute of the <template-by-class> is
58
renamed to class in the bundle configuration.
69

Doctrine/Phpcr/RouteProvider.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use PHPCR\Query\RowInterface;
2121

22+
use PHPCR\Util\UUIDHelper;
2223
use Symfony\Component\Routing\Route as SymfonyRoute;
2324
use Symfony\Component\Routing\RouteCollection;
2425
use Symfony\Component\Routing\Exception\RouteNotFoundException;
@@ -123,20 +124,29 @@ protected function getCandidates($url)
123124

124125
/**
125126
* {@inheritDoc}
127+
*
128+
* @param string $name The absolute path or uuid of the Route document.
126129
*/
127130
public function getRouteByName($name)
128131
{
129-
// $name is the route document path
130-
if ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) {
132+
if (UUIDHelper::isUUID($name)) {
133+
$route = $this->getObjectManager()->find($this->className, $name);
134+
if ($route
135+
&& '' !== $this->idPrefix
136+
&& 0 !== strpos($this->getObjectManager()->getUnitOfWork()->getDocumentId($route), $this->idPrefix)
137+
) {
138+
$route = null;
139+
}
140+
} elseif ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) {
131141
$route = $this->getObjectManager()->find($this->className, $name);
132142
}
133143

134144
if (empty($route)) {
135-
throw new RouteNotFoundException(sprintf('No route found for path "%s"', $name));
145+
throw new RouteNotFoundException(sprintf('No route found at "%s"', $name));
136146
}
137147

138148
if (!$route instanceof SymfonyRoute) {
139-
throw new RouteNotFoundException(sprintf('Document at path "%s" is no route', $name));
149+
throw new RouteNotFoundException(sprintf('Document at "%s" is no route', $name));
140150
}
141151

142152
return $route;
@@ -188,7 +198,7 @@ public function getRoutesByNames($names = null)
188198

189199
if ('' !== $this->idPrefix) {
190200
foreach ($names as $key => $name) {
191-
if (0 !== strpos($name, $this->idPrefix)) {
201+
if (!UUIDHelper::isUUID($name) && 0 !== strpos($name, $this->idPrefix)) {
192202
unset($names[$key]);
193203
}
194204
}

Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Doctrine\Phpcr;
1414

15+
use PHPCR\Util\UUIDHelper;
1516
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RouteProvider;
1617

1718
class RouteProviderTest extends \PHPUnit_Framework_Testcase
@@ -73,6 +74,57 @@ public function testGetRouteByName()
7374
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
7475
}
7576

77+
public function testGetRouteByNameUuid()
78+
{
79+
$uuid = UUIDHelper::generateUUID();
80+
$this->route
81+
->expects($this->any())
82+
->method('getPath')
83+
->will($this->returnValue('/cms/routes/test-route'))
84+
;
85+
$objectManager = $this
86+
->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager')
87+
->disableOriginalConstructor()
88+
->getMock()
89+
;
90+
$uow = $this
91+
->getMockBuilder('Doctrine\ODM\PHPCR\UnitOfWork')
92+
->disableOriginalConstructor()
93+
->getMock()
94+
;
95+
$objectManager
96+
->expects($this->any())
97+
->method('find')
98+
->with(null, $uuid)
99+
->will($this->returnValue($this->route))
100+
;
101+
$objectManager
102+
->expects($this->any())
103+
->method('getUnitOfWork')
104+
->will($this->returnValue($uow))
105+
;
106+
$uow
107+
->expects($this->any())
108+
->method('getDocumentId')
109+
->will($this->returnValue('/cms/routes/test-route'))
110+
;
111+
112+
$this->managerRegistry
113+
->expects($this->any())
114+
->method('getManager')
115+
->will($this->returnValue($objectManager))
116+
;
117+
118+
$routeProvider = new RouteProvider($this->managerRegistry);
119+
$routeProvider->setManagerName('default');
120+
121+
$routeProvider->setPrefix('/cms/routes/');
122+
$foundRoute = $routeProvider->getRouteByName($uuid);
123+
124+
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
125+
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
126+
}
127+
76128
/**
77129
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
78130
*/

0 commit comments

Comments
 (0)