Skip to content

Commit f78ebd4

Browse files
committed
Merge pull request #200 from symfony-cmf/route_collection
updated for RouteCollection support
2 parents aff34f0 + b5d7948 commit f78ebd4

File tree

10 files changed

+91
-7
lines changed

10 files changed

+91
-7
lines changed

DependencyInjection/CmfRoutingExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private function setupDynamicRouter(array $config, ContainerBuilder $container,
9292
$container->setParameter($this->getAlias() . '.controllers_by_class', $config['controllers_by_class']);
9393
$container->setParameter($this->getAlias() . '.templates_by_class', $config['templates_by_class']);
9494
$container->setParameter($this->getAlias() . '.uri_filter_regexp', $config['uri_filter_regexp']);
95+
$container->setParameter($this->getAlias() . '.route_collection_limit', $config['route_collection_limit']);
9596

9697
$locales = false;
9798
if (isset($config['locales'])) {

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function getConfigTreeBuilder()
5656
->addDefaultsIfNotSet()
5757
->canBeEnabled()
5858
->children()
59+
->scalarNode('route_collection_limit')->defaultNull()->end()
5960
->scalarNode('generic_controller')->defaultNull()->end()
6061
->scalarNode('default_controller')->defaultNull()->end()
6162
->arrayNode('controllers_by_type')

Doctrine/DoctrineProvider.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,16 @@ abstract class DoctrineProvider
4444
*/
4545
protected $className;
4646

47+
/**
48+
* Limit to apply when calling getRoutesByNames() with null
49+
*
50+
* @var integer|null
51+
*/
52+
protected $routeCollectionLimit;
53+
4754
/**
4855
* @param ManagerRegistry $managerRegistry
56+
* @param string $className
4957
*/
5058
public function __construct(ManagerRegistry $managerRegistry, $className = null)
5159
{
@@ -64,6 +72,17 @@ public function setManagerName($managerName)
6472
$this->managerName = $managerName;
6573
}
6674

75+
/**
76+
* Set the limit to apply when calling getRoutesByNames() with null.
77+
* Note that setting the limit to null means no limit applied.
78+
*
79+
* @param integer|null $routeCollectionLimit
80+
*/
81+
public function setRouteCollectionLimit($routeCollectionLimit = null)
82+
{
83+
$this->routeCollectionLimit = $routeCollectionLimit;
84+
}
85+
6786
/**
6887
* Get the object manager named $managerName from the registry.
6988
*

Doctrine/Orm/RouteProvider.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,20 @@ public function getRouteByName($name, $parameters = array())
7373
/**
7474
* {@inheritDoc}
7575
*/
76-
public function getRoutesByNames($names, $parameters = array())
76+
public function getRoutesByNames($names = null, $parameters = array())
7777
{
78+
if (null === $names) {
79+
$names = array();
80+
if (0 === $this->routeCollectionLimit) {
81+
return $names;
82+
}
83+
if (null !== $this->routeCollectionLimit) {
84+
return $this->getRoutesRepository()->findBy(array(), null, $this->routeCollectionLimit);
85+
}
86+
87+
return $this->getRoutesRepository()->findAll();
88+
}
89+
7890
$routes = array();
7991
foreach ($names as $name) {
8092
try {

Doctrine/Phpcr/RouteProvider.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212

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

15+
use Doctrine\ODM\PHPCR\DocumentManager;
16+
17+
use PHPCR\Query\QueryInterface;
1518
use PHPCR\RepositoryException;
1619

20+
use PHPCR\Query\RowInterface;
21+
1722
use Symfony\Component\Routing\Route as SymfonyRoute;
1823
use Symfony\Component\Routing\RouteCollection;
1924
use Symfony\Component\Routing\Exception\RouteNotFoundException;
@@ -70,7 +75,9 @@ public function getRouteCollectionForRequest(Request $request)
7075
}
7176

7277
try {
73-
$routes = $this->getObjectManager()->findMany($this->className, $candidates);
78+
/** @var $dm DocumentManager */
79+
$dm = $this->getObjectManager();
80+
$routes = $dm->findMany($this->className, $candidates);
7481
// filter for valid route objects
7582
foreach ($routes as $key => $route) {
7683
if ($route instanceof SymfonyRoute) {
@@ -135,11 +142,50 @@ public function getRouteByName($name, $parameters = array())
135142
return $route;
136143
}
137144

145+
/**
146+
* Get list of route names
147+
*
148+
* @return array
149+
*/
150+
private function getRouteNames()
151+
{
152+
if (0 === $this->routeCollectionLimit) {
153+
return array();
154+
}
155+
156+
/** @var $dm DocumentManager */
157+
$dm = $this->getObjectManager();
158+
$sql2 = 'SELECT * FROM [nt:unstructured] WHERE [phpcr:classparents] = '.$dm->quote('Symfony\Component\Routing\Route');
159+
160+
if ('' !== $this->idPrefix) {
161+
$sql2.= ' AND ISDESCENDANTNODE('.$dm->quote($this->idPrefix).')';
162+
}
163+
164+
$query = $dm->createPhpcrQuery($sql2, QueryInterface::JCR_SQL2);
165+
if (null !== $this->routeCollectionLimit) {
166+
$query->setLimit($this->routeCollectionLimit);
167+
}
168+
169+
$result = $query->execute();
170+
171+
$names = array();
172+
foreach ($result as $row) {
173+
/** @var $row RowInterface */
174+
$names[] = $row->getPath();
175+
}
176+
177+
return $names;
178+
}
179+
138180
/**
139181
* {@inheritDoc}
140182
*/
141-
public function getRoutesByNames($names, $parameters = array())
183+
public function getRoutesByNames($names = null, $parameters = array())
142184
{
185+
if (null === $names) {
186+
$names = $this->getRouteNames();
187+
}
188+
143189
if ('' !== $this->idPrefix) {
144190
foreach ($names as $key => $name) {
145191
if (0 !== strpos($name, $this->idPrefix)) {
@@ -148,7 +194,9 @@ public function getRoutesByNames($names, $parameters = array())
148194
}
149195
}
150196

151-
$collection = $this->getObjectManager()->findMany($this->className, $names);
197+
/** @var $dm DocumentManager */
198+
$dm = $this->getObjectManager();
199+
$collection = $dm->findMany($this->className, $names);
152200
foreach ($collection as $key => $document) {
153201
if (!$document instanceof SymfonyRoute) {
154202
// we follow the logic of DocumentManager::findMany and do not throw an exception
@@ -158,5 +206,4 @@ public function getRoutesByNames($names, $parameters = array())
158206

159207
return $collection;
160208
}
161-
162209
}

Resources/config/provider-orm.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<argument type="service" id="doctrine"/>
2121
<argument>%cmf_routing.route_entity.class%</argument>
2222
<call method="setManagerName"><argument>%cmf_routing.dynamic.persistence.orm.manager_name%</argument></call>
23+
<call method="setRouteCollectionLimit"><argument>%cmf_routing.route_collection_limit%</argument></call>
2324
</service>
2425
</services>
2526
</container>

Resources/config/provider-phpcr.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<argument>%cmf_routing.route_model.class%</argument>
1818
<call method="setManagerName"><argument>%cmf_routing.dynamic.persistence.phpcr.manager_name%</argument></call>
1919
<call method="setPrefix"><argument>%cmf_routing.dynamic.persistence.phpcr.route_basepath%</argument></call>
20+
<call method="setRouteCollectionLimit"><argument>%cmf_routing.route_collection_limit%</argument></call>
2021
</service>
2122

2223
<service id="cmf_routing.phpcr_content_repository" class="%cmf_routing.content_repository.class%">

Resources/config/routing-dynamic.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<argument type="service" id="cmf_routing.generator" />
6868
<argument>%cmf_routing.uri_filter_regexp%</argument>
6969
<argument type="service" id="event_dispatcher" on-invalid="ignore"/>
70+
<argument type="service" id="cmf_routing.route_provider"/>
7071
<call method="setContainer"><argument type="service" id="service_container"/></call>
7172
<call method="addRouteEnhancer">
7273
<argument type="service" id="cmf_routing.enhancer.route_content"/>

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function testSupportsAllConfigFormats()
3939
'replace_symfony_router' => true,
4040
),
4141
'dynamic' => array(
42+
'route_collection_limit' => null,
4243
'generic_controller' => 'acme_main.controller:mainAction',
4344
'controllers_by_type' => array(
4445
'editable' => 'acme_main.some_controller:editableAction',

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"prefer-stable": false,
1616
"require": {
1717
"php": ">=5.3.3",
18-
"symfony-cmf/routing": "1.1.*",
18+
"symfony-cmf/routing": "~1.2.0",
1919
"symfony/framework-bundle": "~2.2"
2020
},
2121
"require-dev": {
@@ -43,7 +43,7 @@
4343
"target-dir": "Symfony/Cmf/Bundle/RoutingBundle",
4444
"extra": {
4545
"branch-alias": {
46-
"dev-master": "1.1-dev"
46+
"dev-master": "1.2-dev"
4747
}
4848
}
4949
}

0 commit comments

Comments
 (0)