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

Commit 0b94e06

Browse files
committed
Merge pull request #72 from symfony-cmf/mapping_refactor
Refactor to URL Schema
2 parents 059a4ad + 4f893d2 commit 0b94e06

File tree

152 files changed

+4374
-4536
lines changed

Some content is hidden

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

152 files changed

+4374
-4536
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ php:
66
- 5.5
77

88
env:
9-
- SYMFONY_VERSION=2.2.*
109
- SYMFONY_VERSION=2.3.*
1110
- SYMFONY_VERSION=2.4.*
1211
- SYMFONY_VERSION=dev-master
@@ -16,7 +15,8 @@ before_script:
1615
- composer require symfony/framework-bundle:${SYMFONY_VERSION} --prefer-source
1716
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh
1817

19-
script: phpunit --coverage-text
18+
script:
19+
- phpunit
2020

2121
notifications:
2222
irc: "irc.freenode.org#symfony-cmf"
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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\AutoRoute\Adapter;
13+
14+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
15+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRouteInterface;
16+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
17+
18+
/**
19+
* Adapters will (eventually) abstract all database operations
20+
* with the aim of enabling other providers such as ORM.
21+
*
22+
* @author Daniel Leech <[email protected]>
23+
*/
24+
interface AdapterInterface
25+
{
26+
/**
27+
* Return locales for object
28+
*
29+
* @return array
30+
*/
31+
public function getLocales($object);
32+
33+
/**
34+
* Translate the given object into the given locale
35+
*
36+
* @param object $object
37+
* @param string $locale e.g. fr, en, de, be, etc.
38+
*/
39+
public function translateObject($object, $locale);
40+
41+
/**
42+
* Create a new auto route at the given path
43+
* with the given document as the content.
44+
*
45+
* @param string $path
46+
* @param object $document
47+
* @param string $tag
48+
*
49+
* @return AutoRouteInterface new route document
50+
*/
51+
public function createAutoRoute($path, $document, $tag);
52+
53+
/**
54+
* Return the canonical name for the given class, this is
55+
* required as somethimes an ORM may return a proxy class.
56+
*
57+
* @return string
58+
*/
59+
public function getRealClassName($className);
60+
61+
/**
62+
* Return true if the content associated with the auto route
63+
* and the given content object are the same.
64+
*
65+
* @param RouteObjectInterface
66+
* @param object
67+
*/
68+
public function compareAutoRouteContent(AutoRouteInterface $autoRoute, $contentObject);
69+
70+
/**
71+
* Attempt to find a route with the given URL
72+
*
73+
* @param string $url
74+
*
75+
* @return null|Symfony\Cmf\Component\Routing\RouteObjectInterface
76+
*/
77+
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+
* @return array
124+
*/
125+
public function getReferringAutoRoutes($contentDocument);
126+
127+
/**
128+
* Create a new redirect route at the path of the given
129+
* referringAutoRoute.
130+
*
131+
* The referring auto route should either be deleted or scheduled to be removed,
132+
* so the route created here will replace it.
133+
*
134+
* The new redirecvt route should redirect the request to the URL determined by
135+
* the $newRoute.
136+
*
137+
* @param AutoRouteInterface $referringAutoRoute
138+
* @param AutoRouteInterface $newRoute
139+
*/
140+
public function createRedirectRoute(AutoRouteInterface $referringAutoRoute, AutoRouteInterface $newRoute);
141+
}

AutoRoute/Adapter/PhpcrOdmAdapter.php

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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\AutoRoute\Adapter;
13+
14+
use Doctrine\ODM\PHPCR\DocumentManager;
15+
use Doctrine\ODM\PHPCR\Document\Generic;
16+
use Doctrine\Common\Util\ClassUtils;
17+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute;
18+
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;
22+
23+
/**
24+
* Adapter for PHPCR-ODM
25+
*
26+
* @author Daniel Leech <[email protected]>
27+
*/
28+
class PhpcrOdmAdapter implements AdapterInterface
29+
{
30+
const TAG_NO_MULTILANG = 'no-multilang';
31+
32+
protected $dm;
33+
protected $baseRoutePath;
34+
35+
/**
36+
* @param DocumentManager $dm
37+
* @param string $routeBasePath Route path for all routes
38+
*/
39+
public function __construct(DocumentManager $dm, $routeBasePath)
40+
{
41+
$this->dm = $dm;
42+
$this->baseRoutePath = $routeBasePath;
43+
}
44+
45+
/**
46+
* {@inheritDoc}
47+
*/
48+
public function getLocales($contentDocument)
49+
{
50+
if ($this->dm->isDocumentTranslatable($contentDocument)) {
51+
return $this->dm->getLocalesFor($contentDocument);
52+
}
53+
54+
return array();
55+
}
56+
57+
/**
58+
* {@inheritDoc}
59+
*/
60+
public function translateObject($contentDocument, $locale)
61+
{
62+
$meta = $this->dm->getMetadataFactory()->getMetadataFor(get_class($contentDocument));
63+
$contentDocument = $this->dm->findTranslation($meta->getName(), $meta->getIdentifierValue($contentDocument), $locale);
64+
65+
return $contentDocument;
66+
}
67+
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)
80+
{
81+
$session = $this->dm->getPhpcrSession();
82+
try {
83+
$node = $this->dm->getNodeForDocument($autoRoute);
84+
$newNode = $this->dm->getNodeForDocument($newRoute);
85+
} catch (InvalidItemStateException $e) {
86+
// nothing ..
87+
}
88+
89+
$session->save();
90+
}
91+
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)
123+
{
124+
$path = $this->baseRoutePath;
125+
$parentDocument = $this->dm->find(null, $path);
126+
$segments = preg_split('#/#', $url, null, PREG_SPLIT_NO_EMPTY);
127+
$headName = array_pop($segments);
128+
foreach ($segments as $segment) {
129+
$path .= '/' . $segment;
130+
$document = $this->dm->find(null, $path);
131+
132+
if (null === $document) {
133+
$document = new Generic();
134+
$document->setParent($parentDocument);
135+
$document->setNodeName($segment);
136+
$this->dm->persist($document);
137+
}
138+
$parentDocument = $document;
139+
}
140+
141+
$headRoute = new AutoRoute();
142+
$headRoute->setContent($contentDocument);
143+
$headRoute->setName($headName);
144+
$headRoute->setParent($document);
145+
$headRoute->setAutoRouteTag($autoRouteTag);
146+
147+
return $headRoute;
148+
}
149+
150+
private function buildParentPathForUrl($url)
151+
{
152+
153+
return $document;
154+
}
155+
156+
/**
157+
* {@inheritDoc}
158+
*/
159+
public function createRedirectRoute(AutoRouteInterface $referringAutoRoute, AutoRouteInterface $newRoute)
160+
{
161+
$parentDocument = $referringAutoRoute->getParent();
162+
163+
$redirectRoute = new RedirectRoute();
164+
$redirectRoute->setName($referringAutoRoute->getName());
165+
$redirectRoute->setRouteTarget($newRoute);
166+
$redirectRoute->setParent($parentDocument);
167+
168+
$this->dm->persist($redirectRoute);
169+
}
170+
171+
/**
172+
* {@inheritDoc}
173+
*/
174+
public function getRealClassName($className)
175+
{
176+
return ClassUtils::getRealClass($className);
177+
}
178+
179+
/**
180+
* {@inheritDoc}
181+
*/
182+
public function compareAutoRouteContent(AutoRouteInterface $autoRoute, $contentDocument)
183+
{
184+
if ($autoRoute->getContent() === $contentDocument) {
185+
return true;
186+
}
187+
188+
return false;
189+
}
190+
191+
/**
192+
* {@inheritDoc}
193+
*/
194+
public function getReferringAutoRoutes($contentDocument)
195+
{
196+
return $this->dm->getReferrers($contentDocument, null, null, null, 'Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRouteInterface');
197+
}
198+
199+
/**
200+
* {@inheritDoc}
201+
*/
202+
public function findRouteForUrl($url)
203+
{
204+
$path = $this->getPathFromUrl($url);
205+
206+
return $this->dm->find(null, $path);
207+
}
208+
209+
private function getPathFromUrl($url)
210+
{
211+
return $this->baseRoutePath . $url;
212+
}
213+
}

0 commit comments

Comments
 (0)