Skip to content

Commit

Permalink
Merge pull request #218 from symfony-cmf/route-by-uuid
Browse files Browse the repository at this point in the history
implement getting a route by uuid too
  • Loading branch information
dbu committed Mar 23, 2014
2 parents 546aa6a + c2d6bee commit cb9124e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
=========

* **2014-03-23**: When using PHPCR-ODM, routes can now be generated with their
uuid as route name as well, in addition to the repository path.

* **2013-11-28**: [BC BREAK] the alias attribute of the <template-by-class> is
renamed to class in the bundle configuration.

Expand Down
20 changes: 15 additions & 5 deletions Doctrine/Phpcr/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use PHPCR\Query\RowInterface;

use PHPCR\Util\UUIDHelper;
use Symfony\Component\Routing\Route as SymfonyRoute;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
Expand Down Expand Up @@ -123,20 +124,29 @@ protected function getCandidates($url)

/**
* {@inheritDoc}
*
* @param string $name The absolute path or uuid of the Route document.
*/
public function getRouteByName($name)
{
// $name is the route document path
if ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) {
if (UUIDHelper::isUUID($name)) {
$route = $this->getObjectManager()->find($this->className, $name);
if ($route
&& '' !== $this->idPrefix
&& 0 !== strpos($this->getObjectManager()->getUnitOfWork()->getDocumentId($route), $this->idPrefix)
) {
$route = null;
}
} elseif ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) {
$route = $this->getObjectManager()->find($this->className, $name);
}

if (empty($route)) {
throw new RouteNotFoundException(sprintf('No route found for path "%s"', $name));
throw new RouteNotFoundException(sprintf('No route found at "%s"', $name));
}

if (!$route instanceof SymfonyRoute) {
throw new RouteNotFoundException(sprintf('Document at path "%s" is no route', $name));
throw new RouteNotFoundException(sprintf('Document at "%s" is no route', $name));
}

return $route;
Expand Down Expand Up @@ -188,7 +198,7 @@ public function getRoutesByNames($names = null)

if ('' !== $this->idPrefix) {
foreach ($names as $key => $name) {
if (0 !== strpos($name, $this->idPrefix)) {
if (!UUIDHelper::isUUID($name) && 0 !== strpos($name, $this->idPrefix)) {
unset($names[$key]);
}
}
Expand Down
52 changes: 52 additions & 0 deletions Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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

use PHPCR\Util\UUIDHelper;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RouteProvider;

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

public function testGetRouteByNameUuid()
{
$uuid = UUIDHelper::generateUUID();
$this->route
->expects($this->any())
->method('getPath')
->will($this->returnValue('/cms/routes/test-route'))
;
$objectManager = $this
->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager')
->disableOriginalConstructor()
->getMock()
;
$uow = $this
->getMockBuilder('Doctrine\ODM\PHPCR\UnitOfWork')
->disableOriginalConstructor()
->getMock()
;
$objectManager
->expects($this->any())
->method('find')
->with(null, $uuid)
->will($this->returnValue($this->route))
;
$objectManager
->expects($this->any())
->method('getUnitOfWork')
->will($this->returnValue($uow))
;
$uow
->expects($this->any())
->method('getDocumentId')
->will($this->returnValue('/cms/routes/test-route'))
;

$this->managerRegistry
->expects($this->any())
->method('getManager')
->will($this->returnValue($objectManager))
;

$routeProvider = new RouteProvider($this->managerRegistry);
$routeProvider->setManagerName('default');

$routeProvider->setPrefix('/cms/routes/');
$foundRoute = $routeProvider->getRouteByName($uuid);

$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
}

/**
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
*/
Expand Down

0 comments on commit cb9124e

Please sign in to comment.