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

Commit 5b4477d

Browse files
committed
Merge pull request #81 from dantleech/defunct_route_handling
[WIP] Support for handling defunct routes
2 parents d3ae963 + 26c9b5a commit 5b4477d

File tree

58 files changed

+903
-358
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+903
-358
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ before_script:
1616
- composer require symfony/framework-bundle:${SYMFONY_VERSION} --prefer-source
1717
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh
1818

19-
script: phpunit --coverage-text
19+
script:
20+
- phpunit --coverage-text
21+
- ./vendor/bin/phpspec run
2022

2123
notifications:
2224
irc: "irc.freenode.org#symfony-cmf"

AutoRoute/Adapter/AdapterInterface.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Adapter;
1313

1414
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
15+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRouteInterface;
16+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
1517

1618
/**
1719
* Adapters will (eventually) abstract all database operations
@@ -32,7 +34,7 @@ public function getLocales($object);
3234
* Translate the given object into the given locale
3335
*
3436
* @param object $object
35-
* @param string $locale e.g. fr, en, de, be, etc.
37+
* @param string $locale e.g. fr, en, de, be, etc.
3638
*/
3739
public function translateObject($object, $locale);
3840

@@ -42,27 +44,28 @@ public function translateObject($object, $locale);
4244
*
4345
* @param string $path
4446
* @param object $document
47+
* @param string $tag
4548
*
46-
* @return Route new route document
49+
* @return AutoRouteInterface new route document
4750
*/
48-
public function createRoute($path, $document);
51+
public function createAutoRoute($path, $document, $tag);
4952

5053
/**
51-
* Return the canonical name for the given class, this is
54+
* Return the canonical name for the given class, this is
5255
* required as somethimes an ORM may return a proxy class.
5356
*
5457
* @return string
5558
*/
5659
public function getRealClassName($className);
5760

5861
/**
59-
* Return true if the content associated with the route
62+
* Return true if the content associated with the auto route
6063
* and the given content object are the same.
6164
*
6265
* @param RouteObjectInterface
6366
* @param object
6467
*/
65-
public function compareRouteContent(RouteObjectInterface $route, $contentObject);
68+
public function compareAutoRouteContent(AutoRouteInterface $autoRoute, $contentObject);
6669

6770
/**
6871
* Attempt to find a route with the given URL
@@ -72,4 +75,50 @@ public function compareRouteContent(RouteObjectInterface $route, $contentObject)
7275
* @return null|Symfony\Cmf\Component\Routing\RouteObjectInterface
7376
*/
7477
public function findRouteForUrl($url);
78+
79+
/**
80+
* Generate a tag which can be used to identify this route from
81+
* other routes as required.
82+
*
83+
* @param UrlContext $urlContext
84+
*/
85+
public function generateAutoRouteTag(UrlContext $urlContext);
86+
87+
/**
88+
* Migrate the descendant path elements from one route to another.
89+
*
90+
* e.g. in an RDBMS with a routes:
91+
*
92+
* /my-blog
93+
* /my-blog/posts/post1
94+
* /my-blog/posts/post2
95+
* /my-new-blog
96+
*
97+
* We want to migrate the children of "my-blog" to "my-new-blog" so that
98+
* we have:
99+
*
100+
* /my-blog
101+
* /my-new-blog
102+
* /my-new-blog/posts/post1
103+
* /my-new-blog/posts/post2
104+
*
105+
* @param AutoRouteInterface $srcAutoRoute
106+
* @param AutoRouteInterface $destAutoRoute
107+
*/
108+
public function migrateAutoRouteChildren(AutoRouteInterface $srcAutoRoute, AutoRouteInterface $destAutoRoute);
109+
110+
/**
111+
* Remove the given auto route
112+
*
113+
* @param AutoRouteInterface $autoRoute
114+
*/
115+
public function removeAutoRoute(AutoRouteInterface $autoRoute);
116+
117+
/**
118+
* Return auto routes which refer to the given content
119+
* object.
120+
*
121+
* @param object $contentDocument
122+
*/
123+
public function getReferringAutoRoutes($contentDocument);
75124
}

AutoRoute/Adapter/PhpcrOdmAdapter.php

Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,37 @@
1414
use Doctrine\ODM\PHPCR\DocumentManager;
1515
use Doctrine\ODM\PHPCR\Document\Generic;
1616
use Doctrine\Common\Util\ClassUtils;
17-
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
18-
use PHPCR\Util\NodeHelper;
1917
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute;
2018
use PHPCR\InvalidItemStateException;
19+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRouteInterface;
20+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
21+
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RedirectRoute;
2122

2223
/**
23-
* Abstraction adapter for PHPCR-ODM
24+
* Adapter for PHPCR-ODM
2425
*
25-
* This class will eventually encapsulate all of the PHPCR-ODM
26-
* specific logic to enable support for multiple backends.
26+
* @author Daniel Leech <[email protected]>
2727
*/
2828
class PhpcrOdmAdapter implements AdapterInterface
2929
{
30+
const TAG_NO_MULTILANG = 'no-multilang';
31+
3032
protected $dm;
3133
protected $baseRoutePath;
3234

35+
/**
36+
* @param DocumentManager $dm
37+
* @param string $routeBasePath Route path for all routes
38+
*/
3339
public function __construct(DocumentManager $dm, $routeBasePath)
3440
{
3541
$this->dm = $dm;
3642
$this->baseRoutePath = $routeBasePath;
3743
}
3844

45+
/**
46+
* {@inheritDoc}
47+
*/
3948
public function getLocales($contentDocument)
4049
{
4150
if ($this->dm->isDocumentTranslatable($contentDocument)) {
@@ -45,6 +54,9 @@ public function getLocales($contentDocument)
4554
return array();
4655
}
4756

57+
/**
58+
* {@inheritDoc}
59+
*/
4860
public function translateObject($contentDocument, $locale)
4961
{
5062
$meta = $this->dm->getMetadataFactory()->getMetadataFor(get_class($contentDocument));
@@ -53,29 +65,64 @@ public function translateObject($contentDocument, $locale)
5365
return $contentDocument;
5466
}
5567

56-
public function removeDefunctRoute($route, $canonicalRoute)
68+
/**
69+
* {@inheritDoc}
70+
*/
71+
public function generateAutoRouteTag(UrlContext $urlContext)
72+
{
73+
return $urlContext->getLocale() ? : self::TAG_NO_MULTILANG;
74+
}
75+
76+
/**
77+
* {@inheritDoc}
78+
*/
79+
public function removeDefunctRoute(AutoRouteInterface $autoRoute, $newRoute)
5780
{
5881
$session = $this->dm->getPhpcrSession();
5982
try {
60-
$node = $this->dm->getNodeForDocument($route);
61-
$canonicalNode = $this->dm->getNodeForDocument($canonicalRoute);
62-
$nodeChildren = $node->getNodes();
63-
foreach ($nodeChildren as $nodeChild) {
64-
$session->move($nodeChild->getPath(), $canonicalNode->getPath() . '/' . $nodeChild->getName());
65-
}
66-
$session->removeItem($node->getPath());
83+
$node = $this->dm->getNodeForDocument($autoRoute);
84+
$newNode = $this->dm->getNodeForDocument($newRoute);
6785
} catch (InvalidItemStateException $e) {
6886
// nothing ..
6987
}
7088

7189
$session->save();
7290
}
7391

74-
public function createRoute($url, $contentDocument)
92+
/**
93+
* {@inheritDoc}
94+
*/
95+
public function migrateAutoRouteChildren(AutoRouteInterface $srcAutoRoute, AutoRouteInterface $destAutoRoute)
96+
{
97+
$session = $this->dm->getPhpcrSession();
98+
$srcAutoRouteNode = $this->dm->getNodeForDocument($srcAutoRoute);
99+
$destAutoRouteNode = $this->dm->getNodeForDocument($destAutoRoute);
100+
101+
$srcAutoRouteChildren = $srcAutoRouteNode->getNodes();
102+
103+
foreach ($srcAutoRouteChildren as $srcAutoRouteChild) {
104+
$session->move($srcAutoRouteChild->getPath(), $destAutoRouteNode->getPath() . '/' . $srcAutoRouteChild->getName());
105+
}
106+
}
107+
108+
/**
109+
* {@inheritDoc}
110+
*/
111+
public function removeAutoRoute(AutoRouteInterface $autoRoute)
112+
{
113+
$session = $this->dm->getPhpcrSession();
114+
$node = $this->dm->getNodeForDocument($autoRoute);
115+
$session->removeItem($node->getPath());
116+
$session->save();
117+
}
118+
119+
/**
120+
* {@inheritDoc}
121+
*/
122+
public function createAutoRoute($url, $contentDocument, $autoRouteTag)
75123
{
76124
$path = $this->baseRoutePath;
77125
$parentDocument = $this->dm->find(null, $path);
78-
79126
$segments = preg_split('#/#', $url, null, PREG_SPLIT_NO_EMPTY);
80127
$headName = array_pop($segments);
81128
foreach ($segments as $segment) {
@@ -95,27 +142,55 @@ public function createRoute($url, $contentDocument)
95142
$headRoute->setContent($contentDocument);
96143
$headRoute->setName($headName);
97144
$headRoute->setParent($document);
145+
$headRoute->setAutoRouteTag($autoRouteTag);
98146

99147
return $headRoute;
100148
}
101149

150+
private function buildParentPathForUrl($url)
151+
{
152+
153+
return $document;
154+
}
155+
156+
public function createRedirectRoute($referringAutoRoute, $newRoute)
157+
{
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);
166+
}
167+
168+
/**
169+
* {@inheritDoc}
170+
*/
102171
public function getRealClassName($className)
103172
{
104173
return ClassUtils::getRealClass($className);
105174
}
106175

107-
public function compareRouteContent(RouteObjectInterface $route, $contentDocument)
176+
/**
177+
* {@inheritDoc}
178+
*/
179+
public function compareAutoRouteContent(AutoRouteInterface $autoRoute, $contentDocument)
108180
{
109-
if ($route->getContent() === $contentDocument) {
181+
if ($autoRoute->getContent() === $contentDocument) {
110182
return true;
111183
}
112184

113185
return false;
114186
}
115187

116-
public function getReferringRoutes($contentDocument)
188+
/**
189+
* {@inheritDoc}
190+
*/
191+
public function getReferringAutoRoutes($contentDocument)
117192
{
118-
return $this->dm->getReferrers($contentDocument, null, null, null, 'Symfony\Cmf\Component\Routing\RouteObjectInterface');
193+
return $this->dm->getReferrers($contentDocument, null, null, null, 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRouteInterface');
119194
}
120195

121196
/**
@@ -124,6 +199,7 @@ public function getReferringRoutes($contentDocument)
124199
public function findRouteForUrl($url)
125200
{
126201
$path = $this->getPathFromUrl($url);
202+
127203
return $this->dm->find(null, $path);
128204
}
129205

@@ -132,4 +208,3 @@ private function getPathFromUrl($url)
132208
return $this->baseRoutePath . $url;
133209
}
134210
}
135-

0 commit comments

Comments
 (0)