Skip to content

Commit b5d7948

Browse files
committed
make it possible to limit the size of the dynamic RouteCollection
1 parent d8558f5 commit b5d7948

File tree

9 files changed

+54
-4
lines changed

9 files changed

+54
-4
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public function getRouteByName($name, $parameters = array())
7676
public function getRoutesByNames($names = null, $parameters = array())
7777
{
7878
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+
7987
return $this->getRoutesRepository()->findAll();
8088
}
8189

Doctrine/Phpcr/RouteProvider.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212

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

15+
use Doctrine\ODM\PHPCR\DocumentManager;
16+
1517
use PHPCR\Query\QueryInterface;
1618
use PHPCR\RepositoryException;
1719

20+
use PHPCR\Query\RowInterface;
21+
1822
use Symfony\Component\Routing\Route as SymfonyRoute;
1923
use Symfony\Component\Routing\RouteCollection;
2024
use Symfony\Component\Routing\Exception\RouteNotFoundException;
@@ -71,7 +75,9 @@ public function getRouteCollectionForRequest(Request $request)
7175
}
7276

7377
try {
74-
$routes = $this->getObjectManager()->findMany($this->className, $candidates);
78+
/** @var $dm DocumentManager */
79+
$dm = $this->getObjectManager();
80+
$routes = $dm->findMany($this->className, $candidates);
7581
// filter for valid route objects
7682
foreach ($routes as $key => $route) {
7783
if ($route instanceof SymfonyRoute) {
@@ -143,6 +149,11 @@ public function getRouteByName($name, $parameters = array())
143149
*/
144150
private function getRouteNames()
145151
{
152+
if (0 === $this->routeCollectionLimit) {
153+
return array();
154+
}
155+
156+
/** @var $dm DocumentManager */
146157
$dm = $this->getObjectManager();
147158
$sql2 = 'SELECT * FROM [nt:unstructured] WHERE [phpcr:classparents] = '.$dm->quote('Symfony\Component\Routing\Route');
148159

@@ -151,10 +162,15 @@ private function getRouteNames()
151162
}
152163

153164
$query = $dm->createPhpcrQuery($sql2, QueryInterface::JCR_SQL2);
165+
if (null !== $this->routeCollectionLimit) {
166+
$query->setLimit($this->routeCollectionLimit);
167+
}
168+
154169
$result = $query->execute();
155170

156171
$names = array();
157172
foreach ($result as $row) {
173+
/** @var $row RowInterface */
158174
$names[] = $row->getPath();
159175
}
160176

@@ -178,7 +194,9 @@ public function getRoutesByNames($names = null, $parameters = array())
178194
}
179195
}
180196

181-
$collection = $this->getObjectManager()->findMany($this->className, $names);
197+
/** @var $dm DocumentManager */
198+
$dm = $this->getObjectManager();
199+
$collection = $dm->findMany($this->className, $names);
182200
foreach ($collection as $key => $document) {
183201
if (!$document instanceof SymfonyRoute) {
184202
// we follow the logic of DocumentManager::findMany and do not throw an exception

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%">

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)