Skip to content

Commit 78266bb

Browse files
committed
Merge pull request #165 from symfony-cmf/format-handling
fix format handling
2 parents c7ab6d7 + fb46282 commit 78266bb

File tree

6 files changed

+127
-18
lines changed

6 files changed

+127
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Changelog
44
1.1.0-RC3
55
---------
66

7+
* **2013-09-04**: If the route matched a pattern with a format extension, the format
8+
extension is no longer set as route a default
79
* **2013-09-04**: Marked ContentAwareGenerator as obsolete, use ContentAwareGenerator
810
from the CMF routing component directly instead. This class will be removed in 1.2
911
* **2013-08-09**: dynamic.generic_controller is now defaulting to null instead

Doctrine/Phpcr/RouteProvider.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ public function getRouteCollectionForRequest(Request $request)
6161
// filter for valid route objects
6262
foreach ($routes as $key => $route) {
6363
if ($route instanceof SymfonyRoute) {
64-
if (preg_match('/.+\.([a-z]+)$/i', $url, $matches)) {
65-
if ($route->getDefault('_format') === $matches[1]) {
66-
continue;
67-
}
68-
69-
$route->setDefault('_format', $matches[1]);
70-
}
7164
$collection->add($key, $route);
7265
}
7366
}
@@ -87,13 +80,13 @@ protected function getCandidates($url)
8780
$candidates = array();
8881
if ('/' !== $url) {
8982
if (preg_match('/(.+)\.[a-z]+$/i', $url, $matches)) {
90-
$candidates[] = $this->idPrefix.$url;
83+
$candidates[] = $this->idPrefix . $url;
9184
$url = $matches[1];
9285
}
9386

9487
$part = $url;
9588
while (false !== ($pos = strrpos($part, '/'))) {
96-
$candidates[] = $this->idPrefix.$part;
89+
$candidates[] = $this->idPrefix . $part;
9790
$part = substr($url, 0, $pos);
9891
}
9992
}
@@ -114,16 +107,28 @@ public function getRouteByName($name, $parameters = array())
114107
if (!$route) {
115108
throw new RouteNotFoundException(sprintf('No route found for path "%s"', $name));
116109
}
110+
117111
if (!$route instanceof SymfonyRoute) {
118112
throw new RouteNotFoundException(sprintf('Document at path "%s" is no route', $name));
119113
}
120114

121115
return $route;
122116
}
123117

118+
/**
119+
* {@inheritDoc}
120+
*/
124121
public function getRoutesByNames($names, $parameters = array())
125122
{
126-
return $this->getObjectManager()->findMany($this->className, $names);
123+
$collection = $this->getObjectManager()->findMany($this->className, $names);
124+
foreach ($collection as $key => $document) {
125+
if (!$document instanceof SymfonyRoute) {
126+
// we follow the logic of DocumentManager::findMany and do not throw an exception
127+
unset($collection[$key]);
128+
}
129+
}
130+
131+
return $collection;
127132
}
128133

129134
}

Tests/Functional/BaseTestCase.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,29 @@
22

33
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional;
44

5+
use Doctrine\ODM\PHPCR\DocumentManager;
6+
use PHPCR\Util\PathHelper;
57
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
6-
78
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase as ComponentBaseTestCase;
8-
use PHPCR\Util\PathHelper;
99
use Symfony\Cmf\Component\Testing\Document\Content;
1010

1111
class BaseTestCase extends ComponentBaseTestCase
1212
{
13+
/**
14+
* @return DocumentManager
15+
*/
1316
protected function getDm()
1417
{
1518
$dm = $this->db('PHPCR')->getOm();
1619

1720
return $dm;
1821
}
1922

23+
/**
24+
* @param string $path
25+
*
26+
* @return Route
27+
*/
2028
protected function createRoute($path)
2129
{
2230
$parentPath = PathHelper::getParentPath($path);
@@ -30,6 +38,11 @@ protected function createRoute($path)
3038
return $route;
3139
}
3240

41+
/**
42+
* @param string $path
43+
*
44+
* @return Content
45+
*/
3346
protected function createContent($path)
3447
{
3548
$content = new Content;

Tests/Functional/Doctrine/Phpcr/RouteProviderTest.php

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ public function setUp()
2525
$this->repository = $this->getContainer()->get('cmf_routing.route_provider');
2626
}
2727

28-
public function testGetRouteCollectionForRequest()
28+
private function buildRoutes()
2929
{
30-
$route = new Route;
3130
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
3231

32+
$route = new Route;
3333
$route->setPosition($root, 'testroute');
34+
$route->setDefault('_format', 'html');
3435
$this->getDm()->persist($route);
3536

3637
// smuggle a non-route thing into the repository
@@ -41,24 +42,76 @@ public function testGetRouteCollectionForRequest()
4142

4243
$childroute = new Route;
4344
$childroute->setPosition($noroute, 'child');
45+
$childroute->setDefault('_format', 'json');
4446
$this->getDm()->persist($childroute);
4547

4648
$this->getDm()->flush();
47-
4849
$this->getDm()->clear();
50+
}
51+
52+
public function testGetRouteCollectionForRequest()
53+
{
54+
$this->buildRoutes();
4955

5056
$routes = $this->repository->getRouteCollectionForRequest(Request::create('/testroute/noroute/child'));
5157
$this->assertCount(3, $routes);
5258
$this->assertContainsOnlyInstancesOf('Symfony\\Cmf\\Component\\Routing\\RouteObjectInterface', $routes);
59+
60+
$routes = $routes->all();
61+
list($key, $child) = each($routes);
62+
$this->assertEquals(self::ROUTE_ROOT . '/testroute/noroute/child', $key);
63+
$this->assertEquals('json', $child->getDefault('_format'));
64+
list($key, $testroute) = each($routes);
65+
$this->assertEquals(self::ROUTE_ROOT . '/testroute', $key);
66+
$this->assertEquals('html', $testroute->getDefault('_format'));
67+
list($key, $root) = each($routes);
68+
$this->assertEquals(self::ROUTE_ROOT, $key);
69+
$this->assertNull($root->getDefault('_format'));
5370
}
5471

55-
public function testFindNophpcrUrl()
72+
public function testGetRouteCollectionForRequestFormat()
73+
{
74+
$this->buildRoutes();
75+
76+
$routes = $this->repository->getRouteCollectionForRequest(Request::create('/testroute/noroute/child.html'));
77+
$this->assertCount(3, $routes);
78+
$this->assertContainsOnlyInstancesOf('Symfony\\Cmf\\Component\\Routing\\RouteObjectInterface', $routes);
79+
80+
$routes = $routes->all();
81+
list($key, $child) = each($routes);
82+
$this->assertEquals(self::ROUTE_ROOT . '/testroute/noroute/child', $key);
83+
$this->assertEquals('json', $child->getDefault('_format'));
84+
list($key, $testroute) = each($routes);
85+
$this->assertEquals(self::ROUTE_ROOT . '/testroute', $key);
86+
$this->assertEquals('html', $testroute->getDefault('_format'));
87+
list($key, $root) = each($routes);
88+
$this->assertEquals(self::ROUTE_ROOT, $key);
89+
$this->assertEquals(null, $root->getDefault('_format'));
90+
}
91+
92+
public function testGetRouteCollectionForRequestNophpcrUrl()
5693
{
5794
$collection = $this->repository->getRouteCollectionForRequest(Request::create(':///'));
5895
$this->assertInstanceOf('Symfony\\Component\\Routing\\RouteCollection', $collection);
5996
$this->assertCount(0, $collection);
6097
}
6198

99+
public function testGetRoutesByNames()
100+
{
101+
$this->buildRoutes();
102+
103+
$routeNames = array(
104+
self::ROUTE_ROOT . '/testroute/noroute/child',
105+
self::ROUTE_ROOT . '/testroute/noroute',
106+
self::ROUTE_ROOT . '/testroute'
107+
);
108+
109+
$routes = $this->repository->getRoutesByNames($routeNames);
110+
$this->assertCount(2, $routes);
111+
$this->assertContainsOnlyInstancesOf('Symfony\\Cmf\\Component\\Routing\\RouteObjectInterface', $routes);
112+
}
113+
114+
62115
public function testSetPrefix()
63116
{
64117
$this->repository->setPrefix(self::ROUTE_ROOT);

Tests/Functional/Routing/DynamicRouterTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ public function setUp()
6060
$formatroute->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
6161
$this->getDm()->persist($formatroute);
6262

63+
$format2jsonroute = new Route(true);
64+
$format2jsonroute->setPosition($root, 'format2.json');
65+
$format2jsonroute->setDefault('_format', 'json');
66+
$format2jsonroute->setRequirement('_format', 'json');
67+
$format2jsonroute->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testJsonController');
68+
$this->getDm()->persist($format2jsonroute);
69+
70+
$format2route = new Route(true);
71+
$format2route->setPosition($root, 'format2');
72+
$format2route->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
73+
$this->getDm()->persist($format2route);
74+
6375
$this->getDm()->flush();
6476
}
6577

@@ -153,6 +165,30 @@ public function testMatchFormat()
153165

154166
$this->assertTrue($request->attributes->has(DynamicRouter::ROUTE_KEY));
155167
$this->assertEquals($expected, $matches);
168+
169+
$expected = array(
170+
'_controller' => 'testController',
171+
'_format' => 'html',
172+
RouteObjectInterface::ROUTE_NAME => '/test/routing/format2',
173+
);
174+
$request = Request::create('/format2.html');
175+
$matches = $this->router->matchRequest($request);
176+
ksort($matches);
177+
178+
$this->assertTrue($request->attributes->has(DynamicRouter::ROUTE_KEY));
179+
$this->assertEquals($expected, $matches);
180+
181+
$expected = array(
182+
'_controller' => 'testJsonController',
183+
'_format' => 'json',
184+
RouteObjectInterface::ROUTE_NAME => '/test/routing/format2.json',
185+
);
186+
$request = Request::create('/format2.json');
187+
$matches = $this->router->matchRequest($request);
188+
ksort($matches);
189+
190+
$this->assertTrue($request->attributes->has(DynamicRouter::ROUTE_KEY));
191+
$this->assertEquals($expected, $matches);
156192
}
157193

158194
/**

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"symfony/framework-bundle": "~2.2"
2020
},
2121
"require-dev": {
22-
"symfony-cmf/core-bundle": "~1.0.0-alpha1",
22+
"symfony-cmf/core-bundle": "1.0.*",
2323
"symfony-cmf/testing": "1.0.*",
2424
"sonata-project/doctrine-phpcr-admin-bundle": "1.0.*",
2525
"symfony/monolog-bundle": "2.2.*",
@@ -31,7 +31,7 @@
3131
"doctrine/orm": "To enable support for the ORM entities",
3232

3333
"symfony-cmf/content-bundle": "To optionally use the configured value for 'content_basepath' from the CoreBundle",
34-
"symfony-cmf/core-bundle": "For compatibility with Symfony 2.2",
34+
"symfony-cmf/core-bundle": "For compatibility with Symfony 2.2 and easier configuration",
3535

3636
"sonata-project/doctrine-phpcr-admin-bundle": "To provide an admin interface for the PHPCR ODM documents"
3737
},

0 commit comments

Comments
 (0)