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

Commit 2cef3cd

Browse files
committed
LeaveRedirect "works"
But needs an additional flush
1 parent 388062e commit 2cef3cd

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

AutoRoute/Adapter/PhpcrOdmAdapter.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use PHPCR\InvalidItemStateException;
1919
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRouteInterface;
2020
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
21+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute;
2122

2223
/**
2324
* Adapter for PHPCR-ODM
@@ -122,7 +123,6 @@ public function createAutoRoute($url, $contentDocument, $autoRouteTag)
122123
{
123124
$path = $this->baseRoutePath;
124125
$parentDocument = $this->dm->find(null, $path);
125-
126126
$segments = preg_split('#/#', $url, null, PREG_SPLIT_NO_EMPTY);
127127
$headName = array_pop($segments);
128128
foreach ($segments as $segment) {
@@ -147,9 +147,22 @@ public function createAutoRoute($url, $contentDocument, $autoRouteTag)
147147
return $headRoute;
148148
}
149149

150+
private function buildParentPathForUrl($url)
151+
{
152+
153+
return $document;
154+
}
155+
150156
public function createRedirectRoute($referringAutoRoute, $newRoute)
151157
{
152-
throw new \Exception('IMPLMENT ME');
158+
$parentDocument = $referringAutoRoute->getParent();
159+
160+
$redirectRoute = new RedirectRoute();
161+
$redirectRoute->setName($referringAutoRoute->getName());
162+
$redirectRoute->setRouteTarget($newRoute);
163+
$redirectRoute->setParent($parentDocument);
164+
165+
$this->dm->persist($redirectRoute);
153166
}
154167

155168
/**

AutoRoute/Mapping/ClassMetadata.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ClassMetadata extends MergeableClassMetadata
3131
*
3232
* @var array
3333
*/
34-
protected $defunctRouteHandler = array('remove', array());
34+
protected $defunctRouteHandler = array('name' => 'remove');
3535

3636
protected $extend;
3737

Doctrine/Phpcr/AutoRouteListener.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute;
1818
use Doctrine\Common\Util\ClassUtils;
1919
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContextCollection;
20+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Mapping\Exception\ClassNotMappedException;
2021

2122
/**
2223
* Doctrine PHPCR ODM listener for maintaining automatic routes.
@@ -90,14 +91,19 @@ public function onFlush(ManagerEventArgs $args)
9091
}
9192
}
9293

93-
public function postFlush()
94+
public function postFlush(ManagerEventArgs $args)
9495
{
96+
$dm = $args->getObjectManager();
9597
$arm = $this->getAutoRouteManager();
9698
$arm->handleDefunctRoutes();
9799
}
98100

99101
private function isAutoRouteable($document)
100102
{
101-
return $this->getMetadataFactory()->getMetadataForClass(get_class($document));
103+
try {
104+
return (boolean) $this->getMetadataFactory()->getMetadataForClass(get_class($document));
105+
} catch (ClassNotMappedException $e) {
106+
return false;
107+
}
102108
}
103109
}

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,16 @@ public function provideLeaveRedirect()
328328
'es' => 'Adios todo el mundo',
329329
),
330330
array(
331-
'test/auto-route/seo-articles/en/goodbye-everybody',
332-
'test/auto-route/seo-articles/fr/aurevoir-le-monde',
333-
'test/auto-route/seo-articles/de/aud-weidersehn',
334-
'test/auto-route/seo-articles/es/adios-todo-el-mundo',
331+
'test/auto-route/articles/en/hello-everybody',
332+
'test/auto-route/articles/fr/bonjour-le-monde',
333+
'test/auto-route/articles/de/gutentag',
334+
'test/auto-route/articles/es/hola-todo-el-mundo',
335+
),
336+
array(
337+
'test/auto-route/articles/en/goodbye-everybody',
338+
'test/auto-route/articles/fr/aurevoir-le-monde',
339+
'test/auto-route/articles/de/aud-weidersehn',
340+
'test/auto-route/articles/es/adios-todo-el-mundo',
335341
),
336342
),
337343
);
@@ -340,9 +346,10 @@ public function provideLeaveRedirect()
340346
/**
341347
* @dataProvider provideLeaveRedirect
342348
*/
343-
public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePaths)
349+
public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePaths, $expectedAutoRoutePaths)
344350
{
345351
$article = new SeoArticle;
352+
$article->title = 'Hai';
346353
$article->path = '/test/article-1';
347354
$this->getDm()->persist($article);
348355

@@ -361,6 +368,19 @@ public function testLeaveRedirect($data, $updatedData, $expectedRedirectRoutePat
361368

362369
$this->getDm()->persist($article);
363370
$this->getDm()->flush();
371+
372+
// additional flush -- maybe we should handle this with an event listener of some sort?
373+
$this->getDm()->flush();
374+
375+
foreach ($expectedRedirectRoutePaths as $originalPath) {
376+
$redirectRoute = $this->getDm()->find('Symfony\Cmf\Bundle\RoutingBundle\Model\RedirectRoute', $originalPath);
377+
$this->assertNotNull($redirectRoute, 'Redirect exists for: ' . $originalPath);
378+
}
379+
380+
foreach ($expectedAutoRoutePaths as $newPath) {
381+
$autoRoute = $this->getDm()->find('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $newPath);
382+
$this->assertNotNull($redirectRoute, 'Autoroute exists for: ' . $originalPath);
383+
}
364384
}
365385

366386
/**

0 commit comments

Comments
 (0)