Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 754e6aa

Browse files
committed
Merge pull request symfony#2 from symfony-cmf/content_router_refactoring
[WIP] some refactoring to make the ContentRouter more flexible
2 parents 9e3f7e6 + 718b64c commit 754e6aa

5 files changed

Lines changed: 40 additions & 35 deletions

File tree

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace Symfony\Cmf\Bundle\ChainRoutingBundle\Controller;
44

5-
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
6-
use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver;
5+
use Symfony\Cmf\Bundle\ChainRoutingBundle\Routing\RouteObjectInterface;
76

8-
class DocumentControllerResolver
7+
class ControllerResolver
98
{
10-
119
/**
1210
* array containing the mapping between phpcr_alias and controller
1311
* i.e array('static_pages' => 'symfony_cmf_content.controller:indexAction')
@@ -22,12 +20,12 @@ public function __construct(array $controllers_by_alias = array())
2220
/**
2321
* Retrieves the right controller for the given $document.
2422
*/
25-
public function getController($document)
23+
public function getController(RouteObjectInterface $document)
2624
{
27-
$controller = $document->getController();
28-
29-
if (array_key_exists($controller, $this->controllers_by_alias)) {
30-
return $this->controllers_by_alias[$controller];
25+
$defaults = $document->getRouteDefaults();
26+
if (isset($this->controllers_by_alias[$defaults['type']])) {
27+
$defaults['_controller'] = $this->controllers_by_alias[$defaults['type']];
28+
return $defaults;
3129
}
3230
}
3331

Routing/ContentRouter.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@
33
namespace Symfony\Cmf\Bundle\ChainRoutingBundle\Routing;
44

55
use Symfony\Component\Routing\Router;
6+
use Doctrine\Common\Persistence\ObjectManager;
67

78
class ContentRouter extends Router
89
{
910

10-
protected $document_manager;
11+
protected $om;
1112
protected $controller_resolver;
1213

13-
public function setDocumentManager(\Doctrine\ODM\PHPCR\DocumentManager $dm)
14+
public function setObjectManager(ObjectManager $om)
1415
{
15-
$this->document_manager = $dm;
16+
$this->om = $om;
1617
}
1718

18-
public function setControllerResolver(\Symfony\Cmf\Bundle\ChainRoutingBundle\Controller\DocumentControllerResolver $cr)
19+
public function setControllerResolver(\Symfony\Cmf\Bundle\ChainRoutingBundle\Controller\ControllerResolver $cr)
1920
{
2021
$this->controller_resolver = $cr;
2122
}
@@ -25,28 +26,28 @@ public function setControllerResolver(\Symfony\Cmf\Bundle\ChainRoutingBundle\Con
2526
*
2627
* array(
2728
* "_controller" => "NameSpace\Controller::action",
28-
* "document" => $document)
29+
* "reference" => $document,
30+
* )
2931
*
3032
* @param string $url
3133
* @return array
3234
*/
3335
public function match($url)
3436
{
35-
$node = $this->document_manager->find(null, $url);
37+
$document = $this->om->find(null, $url);
3638

37-
if (!$node || !\method_exists($node, 'getReference')) {
39+
if (!$document instanceof RouteObjectInterface) {
3840
return false;
3941
}
4042

41-
$document = $node->getReference();
42-
$controller = $this->controller_resolver->getController($document);
43-
44-
if (!$controller) {
43+
$defaults = $this->controller_resolver->getController($document);
44+
if (empty($defaults['_controller'])) {
4545
return false;
4646
}
4747

48-
return array('_controller' => $controller,
49-
'document' => $document);
48+
$defaults['reference'] = $document->getReference();
49+
50+
return $defaults;
5051
}
5152

5253
}

Routing/RouteObjectInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\ChainRoutingBundle\Routing;
4+
5+
interface RouteObjectInterface
6+
{
7+
function getReference();
8+
9+
function getRouteDefaults();
10+
}

Tests/Controller/DocumentControllerResolverTest.php renamed to Tests/Controller/ControllerResolverTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
namespace Symfony\Cmf\Bundle\ChainRoutingBundle\Tests\Controller;
44

55
use Symfony\Cmf\Bundle\ChainRoutingBundle\Test\CmfUnitTestCase;
6-
use Symfony\Cmf\Bundle\ChainRoutingBundle\Controller\DocumentControllerResolver;
6+
use Symfony\Cmf\Bundle\ChainRoutingBundle\Controller\ControllerResolver;
77

8-
class DocumentControllerResolverTest extends CmfUnitTestCase
8+
class ControllerResolverTest extends CmfUnitTestCase
99
{
10-
1110
public function setUp()
1211
{
1312
$this->document = $this->buildMock('Document', array('getController'));
1413
$mapping = array('static_pages' => 'symfony_cmf_content.controller:indexAction');
1514

16-
$this->resolver = new DocumentControllerResolver($mapping);
15+
$this->resolver = new ControllerResolver($mapping);
1716
}
1817

1918
public function testControllerFoundInMapping()
@@ -33,5 +32,4 @@ public function testControllerNotFoundInMapping()
3332

3433
$this->assertEquals(null, $this->resolver->getController($this->document));
3534
}
36-
3735
}

Tests/Routing/ContentRouterTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77

88
class ContentRouterTest extends CmfUnitTestCase
99
{
10-
1110
public function setUp()
1211
{
1312
$this->node = $this->buildMock('Navigation', array('getReference'));
1413
$this->document = $this->buildMock('Document');
1514
$this->loader_interface = $this->buildMock("\Symfony\Component\Config\Loader\LoaderInterface");
16-
$this->document_manager = $this->buildMock("\Doctrine\ODM\PHPCR\DocumentManager", array('find'));
17-
$this->controller_resolver = $this->buildMock('\Symfony\Cmf\Bundle\ChainRoutingBundle\Controller\DocumentControllerResolver', array('getController'));
15+
$this->object_manager = $this->buildMock("\Doctrine\Common\Persistence\ObjectManager", array('find'));
16+
$this->controller_resolver = $this->buildMock('\Symfony\Cmf\Bundle\ChainRoutingBundle\Controller\ControllerResolver', array('getController'));
1817

1918
$this->router = new ContentRouter($this->loader_interface, array());
20-
$this->router->setDocumentManager($this->document_manager);
19+
$this->router->setObjectManager($this->object_manager);
2120
$this->router->setControllerResolver($this->controller_resolver);
2221
}
2322

@@ -29,7 +28,7 @@ public function testMatch()
2928
->method('getReference')
3029
->will($this->returnValue($this->document));
3130

32-
$this->document_manager->expects($this->once())
31+
$this->object_manager->expects($this->once())
3332
->method('find')
3433
->with(null, $url_alias)
3534
->will($this->returnValue($this->node));
@@ -49,7 +48,7 @@ public function testNoReferenceMatch()
4948
->method('getReference')
5049
->will($this->returnValue(null));
5150

52-
$this->document_manager->expects($this->once())
51+
$this->object_manager->expects($this->once())
5352
->method('find')
5453
->with(null, $url_alias)
5554
->will($this->returnValue($this->node));
@@ -61,12 +60,11 @@ public function testNoNodeMatch()
6160
{
6261
$url_alias = "/company/more_no_match";
6362

64-
$this->document_manager->expects($this->once())
63+
$this->object_manager->expects($this->once())
6564
->method('find')
6665
->with(null, $url_alias)
6766
->will($this->returnValue(null));
6867

6968
$this->assertFalse($this->router->match($url_alias));
7069
}
71-
7270
}

0 commit comments

Comments
 (0)