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

Commit bd72f74

Browse files
committed
Merge pull request #113 from symfony-cmf/ghi-111
LeaveRedirect strategy refactoring
2 parents aa44506 + a91da87 commit bd72f74

File tree

12 files changed

+184
-43
lines changed

12 files changed

+184
-43
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ env:
1111
- SYMFONY_VERSION=dev-master
1212

1313
before_script:
14-
- composer self-update
1514
- composer require symfony/framework-bundle:${SYMFONY_VERSION} --prefer-source
1615
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh
1716

Adapter/PhpcrOdmAdapter.php

Lines changed: 4 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
*/
@@ -152,29 +137,18 @@ public function createAutoRoute($url, $contentDocument, $autoRouteTag)
152137
$headRoute->setName($headName);
153138
$headRoute->setParent($document);
154139
$headRoute->setAutoRouteTag($autoRouteTag);
140+
$headRoute->setType(AutoRouteInterface::TYPE_PRIMARY);
155141

156142
return $headRoute;
157143
}
158144

159-
private function buildParentPathForUrl($url)
160-
{
161-
162-
return $document;
163-
}
164-
165145
/**
166146
* {@inheritDoc}
167147
*/
168148
public function createRedirectRoute(AutoRouteInterface $referringAutoRoute, AutoRouteInterface $newRoute)
169149
{
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);
150+
$referringAutoRoute->setRedirectTarget($newRoute);
151+
$referringAutoRoute->setType(AutoRouteInterface::TYPE_REDIRECT);
178152
}
179153

180154
/**

Model/AutoRoute.php

Lines changed: 20 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 $redirectRoute;
31+
2732
/**
2833
* {@inheritDoc}
2934
*/
@@ -39,4 +44,19 @@ 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 $redirectRoute)
54+
{
55+
$this->redirectRoute = $redirectRoute;
56+
}
57+
58+
public function getRedirectTarget()
59+
{
60+
return $this->redirectRoute;
61+
}
4262
}
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="redirectRoute"/>
6+
</document>
57

68
</doctrine-mapping>

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 31 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,40 @@ 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 testLeaveRedirectAndRenameToOriginal()
394+
{
395+
$article = new SeoArticle;
396+
$article->title = 'Hai';
397+
$article->path = '/test/article-1';
398+
$this->getDm()->persist($article);
399+
$this->getDm()->flush();
400+
401+
$article->title = 'Ho';
402+
$this->getDm()->persist($article);
403+
$this->getDm()->flush();
404+
405+
$article->title = 'Hai';
406+
$this->getDm()->persist($article);
407+
$this->getDm()->flush();
408+
}
409+
384410
/**
385411
* Ensure that we can map parent classes: #56
386412
*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Controller;
4+
5+
use Symfony\Cmf\Component\RoutingAuto\Model\AutoRouteInterface;
6+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7+
8+
class TestController extends Controller
9+
{
10+
public function redirectAction(AutoRouteInterface $routeDocument)
11+
{
12+
$routeTarget = $routeDocument->getRedirectTarget();
13+
return $this->redirect($this->get('router')->generate($routeTarget));
14+
}
15+
}

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/app_config.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ cmf_routing:
66
dynamic:
77
enabled: true
88
controllers_by_type:
9-
demo_alias: test.controller:aliasAction
10-
controllers_by_class:
11-
Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute: cmf_routing.redirect_controller:redirectAction
9+
cmf_routing_auto.redirect: Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Controller\TestController::redirectAction
1210
persistence:
1311
phpcr:
1412
enabled: true
15-
route_basepath: /test/routing
13+
route_basepath: /test/auto-route
1614

1715
cmf_routing_auto:
1816
auto_mapping: false
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use Symfony\Component\Routing\RouteCollection;
4+
5+
$collection = new RouteCollection();
6+
7+
return $collection;

0 commit comments

Comments
 (0)