14
14
use Doctrine \ODM \PHPCR \DocumentManager ;
15
15
use Doctrine \ODM \PHPCR \Document \Generic ;
16
16
use Doctrine \Common \Util \ClassUtils ;
17
- use Symfony \Cmf \Component \Routing \RouteObjectInterface ;
18
- use PHPCR \Util \NodeHelper ;
19
17
use Symfony \Cmf \Bundle \RoutingAutoBundle \Model \AutoRoute ;
20
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 ;
21
22
22
23
/**
23
- * Abstraction adapter for PHPCR-ODM
24
+ * Adapter for PHPCR-ODM
24
25
*
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] >
27
27
*/
28
28
class PhpcrOdmAdapter implements AdapterInterface
29
29
{
30
+ const TAG_NO_MULTILANG = 'no-multilang ' ;
31
+
30
32
protected $ dm ;
31
33
protected $ baseRoutePath ;
32
34
35
+ /**
36
+ * @param DocumentManager $dm
37
+ * @param string $routeBasePath Route path for all routes
38
+ */
33
39
public function __construct (DocumentManager $ dm , $ routeBasePath )
34
40
{
35
41
$ this ->dm = $ dm ;
36
42
$ this ->baseRoutePath = $ routeBasePath ;
37
43
}
38
44
45
+ /**
46
+ * {@inheritDoc}
47
+ */
39
48
public function getLocales ($ contentDocument )
40
49
{
41
50
if ($ this ->dm ->isDocumentTranslatable ($ contentDocument )) {
@@ -45,6 +54,9 @@ public function getLocales($contentDocument)
45
54
return array ();
46
55
}
47
56
57
+ /**
58
+ * {@inheritDoc}
59
+ */
48
60
public function translateObject ($ contentDocument , $ locale )
49
61
{
50
62
$ meta = $ this ->dm ->getMetadataFactory ()->getMetadataFor (get_class ($ contentDocument ));
@@ -53,29 +65,64 @@ public function translateObject($contentDocument, $locale)
53
65
return $ contentDocument ;
54
66
}
55
67
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 )
57
80
{
58
81
$ session = $ this ->dm ->getPhpcrSession ();
59
82
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 );
67
85
} catch (InvalidItemStateException $ e ) {
68
86
// nothing ..
69
87
}
70
88
71
89
$ session ->save ();
72
90
}
73
91
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 )
75
123
{
76
124
$ path = $ this ->baseRoutePath ;
77
125
$ parentDocument = $ this ->dm ->find (null , $ path );
78
-
79
126
$ segments = preg_split ('#/# ' , $ url , null , PREG_SPLIT_NO_EMPTY );
80
127
$ headName = array_pop ($ segments );
81
128
foreach ($ segments as $ segment ) {
@@ -95,27 +142,55 @@ public function createRoute($url, $contentDocument)
95
142
$ headRoute ->setContent ($ contentDocument );
96
143
$ headRoute ->setName ($ headName );
97
144
$ headRoute ->setParent ($ document );
145
+ $ headRoute ->setAutoRouteTag ($ autoRouteTag );
98
146
99
147
return $ headRoute ;
100
148
}
101
149
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
+ */
102
171
public function getRealClassName ($ className )
103
172
{
104
173
return ClassUtils::getRealClass ($ className );
105
174
}
106
175
107
- public function compareRouteContent (RouteObjectInterface $ route , $ contentDocument )
176
+ /**
177
+ * {@inheritDoc}
178
+ */
179
+ public function compareAutoRouteContent (AutoRouteInterface $ autoRoute , $ contentDocument )
108
180
{
109
- if ($ route ->getContent () === $ contentDocument ) {
181
+ if ($ autoRoute ->getContent () === $ contentDocument ) {
110
182
return true ;
111
183
}
112
184
113
185
return false ;
114
186
}
115
187
116
- public function getReferringRoutes ($ contentDocument )
188
+ /**
189
+ * {@inheritDoc}
190
+ */
191
+ public function getReferringAutoRoutes ($ contentDocument )
117
192
{
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 ' );
119
194
}
120
195
121
196
/**
@@ -124,6 +199,7 @@ public function getReferringRoutes($contentDocument)
124
199
public function findRouteForUrl ($ url )
125
200
{
126
201
$ path = $ this ->getPathFromUrl ($ url );
202
+
127
203
return $ this ->dm ->find (null , $ path );
128
204
}
129
205
@@ -132,4 +208,3 @@ private function getPathFromUrl($url)
132
208
return $ this ->baseRoutePath . $ url ;
133
209
}
134
210
}
135
-
0 commit comments