Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 2897c54

Browse files
committed
Support for multiple routes
1 parent c1bb5a4 commit 2897c54

File tree

6 files changed

+92
-11
lines changed

6 files changed

+92
-11
lines changed

Adapter/PhpcrOdmAdapter.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoR
119119
$basePath
120120
));
121121
}
122-
123122
$segments = preg_split('#/#', $uriContext->getUri(), null, PREG_SPLIT_NO_EMPTY);
124123
$headName = array_pop($segments);
125124
foreach ($segments as $segment) {
@@ -165,6 +164,10 @@ public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoR
165164
$headRoute->setAutoRouteTag($autoRouteTag);
166165
$headRoute->setType(AutoRouteInterface::TYPE_PRIMARY);
167166

167+
foreach ($uriContext->getDefaults() as $key => $value) {
168+
$headRoute->setDefault($key, $value);
169+
}
170+
168171
return $headRoute;
169172
}
170173

Resources/config/auto_route.xml

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<parameter key="cmf_routing_auto.auto_route_manager.class">Symfony\Cmf\Component\RoutingAuto\AutoRouteManager</parameter>
1010
<parameter key="cmf_routing_auto.service_registry.class">Symfony\Cmf\Component\RoutingAuto\ServiceRegistry</parameter>
1111
<parameter key="cmf_routing_auto.uri_generator.class">Symfony\Cmf\Component\RoutingAuto\UriGenerator</parameter>
12+
<parameter key="cmf_routing_auto.uri_context_collection_builder.class">Symfony\Cmf\Component\RoutingAuto\UriContextCollectionBuilder</parameter>
1213

1314
<parameter key="cmf_routing_auto.metadata.loader.yaml.class">Symfony\Cmf\Component\RoutingAuto\Mapping\Loader\YmlFileLoader</parameter>
1415
<parameter key="cmf_routing_auto.metadata.loader.xml.class">Symfony\Cmf\Component\RoutingAuto\Mapping\Loader\XmlFileLoader</parameter>
@@ -37,13 +38,17 @@
3738
<argument type="service" id="cmf_routing_auto.adapter" />
3839
<argument type="service" id="cmf_routing_auto.uri_generator" />
3940
<argument type="service" id="cmf_routing_auto.defunct_route_handler.delegating" />
41+
<argument type="service" id="cmf_routing_auto.uri_context_collection_builder" />
4042
</service>
4143

4244
<!-- URL Generator -->
4345
<service id="cmf_routing_auto.uri_generator" class="%cmf_routing_auto.uri_generator.class%">
46+
<argument type="service" id="cmf_routing_auto.service_registry" />
47+
</service>
48+
49+
<service id="cmf_routing_auto.uri_context_collection_builder" class="%cmf_routing_auto.uri_context_collection_builder.class%">
4450
<argument type="service" id="cmf_routing_auto.metadata.factory" />
4551
<argument type="service" id="cmf_routing_auto.adapter" />
46-
<argument type="service" id="cmf_routing_auto.service_registry" />
4752
</service>
4853

4954
<!-- Service Registry -->
@@ -84,8 +89,7 @@
8489
<service id="cmf_routing_auto.metadata.factory"
8590
class="%cmf_routing_auto.metadata.factory.class%"
8691
factory-service="cmf_routing_auto.metadata.factory.builder"
87-
factory-method="getMetadataFactory"
88-
/>
92+
factory-method="getMetadataFactory" />
8993

9094
<!-- Controller -->
9195
<service

Tests/Functional/EventListener/AutoRouteListenerTest.php

+26-4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ protected function createBlog($withPosts = false)
4646
$this->getDm()->clear();
4747
}
4848

49+
/**
50+
* It should persist the blog document and create an auto route.
51+
* It should set the defaults on the route.
52+
*/
4953
public function testPersistBlog()
5054
{
5155
$this->createBlog();
@@ -62,6 +66,11 @@ public function testPersistBlog()
6266
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $routes[0]);
6367
$this->assertEquals('unit-testing-blog', $routes[0]->getName());
6468
$this->assertEquals(PhpcrOdmAdapter::TAG_NO_MULTILANG, $routes[0]->getAutoRouteTag());
69+
$this->assertEquals(array(
70+
'_auto_route_tag' => 'no-multilang',
71+
'type' => 'cmf_routing_auto.primary',
72+
'_controller' => 'BlogController',
73+
), $routes[0]->getDefaults());
6574
}
6675

6776
public function provideTestUpdateBlog()
@@ -206,6 +215,16 @@ public function provideMultilangArticle()
206215
'test/auto-route/articles/fr/bonjour-le-monde',
207216
'test/auto-route/articles/de/gutentag',
208217
'test/auto-route/articles/es/hola-todo-el-mundo',
218+
219+
'test/auto-route/articles/en/hello-everybody-edit',
220+
'test/auto-route/articles/fr/bonjour-le-monde-edit',
221+
'test/auto-route/articles/de/gutentag-edit',
222+
'test/auto-route/articles/es/hola-todo-el-mundo-edit',
223+
224+
'test/auto-route/articles/en/hello-everybody-review',
225+
'test/auto-route/articles/fr/bonjour-le-monde-review',
226+
'test/auto-route/articles/de/gutentag-review',
227+
'test/auto-route/articles/es/hola-todo-el-mundo-review',
209228
),
210229
),
211230
);
@@ -231,11 +250,12 @@ public function testMultilangArticle($data, $expectedPaths)
231250
$locales = array_keys($data);
232251

233252
foreach ($expectedPaths as $i => $expectedPath) {
234-
$expectedLocale = $locales[$i];
253+
$localeIndex = $i % count($locales);
254+
$expectedLocale = $locales[$localeIndex];
235255

236256
$route = $this->getDm()->find(null, $expectedPath);
237257

238-
$this->assertNotNull($route);
258+
$this->assertNotNull($route, 'Route: '.$expectedPath);
239259
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $route);
240260
$this->assertEquals($expectedLocale, $route->getAutoRouteTag());
241261

@@ -317,11 +337,13 @@ public function testUpdateMultilangArticle($data, $expectedPaths)
317337

318338
$article_de = $this->getDm()->findTranslation('Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Article', '/test/article-1', 'de');
319339
$routes = $this->getDm()->getReferrers($article_de);
320-
$this->assertCount(count($data), $routes);
340+
341+
// Multiply the expected paths by 3 because Article has 3 routes defined.
342+
$this->assertCount(count($data) * 3, $routes);
321343

322344
$this->getDm()->clear();
323345

324-
foreach ($expectedPaths as $i => $expectedPath) {
346+
foreach ($expectedPaths as $expectedPath) {
325347
$route = $this->getDm()->find(null, $expectedPath);
326348

327349
$this->assertNotNull($route);

Tests/Resources/app/config/routing_auto.yml

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Blog:
2-
uri_schema: /blog/{blog_title}
2+
uri_schema:
3+
frontend:
4+
uri_schema: /blog/{blog_title}
5+
defaults:
6+
_controller: BlogController
37
token_providers:
48
blog_title: [content_method, { method: getTitle } ]
59

@@ -12,7 +16,19 @@ Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Post:
1216
post_title: [content_method, { method: getTitle } ]
1317

1418
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Article:
15-
uri_schema: /articles/{article_locale}/{article_title}
19+
definitions:
20+
view:
21+
uri_schema: /articles/{article_locale}/{article_title}
22+
defaults:
23+
_type: article_view
24+
edit:
25+
uri_schema: /articles/{article_locale}/{article_title}-edit
26+
defaults:
27+
_type: article_edit
28+
review:
29+
uri_schema: /articles/{article_locale}/{article_title}-review
30+
defaults:
31+
_type: article_review
1632
token_providers:
1733
article_title: [content_method, { method: getTitle } ]
1834
article_locale: [content_locale, {} ]

Tests/Unit/Adapter/PhpcrOdmAdapterTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function testCreateAutoRoute($path, $expectedParentPath, $expectedName, $
117117
}
118118

119119
$this->uriContext->getUri()->willReturn($path);
120+
$this->uriContext->getDefaults()->willReturn(array());
120121
$res = $this->adapter->createAutoRoute($this->uriContext->reveal(), $this->contentDocument, 'fr');
121122
$this->assertNotNull($res);
122123
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $res);
@@ -126,6 +127,41 @@ public function testCreateAutoRoute($path, $expectedParentPath, $expectedName, $
126127
$this->assertSame($this->contentDocument, $res->getContent());
127128
}
128129

130+
/**
131+
* It should set the route defaults on the head document.
132+
*/
133+
public function testCreateAutoRouteSetDefaults()
134+
{
135+
$this->dm->getPhpcrSession()->willReturn($this->phpcrSession);
136+
$this->phpcrSession->getRootNode()->willReturn($this->phpcrRootNode);
137+
$this->dm->find(null, $this->baseRoutePath)->willReturn($this->baseNode);
138+
139+
$this->dm->find(null, '/test/uri')
140+
->willReturn($this->parentRoute);
141+
$this->dm->find(null, '/test/uri/to')
142+
->willReturn(null);
143+
144+
$this->uriContext->getUri()->willReturn('/uri/to');
145+
$this->uriContext->getDefaults()->willReturn(array(
146+
'one' => 'k1',
147+
'two' => 'k2',
148+
));
149+
150+
$res = $this->adapter->createAutoRoute($this->uriContext->reveal(), $this->contentDocument, 'fr');
151+
$this->assertNotNull($res);
152+
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $res);
153+
$this->assertEquals('to', $res->getName());
154+
$this->assertEquals(array(
155+
'_auto_route_tag' => 'fr',
156+
'type' => 'cmf_routing_auto.primary',
157+
'one' => 'k1',
158+
'two' => 'k2',
159+
), $res->getDefaults());
160+
161+
$this->assertSame($this->parentRoute, $res->getParent());
162+
$this->assertSame($this->contentDocument, $res->getContent());
163+
}
164+
129165
/**
130166
* @expectedException \RuntimeException
131167
* @expectedExceptionMessageRegExp /Failed to migrate existing.*? at "\/test\/generic" .*? It is an instance of "stdClass"\./

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"prefer-stable": true,
1414
"require": {
1515
"php": "^5.3.9|^7.0",
16-
"symfony-cmf/routing-auto": "^1.1",
16+
"symfony-cmf/routing-auto": "dev-multiple_routes",
1717
"symfony-cmf/routing-bundle": "^1.2.0",
1818
"symfony-cmf/core-bundle": "^1.2",
1919
"aferrandini/urlizer": "1.0.*",

0 commit comments

Comments
 (0)