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

Commit 4d88a0f

Browse files
committed
LeaveRedirect strategy refactoring
- Do not leave items of class RedirectRoute - AutoRoute class now natively supports redirect
1 parent aa44506 commit 4d88a0f

File tree

7 files changed

+140
-38
lines changed

7 files changed

+140
-38
lines changed

Adapter/PhpcrOdmAdapter.php

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Cmf\Component\RoutingAuto\UrlContext;
2020
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute;
2121
use Symfony\Cmf\Component\RoutingAuto\AdapterInterface;
22+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRedirectRoute;
2223

2324
/**
2425
* Adapter for PHPCR-ODM
@@ -82,22 +83,6 @@ public function generateAutoRouteTag(UrlContext $urlContext)
8283
return $urlContext->getLocale() ? : self::TAG_NO_MULTILANG;
8384
}
8485

85-
/**
86-
* {@inheritDoc}
87-
*/
88-
public function removeDefunctRoute(AutoRouteInterface $autoRoute, $newRoute)
89-
{
90-
$session = $this->dm->getPhpcrSession();
91-
try {
92-
$node = $this->dm->getNodeForDocument($autoRoute);
93-
$newNode = $this->dm->getNodeForDocument($newRoute);
94-
} catch (InvalidItemStateException $e) {
95-
// nothing ..
96-
}
97-
98-
$session->save();
99-
}
100-
10186
/**
10287
* {@inheritDoc}
10388
*/
@@ -114,6 +99,22 @@ public function migrateAutoRouteChildren(AutoRouteInterface $srcAutoRoute, AutoR
11499
}
115100
}
116101

102+
/**
103+
* {@inheritDoc}
104+
*/
105+
public function removeDefunctRoute(AutoRouteInterface $autoRoute, $newRoute)
106+
{
107+
$session = $this->dm->getPhpcrSession();
108+
try {
109+
$node = $this->dm->getNodeForDocument($autoRoute);
110+
$newNode = $this->dm->getNodeForDocument($newRoute);
111+
} catch (InvalidItemStateException $e) {
112+
// nothing ..
113+
}
114+
115+
$session->save();
116+
}
117+
117118
/**
118119
* {@inheritDoc}
119120
*/
@@ -152,29 +153,19 @@ public function createAutoRoute($url, $contentDocument, $autoRouteTag)
152153
$headRoute->setName($headName);
153154
$headRoute->setParent($document);
154155
$headRoute->setAutoRouteTag($autoRouteTag);
156+
$headRoute->setType(AutoRouteInterface::TYPE_PRIMARY);
155157

156158
return $headRoute;
157159
}
158160

159-
private function buildParentPathForUrl($url)
160-
{
161-
162-
return $document;
163-
}
164-
165161
/**
166162
* {@inheritDoc}
167163
*/
168164
public function createRedirectRoute(AutoRouteInterface $referringAutoRoute, AutoRouteInterface $newRoute)
169165
{
170-
$parentDocument = $referringAutoRoute->getParent();
171-
172-
$redirectRoute = new RedirectRoute();
173-
$redirectRoute->setName($referringAutoRoute->getName());
174-
$redirectRoute->setRouteTarget($newRoute);
175-
$redirectRoute->setParent($parentDocument);
176-
177-
$this->dm->persist($redirectRoute);
166+
$referringAutoRoute->setRedirectTarget($newRoute);
167+
$referringAutoRoute->setType(AutoRouteInterface::TYPE_REDIRECT);
168+
$this->dm->persist($referringAutoRoute);
178169
}
179170

180171
/**

Model/AutoRoute.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ class AutoRoute extends Route implements AutoRouteInterface
2424
{
2525
const DEFAULT_KEY_AUTO_ROUTE_TAG = '_auto_route_tag';
2626

27+
/**
28+
* @var AutoRouteInterface
29+
*/
30+
protected $redirectTarget;
31+
2732
/**
2833
* {@inheritDoc}
2934
*/
@@ -39,4 +44,14 @@ public function getAutoRouteTag()
3944
{
4045
return $this->getDefault(self::DEFAULT_KEY_AUTO_ROUTE_TAG);
4146
}
47+
48+
public function setType($type)
49+
{
50+
$this->setDefault('type', $type);
51+
}
52+
53+
public function setRedirectTarget(AutoRouteInterface $redirectTarget)
54+
{
55+
$this->redirectTarget = $redirectTarget;
56+
}
4257
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/phpcr/doctrine-mapping"
22
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
33

4-
<document name="Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute"/>
4+
<document name="Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute">
5+
<reference-one name="redirectTarget"/>
6+
</document>
57

68
</doctrine-mapping>

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute;
1919
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\ConcreteContent;
2020
use Symfony\Cmf\Bundle\RoutingAutoBundle\Adapter\PhpcrOdmAdapter;
21+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticleMultilang;
2122
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticle;
23+
use Symfony\Cmf\Component\RoutingAuto\Model\AutoRouteInterface;
2224

2325
class AutoRouteListenerTest extends BaseTestCase
2426
{
@@ -349,7 +351,7 @@ public function provideLeaveRedirect()
349351
*/
350352
public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePaths, $expectedAutoRoutePaths)
351353
{
352-
$article = new SeoArticle;
354+
$article = new SeoArticleMultilang;
353355
$article->title = 'Hai';
354356
$article->path = '/test/article-1';
355357
$this->getDm()->persist($article);
@@ -362,7 +364,7 @@ public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePat
362364
$this->getDm()->flush();
363365

364366
foreach ($updatedData as $lang => $title) {
365-
$article = $this->getDm()->findTranslation('Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticle', '/test/article-1', $lang);
367+
$article = $this->getDm()->findTranslation('Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticleMultilang', '/test/article-1', $lang);
366368
$article->title = $title;
367369
$this->getDm()->bindTranslation($article, $lang);
368370
}
@@ -371,16 +373,43 @@ public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePat
371373
$this->getDm()->flush();
372374

373375
foreach ($expectedRedirectRoutePaths as $originalPath) {
374-
$redirectRoute = $this->getDm()->find('Symfony\Cmf\Bundle\RoutingBundle\Model\RedirectRoute', $originalPath);
375-
$this->assertNotNull($redirectRoute, 'Redirect exists for: ' . $originalPath);
376+
$redirectRoute = $this->getDm()->find(null, $originalPath);
377+
$this->assertNotNull($redirectRoute, 'Autoroute exists for: ' . $originalPath);
378+
$this->assertEquals(AutoRouteInterface::TYPE_REDIRECT, $redirectRoute->getDefault('type'));
376379
}
377380

378381
foreach ($expectedAutoRoutePaths as $newPath) {
379-
$autoRoute = $this->getDm()->find('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $newPath);
382+
$autoRoute = $this->getDm()->find(null, $newPath);
380383
$this->assertNotNull($redirectRoute, 'Autoroute exists for: ' . $originalPath);
384+
$this->assertEquals(AutoRouteInterface::TYPE_REDIRECT, $redirectRoute->getDefault('type'));
381385
}
382386
}
383387

388+
/**
389+
* depends testLeaveRedirect
390+
*
391+
* See https://github.com/symfony-cmf/RoutingAutoBundle/issues/111
392+
*/
393+
public function testLLeaveRedirectAndRenameToOriginal()
394+
{
395+
$article = new SeoArticle;
396+
$article->title = 'Hai';
397+
$article->path = '/test/article-1';
398+
$this->getDm()->persist($article);
399+
400+
$this->getDm()->persist($article);
401+
402+
$this->getDm()->flush();
403+
404+
$article->title = 'Ho';
405+
$this->getDm()->persist($article);
406+
$this->getDm()->flush();
407+
408+
$article->title = 'Hai';
409+
$this->getDm()->persist($article);
410+
$this->getDm()->flush();
411+
}
412+
384413
/**
385414
* Ensure that we can map parent classes: #56
386415
*/

Tests/Resources/Document/SeoArticle.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,46 @@
1414
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
1515

1616
/**
17-
* @PHPCR\Document(translator="child", referenceable=true)
17+
* @PHPCR\Document(referenceable=true)
1818
*/
19-
class SeoArticle extends Article
19+
class SeoArticle
2020
{
21+
/**
22+
* @PHPCR\Id()
23+
*/
24+
public $path;
25+
26+
/**
27+
* @PHPCR\Referrers(
28+
* referringDocument="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route",
29+
* referencedBy="content"
30+
* )
31+
*/
32+
public $routes;
33+
34+
/**
35+
* @PHPCR\String()
36+
*/
37+
public $title;
38+
39+
/**
40+
* @PHPCR\Date(nullable=true)
41+
*/
42+
public $date;
43+
44+
public function getTitle()
45+
{
46+
return $this->title;
47+
}
48+
49+
public function getDate()
50+
{
51+
return $this->date;
52+
}
53+
54+
public function setDate($date)
55+
{
56+
$this->date = $date;
57+
}
58+
2159
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2013 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document;
13+
14+
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
15+
16+
/**
17+
* @PHPCR\Document(translator="child", referenceable=true)
18+
*/
19+
class SeoArticleMultilang extends Article
20+
{
21+
}

Tests/Resources/app/config/routing_auto.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Article:
1818
article_locale: [content_locale, {} ]
1919

2020
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticle:
21+
url_schema: /seo-articles/{article_title}
22+
defunct_route_handler: [leave_redirect, {}]
23+
token_providers:
24+
article_title: [content_method, { method: getTitle } ]
25+
26+
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticleMultilang:
2127
url_schema: /seo-articles/{article_locale}/{article_title}
2228
defunct_route_handler: [leave_redirect, {}]
2329
token_providers:

0 commit comments

Comments
 (0)