7
7
use Symfony \Cmf \Bundle \RoutingAutoRouteBundle \Document \AutoRoute ;
8
8
use Symfony \Cmf \Bundle \CoreBundle \Slugifier \SlugifierInterface ;
9
9
use PHPCR \Util \NodeHelper ;
10
+ use Doctrine \Common \Util \ClassUtils ;
10
11
11
12
/**
12
13
* This class is concerned with the automatic creation of route objects.
13
14
*
14
15
* @author Daniel Leech <[email protected] >
15
- * @author Sjoerd Peters <[email protected] >
16
16
*/
17
17
class AutoRouteManager
18
18
{
19
- protected $ dm ;
20
- protected $ phpcrSession ;
21
- protected $ mapping ;
22
- protected $ defaultPath ;
23
- protected $ slugifier ;
19
+ protected $ bucf ;
24
20
25
- /**
26
- * @TODO: Should defaultPath be contained in a service to
27
- * enable this property to be modified at runtime?
28
- * @TODO: Replace Slugifier with TransformerFactory or similar.
29
- *
30
- * @param DocumentManager $dm PHPCR-ODM Document Manager
31
- * @param array $mapping Class => configuration mapping
32
- * @param SlugifierInterface $slugifier Slugifier
33
- * @param string $defaultPath Default base path for new routes
34
- */
35
- public function __construct (
36
- DocumentManager $ dm ,
37
- array $ mapping ,
38
- SlugifierInterface $ slugifier ,
39
- $ defaultPath
40
- )
21
+ public function __construct (BuilderUnitChainFactory $ bucf )
41
22
{
42
- $ this ->dm = $ dm ;
43
- $ this ->mapping = $ mapping ;
44
- $ this ->slugifier = $ slugifier ;
45
- $ this ->defaultPath = $ defaultPath ;
46
- $ this ->phpcrSession = $ dm ->getPhpcrSession ();
23
+ $ this ->bucf = $ bucf ;
47
24
}
48
25
49
26
/**
@@ -58,18 +35,13 @@ public function __construct(
58
35
*/
59
36
public function updateAutoRouteForDocument ($ document )
60
37
{
61
- $ metadata = $ this ->getMetadata ($ document );
62
- $ autoRoute = $ this ->getAutoRouteForDocument ($ document );
63
-
64
- $ autoRouteName = $ this ->getRouteName ($ document );
65
- $ autoRoute ->setName ($ autoRouteName );
66
-
67
- $ autoRouteParent = $ this ->getParentRoute ($ document );
68
- $ autoRoute ->setParent ($ autoRouteParent );
38
+ $ context = new BuilderContext ;
39
+ $ context ->setObject ($ document );
69
40
70
- $ this ->dm ->persist ($ autoRoute );
41
+ $ builderUnitChain = $ this ->bucf ->getChain (ClassUtils::getClass ($ document ));
42
+ $ builderUnitChain ->executeChain ($ context );
71
43
72
- return $ autoRoute ;
44
+ return $ context ;
73
45
}
74
46
75
47
/**
@@ -83,12 +55,7 @@ public function updateAutoRouteForDocument($document)
83
55
*/
84
56
public function removeAutoRoutesForDocument ($ document )
85
57
{
86
- $ autoRoutes = $ this ->fetchAutoRoutesForDocument ($ document );
87
- foreach ($ autoRoutes as $ autoRoute ) {
88
- $ this ->dm ->remove ($ autoRoute );
89
- }
90
-
91
- return $ autoRoutes ;
58
+ throw new \Exception ('Implement me?? ' );
92
59
}
93
60
94
61
/**
@@ -100,158 +67,6 @@ public function removeAutoRoutesForDocument($document)
100
67
*/
101
68
public function isAutoRouteable ($ document )
102
69
{
103
- foreach ($ this ->mapping as $ classFqn => $ metadata ) {
104
- if ($ document instanceof $ classFqn ) {
105
- return true ;
106
- }
107
- }
108
-
109
- return false ;
110
- }
111
-
112
- /**
113
- * Generate a route name based on the designated route name method in
114
- * the given mapped document.
115
- *
116
- * Here we use the slugifier service given to this class to normalize
117
- * the title.
118
- *
119
- * @param object Mapped document
120
- *
121
- * @return string
122
- */
123
- protected function getRouteName ($ document )
124
- {
125
- $ metadata = $ this ->getMetadata ($ document );
126
-
127
- $ routeNameMethod = $ metadata ['route_method_name ' ];
128
- $ routeName = $ document ->$ routeNameMethod ();
129
- $ routeName = $ this ->slugifier ->slugify ($ routeName );
130
-
131
- return $ routeName ;
132
- }
133
-
134
- /**
135
- * Return the parent route for the generated AutoRoute.
136
- *
137
- * Currently we check to see if a base route path has been specified
138
- * in the given mapped document, if not we fall back to the global default.
139
- *
140
- * @TODO: Enable dynamic parents (e.g. name-of-my-blog/my-post)
141
- *
142
- * @param object Get parent route of this mapped document.
143
- *
144
- * @return Route
145
- */
146
- protected function getParentRoute ($ document )
147
- {
148
- $ metadata = $ this ->getMetadata ($ document );
149
- $ defaultPath = $ metadata ['base_path ' ] ? : $ this ->defaultPath ;
150
-
151
- if ($ metadata ['base_path_auto_create ' ]) {
152
- if (!$ this ->phpcrSession ->nodeExists ($ defaultPath )) {
153
- NodeHelper::createPath ($ this ->phpcrSession , $ defaultPath );
154
- }
155
- }
156
-
157
- $ parent = $ this ->dm ->find (null , $ defaultPath );
158
-
159
- if (!$ parent ) {
160
- throw new \Exception (sprintf (
161
- 'Could not find default route parent at path "%s" ' ,
162
- $ defaultPath
163
- ));
164
- }
165
-
166
- return $ parent ;
167
- }
168
-
169
- /**
170
- * Convenience method for retrieving Metadata.
171
- */
172
- protected function getMetadata ($ document )
173
- {
174
- foreach ($ this ->mapping as $ classFqn => $ metadata ) {
175
- if ($ document instanceof $ classFqn ) {
176
- return $ metadata ;
177
- }
178
- }
179
- }
180
-
181
- /**
182
- * Return the existing or a new AutoRoute for the given document.
183
- *
184
- * @throws \Exception If we have more than one
185
- *
186
- * @param object $document Mapped document that needs an AutoRoute
187
- *
188
- * @return AutoRoute
189
- */
190
- protected function getAutoRouteForDocument ($ document )
191
- {
192
- $ autoRoutes = array ();
193
-
194
- if ($ this ->isDocumentPersisted ($ document )) {
195
- $ autoRoutes = $ this ->fetchAutoRoutesForDocument ($ document );
196
- }
197
-
198
- $ locale = null ;
199
-
200
- if ($ locale ) {
201
- // filter non-matching locales, note that we could do this with the QueryBuilder
202
- // but currently searching array values is not supported by jackalope-doctrine-dbal.
203
- array_filter ($ res , function ($ route ) use ($ locale ) {
204
- if ($ route ->getDefault ('_locale ' ) != $ locale ) {
205
- return false ;
206
- }
207
-
208
- return true ;
209
- });
210
- }
211
-
212
- if (count ($ autoRoutes ) > 1 ) {
213
- throw new Exception \MoreThanOneAutoRoute ($ document );
214
- } elseif (count ($ autoRoutes ) == 1 ) {
215
- $ autoRoute = $ autoRoutes ->first ();
216
- } else {
217
- $ autoRoute = new AutoRoute ;
218
- $ autoRoute ->setRouteContent ($ document );
219
- }
220
-
221
- return $ autoRoute ;
222
- }
223
-
224
- /**
225
- * Fetch all the automatic routes for the given document
226
- *
227
- * @param object $document Mapped document
228
- *
229
- * @return array
230
- */
231
- public function fetchAutoRoutesForDocument ($ document )
232
- {
233
- $ routes = $ this ->dm ->getReferrers ($ document , null , 'routeContent ' );
234
- $ routes = $ routes ->filter (function ($ route ) {
235
- if ($ route instanceof AutoRoute) {
236
- return true ;
237
- }
238
-
239
- return false ;
240
- });
241
-
242
- return $ routes ;
243
- }
244
-
245
- public function getDefaultPath ()
246
- {
247
- return $ this ->defaultPath ;
248
- }
249
-
250
- protected function isDocumentPersisted ($ document )
251
- {
252
- $ metadata = $ this ->dm ->getClassMetadata (get_class ($ document ));
253
- $ id = $ metadata ->getIdentifierValue ($ document );
254
-
255
- return $ this ->phpcrSession ->nodeExists ($ id );
70
+ return $ this ->bucf ->hasMapping (ClassUtils::getClass ($ document ));
256
71
}
257
72
}
0 commit comments