Skip to content

Commit fcabe73

Browse files
committed
Merge pull request #186 from symfony-cmf/optimize_get_route_by_name
filter routes with the id prefix before doing lookups
2 parents 3082ce5 + b9b6e1a commit fcabe73

File tree

3 files changed

+129
-3
lines changed

3 files changed

+129
-3
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
1.1.0-RC8
5+
---------
6+
7+
* **2013-10-08**: The idPrefix is now used as a filter in getRouteByName() and getRoutesByNames()
8+
in the PHPCR RouteProvider. This means its no longer possible to get routes that are not children
9+
of a path that begins with idPrefix
10+
411
1.1.0-RC5
512
---------
613

Doctrine/Phpcr/RouteProvider.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ protected function getCandidates($url)
120120
public function getRouteByName($name, $parameters = array())
121121
{
122122
// $name is the route document path
123-
$route = $this->getObjectManager()->find($this->className, $name);
123+
if ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) {
124+
$route = $this->getObjectManager()->find($this->className, $name);
125+
}
124126

125-
if (!$route) {
127+
if (empty($route)) {
126128
throw new RouteNotFoundException(sprintf('No route found for path "%s"', $name));
127129
}
128130

@@ -138,6 +140,14 @@ public function getRouteByName($name, $parameters = array())
138140
*/
139141
public function getRoutesByNames($names, $parameters = array())
140142
{
143+
if ('' !== $this->idPrefix) {
144+
foreach ($names as $key => $name) {
145+
if (0 !== strpos($name, $this->idPrefix)) {
146+
unset($names[$key]);
147+
}
148+
}
149+
}
150+
141151
$collection = $this->getObjectManager()->findMany($this->className, $names);
142152
foreach ($collection as $key => $document) {
143153
if (!$document instanceof SymfonyRoute) {

Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php

+110-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function testGetRouteByName()
6666
$routeProvider = new RouteProvider($this->managerRegistry);
6767
$routeProvider->setManagerName('default');
6868

69+
$routeProvider->setPrefix('/cms/routes/');
6970
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
7071

7172
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
@@ -92,7 +93,7 @@ public function testGetRouteByNameNotFound()
9293

9394
$routeProvider = new RouteProvider($this->managerRegistry);
9495
$routeProvider->setManagerName('default');
95-
96+
$routeProvider->setPrefix('/cms/routes/');
9697
$routeProvider->getRouteByName('/cms/routes/test-route');
9798
}
9899

@@ -116,6 +117,112 @@ public function testGetRouteByNameNoRoute()
116117

117118
$routeProvider = new RouteProvider($this->managerRegistry);
118119
$routeProvider->setManagerName('default');
120+
$routeProvider->setPrefix('/cms/routes/');
121+
$routeProvider->getRouteByName('/cms/routes/test-route');
122+
}
123+
124+
/**
125+
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
126+
*/
127+
public function testGetRouteByNameInvalidRoute()
128+
{
129+
$this->objectManager
130+
->expects($this->never())
131+
->method('find')
132+
;
133+
134+
$this->managerRegistry
135+
->expects($this->any())
136+
->method('getManager')
137+
->will($this->returnValue($this->objectManager))
138+
;
139+
140+
$routeProvider = new RouteProvider($this->managerRegistry);
141+
$routeProvider->setManagerName('default');
142+
143+
$routeProvider->setPrefix('/cms/routes');
144+
145+
$routeProvider->getRouteByName('invalid_route');
146+
}
147+
148+
public function testGetRouteByNameIdPrefixEmptyString()
149+
{
150+
151+
$this->route
152+
->expects($this->any())
153+
->method('getPath')
154+
->will($this->returnValue('/cms/routes/test-route'))
155+
;
156+
157+
$this->objectManager
158+
->expects($this->any())
159+
->method('find')
160+
->with(null, '/cms/routes/test-route')
161+
->will($this->returnValue($this->route))
162+
;
163+
164+
$this->managerRegistry
165+
->expects($this->any())
166+
->method('getManager')
167+
->will($this->returnValue($this->objectManager))
168+
;
169+
170+
$routeProvider = new RouteProvider($this->managerRegistry);
171+
$routeProvider->setManagerName('default');
172+
173+
$routeProvider->setPrefix('');
174+
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
175+
176+
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
177+
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
178+
}
179+
180+
181+
/**
182+
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
183+
*/
184+
public function testGetRouteByNameNotFoundIdPrefixEmptyString()
185+
{
186+
$this->objectManager
187+
->expects($this->any())
188+
->method('find')
189+
->with(null, '/cms/routes/test-route')
190+
->will($this->returnValue(null))
191+
;
192+
193+
$this->managerRegistry
194+
->expects($this->any())
195+
->method('getManager')
196+
->will($this->returnValue($this->objectManager))
197+
;
198+
199+
$routeProvider = new RouteProvider($this->managerRegistry);
200+
$routeProvider->setManagerName('default');
201+
$routeProvider->setPrefix('');
202+
$routeProvider->getRouteByName('/cms/routes/test-route');
203+
}
204+
205+
/**
206+
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
207+
*/
208+
public function testGetRouteByNameNoRoutePrefixEmptyString()
209+
{
210+
$this->objectManager
211+
->expects($this->any())
212+
->method('find')
213+
->with(null, '/cms/routes/test-route')
214+
->will($this->returnValue($this))
215+
;
216+
217+
$this->managerRegistry
218+
->expects($this->any())
219+
->method('getManager')
220+
->will($this->returnValue($this->objectManager))
221+
;
222+
223+
$routeProvider = new RouteProvider($this->managerRegistry);
224+
$routeProvider->setManagerName('default');
225+
$routeProvider->setPrefix('');
119226

120227
$routeProvider->getRouteByName('/cms/routes/test-route');
121228
}
@@ -173,6 +280,8 @@ function ($name) use ($objectManagers) {
173280
$routeProvider = new RouteProvider($this->managerRegistry);
174281

175282
$routeProvider->setManagerName('default');
283+
$routeProvider->setPrefix('/cms/routes/');
284+
176285
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
177286
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
178287
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());

0 commit comments

Comments
 (0)