1414use Doctrine \ODM \PHPCR \DocumentManager ;
1515use Doctrine \ODM \PHPCR \Document \Generic ;
1616use Doctrine \Common \Util \ClassUtils ;
17- use Symfony \Cmf \Component \Routing \RouteObjectInterface ;
18- use PHPCR \Util \NodeHelper ;
1917use Symfony \Cmf \Bundle \RoutingAutoBundle \Model \AutoRoute ;
2018use 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 */
2828class 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