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

Commit ddb0e63

Browse files
committed
Merge pull request #172 from symfony-cmf/multiple_routes
Support for multiple routes
2 parents 1470959 + 4a21a75 commit ddb0e63

File tree

9 files changed

+115
-26
lines changed

9 files changed

+115
-26
lines changed

.travis.yml

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
language: php
22

33
php:
4-
- 5.3
5-
- 5.4
64
- 5.5
75
- 5.6
86
- 7.0
@@ -19,7 +17,7 @@ env:
1917

2018
matrix:
2119
include:
22-
- php: 5.3
20+
- php: 5.5
2321
env: SYMFONY_VERSION=2.3.* COMPOSER_FLAGS="--prefer-lowest"
2422
- php: 5.6
2523
env: SYMFONY_VERSION=2.3.* SYMFONY_DEPRECATIONS_HELPER=weak

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

Command/RefreshCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function configure()
2525
$this
2626
->setName('cmf:routing:auto:refresh')
2727
->setDescription('Refresh auto-routeable documents')
28-
->setHelp(<<<HERE
28+
->setHelp(<<<'HERE'
2929
WARNING: Experimental!
3030
3131
This command iterates over all Documents that are mapped by the auto

Doctrine/Phpcr/AutoRouteListener.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function endFlush(ManagerEventArgs $args)
115115
private function isAutoRouteable($document)
116116
{
117117
try {
118-
return (boolean) $this->getMetadataFactory()->getMetadataForClass(get_class($document));
118+
return (bool) $this->getMetadataFactory()->getMetadataForClass(get_class($document));
119119
} catch (ClassNotMappedException $e) {
120120
return false;
121121
}

Resources/config/auto_route.xml

+8-4
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>
@@ -24,7 +25,7 @@
2425
<services>
2526
<service
2627
id="cmf_routing_auto.slugifier"
27-
class="Symfony\Cmf\Bundle\CoreBundle\Slugifier\CallbackSlugifier"
28+
class="Symfony\Cmf\Api\Slugifier\CallbackSlugifier"
2829
>
2930
<argument>Ferrandini\Urlizer::urlize</argument>
3031
</service>
@@ -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);
+34-8
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,68 @@
11
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Blog:
2-
uri_schema: /blog/{blog_title}
2+
definitions:
3+
frontend:
4+
uri_schema: /blog/{blog_title}
5+
defaults:
6+
_controller: BlogController
37
token_providers:
48
blog_title: [content_method, { method: getTitle } ]
59

610
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Post:
7-
uri_schema: /blog/{blog_title}/{post_date}/{post_title}
8-
conflict_resolver: [auto_increment, { }]
11+
definitions:
12+
view:
13+
uri_schema: /blog/{blog_title}/{post_date}/{post_title}
914
token_providers:
1015
blog_title: [content_method, { method: getBlogTitle } ]
1116
post_date: [content_datetime, { method: getDate, date_format: Y/m/d } ]
1217
post_title: [content_method, { method: getTitle } ]
18+
conflict_resolver: [auto_increment, { }]
1319

1420
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Article:
15-
uri_schema: /articles/{article_locale}/{article_title}
21+
definitions:
22+
view:
23+
uri_schema: /articles/{article_locale}/{article_title}
24+
defaults:
25+
_type: article_view
26+
edit:
27+
uri_schema: /articles/{article_locale}/{article_title}-edit
28+
defaults:
29+
_type: article_edit
30+
review:
31+
uri_schema: /articles/{article_locale}/{article_title}-review
32+
defaults:
33+
_type: article_review
1634
token_providers:
1735
article_title: [content_method, { method: getTitle } ]
1836
article_locale: [content_locale, {} ]
1937

2038
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticle:
21-
uri_schema: /seo-articles/{article_title}
39+
definitions:
40+
view:
41+
uri_schema: /seo-articles/{article_title}
2242
defunct_route_handler: [leave_redirect, {}]
2343
token_providers:
2444
article_title: [content_method, { method: getTitle } ]
2545

2646
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticleMultilang:
27-
uri_schema: /seo-articles/{article_locale}/{article_title}
47+
definitions:
48+
view:
49+
uri_schema: /seo-articles/{article_locale}/{article_title}
2850
defunct_route_handler: [leave_redirect, {}]
2951
token_providers:
3052
article_title: [content_method, { method: getTitle } ]
3153
article_locale: [content_locale, {} ]
3254

3355
# AbstractContent for ensuring that parent class mapping works
3456
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\AbstractContent:
35-
uri_schema: /articles/{article_title}
57+
definitions:
58+
view:
59+
uri_schema: /articles/{article_title}
3660
token_providers:
3761
article_title: [content_method, { method: getTitle } ]
3862

3963
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Page:
40-
uri_schema: /{title}
64+
definitions:
65+
view:
66+
uri_schema: /{title}
4167
token_providers:
4268
title: [content_method, { method: getTitle } ]

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
"minimum-stability": "dev",
1313
"prefer-stable": true,
1414
"require": {
15-
"php": "^5.3.9|^7.0",
16-
"symfony-cmf/routing-auto": "^1.1",
15+
"php": "^5.5.0|^7.0",
16+
"symfony-cmf/routing-auto": "^2.0@dev",
1717
"symfony-cmf/routing-bundle": "^1.2.0",
18-
"symfony-cmf/core-bundle": "^1.2",
18+
"symfony-cmf/slugifier-api": "^1.0",
1919
"aferrandini/urlizer": "1.0.*",
2020
"symfony/config": "^2.2",
2121
"jms/metadata": "1.5.*"
@@ -41,7 +41,7 @@
4141
},
4242
"extra": {
4343
"branch-alias": {
44-
"dev-master": "1.1-dev"
44+
"dev-master": "2.0-dev"
4545
}
4646
}
4747
}

0 commit comments

Comments
 (0)