Complex classes like ConventionalLoader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConventionalLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class ConventionalLoader extends YamlFileLoader |
||
12 | { |
||
13 | private static $supportedControllerKeys = array( |
||
14 | 'prefix', 'defaults', 'requirements', 'options', 'collections', 'resources' |
||
15 | ); |
||
16 | private static $supportedActionKeys = array( |
||
17 | 'pattern', 'defaults', 'requirements', 'options', 'methods' |
||
18 | ); |
||
19 | private $yaml; |
||
20 | |||
21 | public function __construct(FileLocatorInterface $locator, YamlParser $yaml = null) |
||
27 | |||
28 | public function supports($resource, $type = null) |
||
32 | |||
33 | public function load($file, $type = null) |
||
34 | { |
||
35 | $path = $this->locator->locate($file); |
||
36 | $config = $this->yaml->parse($path); |
||
37 | |||
38 | $collection = new RouteCollection(); |
||
39 | $collection->addResource(new FileResource($file)); |
||
40 | |||
41 | if (null === $config) { |
||
42 | return $collection; |
||
43 | } |
||
44 | |||
45 | if (!is_array($config)) { |
||
46 | throw new \InvalidArgumentException(sprintf( |
||
47 | 'The file "%s" must contain a YAML array.', $path |
||
48 | )); |
||
49 | } |
||
50 | |||
51 | foreach ($config as $shortname => $mapping) { |
||
52 | $parts = explode(':', $shortname); |
||
53 | |||
54 | if (3 == count($parts)) { |
||
55 | list($bundle, $class, $action) = $parts; |
||
56 | |||
57 | $routeName = $this->getRouteName($bundle, $class, $action); |
||
58 | $route = $this->getCustomCollectionRoute($bundle, $class, $action); |
||
59 | |||
60 | $this->overrideRouteParams($shortname, $route, $mapping); |
||
61 | $collection->add($routeName, $route); |
||
62 | |||
63 | continue; |
||
64 | } |
||
65 | |||
66 | if (1 == count($parts)) { |
||
67 | $this->parseClassical($collection, $shortname, $mapping, $path, $file); |
||
68 | |||
69 | continue; |
||
70 | } |
||
71 | |||
72 | if (is_array($mapping)) { |
||
73 | foreach ($mapping as $key => $val) { |
||
74 | if (in_array($key, self::$supportedControllerKeys)) { |
||
75 | continue; |
||
76 | } |
||
77 | |||
78 | if ('pattern' === $key) { |
||
79 | throw new \InvalidArgumentException( |
||
80 | 'The `pattern` is only supported for actions, if you want to prefix '. |
||
81 | 'all the routes of the controller, use `prefix` instead.' |
||
82 | ); |
||
83 | } |
||
84 | |||
85 | throw new \InvalidArgumentException(sprintf( |
||
86 | '`%s` parameter is not supported by `%s` controller route. Use one of [%s].', |
||
87 | $key, $shortname, implode(', ', self::$supportedControllerKeys) |
||
88 | )); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | list($bundle, $class) = $parts; |
||
93 | |||
94 | $prefix = $this->getPatternPrefix($class, $mapping); |
||
95 | $collectionDefaults = $this->getDefaultsFromMapping($mapping, 'collections'); |
||
96 | $collectionRequirements = $this->getRequirementsFromMapping($mapping, 'collections'); |
||
97 | $collectionOptions = $this->getOptionsFromMapping($mapping, 'collections'); |
||
98 | $resourceDefaults = $this->getDefaultsFromMapping($mapping, 'resources'); |
||
99 | $resourceRequirements = $this->getRequirementsFromMapping($mapping, 'resources'); |
||
100 | $resourceOptions = $this->getOptionsFromMapping($mapping, 'resources'); |
||
101 | |||
102 | $collectionRoutes = $this->getCollectionRoutesFromMapping($shortname, $mapping, $bundle, $class); |
||
103 | $resourceRoutes = $this->getResourceRoutesFromMapping($shortname, $mapping, $bundle, $class); |
||
104 | |||
105 | $controllerCollection = new RouteCollection(); |
||
106 | foreach ($collectionRoutes as $name => $route) { |
||
107 | $route->setDefaults(array_merge($collectionDefaults, $route->getDefaults())); |
||
108 | $route->setRequirements(array_merge( |
||
109 | $collectionRequirements, $route->getRequirements() |
||
110 | )); |
||
111 | $route->setOptions(array_merge( |
||
112 | $collectionOptions, $route->getOptions() |
||
113 | )); |
||
114 | $controllerCollection->add($name, $route); |
||
115 | } |
||
116 | foreach ($resourceRoutes as $name => $route) { |
||
117 | $route->setDefaults(array_merge($resourceDefaults, $route->getDefaults())); |
||
118 | $route->setRequirements(array_merge( |
||
119 | $resourceRequirements, $route->getRequirements() |
||
120 | )); |
||
121 | $route->setOptions(array_merge( |
||
122 | $resourceOptions, $route->getOptions() |
||
123 | )); |
||
124 | $controllerCollection->add($name, $route); |
||
125 | } |
||
126 | $controllerCollection->addPrefix($prefix); |
||
127 | $collection->addCollection($controllerCollection); |
||
128 | } |
||
129 | |||
130 | return $collection; |
||
131 | } |
||
132 | |||
133 | protected function parseClassical(RouteCollection $collection, $shortname, |
||
192 | |||
193 | private function getDefaultsFromMapping($mapping, $routeType = 'collections') |
||
213 | |||
214 | private function getRequirementsFromMapping($mapping, $routeType = 'collections') |
||
234 | |||
235 | private function getOptionsFromMapping($mapping, $routeType = 'collections') |
||
236 | { |
||
237 | $options = array(); |
||
238 | |||
239 | if (!is_array($mapping)) { |
||
240 | return $options; |
||
241 | } |
||
242 | |||
243 | if (isset($mapping['options'])) { |
||
244 | $options = $mapping['options']; |
||
245 | } |
||
246 | |||
247 | if (isset($mapping[$routeType]) && is_array($mapping[$routeType])) { |
||
248 | if (isset($mapping[$routeType]['options'])) { |
||
249 | $options = array_merge($options, $mapping[$routeType]['options']); |
||
250 | } |
||
251 | } |
||
252 | |||
253 | return $options; |
||
254 | } |
||
255 | |||
256 | private function getCollectionRoutesFromMapping($shortname, $mapping, $bundle, $class) |
||
257 | { |
||
258 | $defaults = $this->getDefaultCollectionRoutes($bundle, $class); |
||
259 | if (!is_array($mapping) || !isset($mapping['collections'])) { |
||
260 | return $defaults; |
||
261 | } |
||
262 | |||
263 | $collections = $mapping['collections']; |
||
264 | unset($collections['defaults']); |
||
265 | unset($collections['requirements']); |
||
266 | unset($collections['options']); |
||
267 | |||
268 | if (0 == count($collections)) { |
||
269 | return $defaults; |
||
270 | } |
||
271 | |||
272 | if (false === $collections) { |
||
273 | return array(); |
||
274 | } |
||
275 | |||
276 | $routes = array(); |
||
277 | foreach ($collections as $action => $params) { |
||
278 | if (is_integer($action)) { |
||
279 | $action = $params; |
||
280 | $params = null; |
||
281 | } |
||
282 | |||
283 | $routeName = $this->getRouteName($bundle, $class, $action); |
||
284 | if (isset($defaults[$routeName])) { |
||
285 | $route = $defaults[$routeName]; |
||
286 | } else { |
||
287 | $route = $this->getCustomCollectionRoute($bundle, $class, $action); |
||
288 | } |
||
289 | |||
290 | $this->overrideRouteParams($shortname, $route, $params); |
||
291 | |||
292 | $routes[$routeName] = $route; |
||
293 | } |
||
294 | |||
295 | return $routes; |
||
296 | } |
||
297 | |||
298 | private function getResourceRoutesFromMapping($shortname, $mapping, $bundle, $class) |
||
299 | { |
||
300 | $defaults = $this->getDefaultResourceRoutes($bundle, $class); |
||
301 | if (!is_array($mapping) || !isset($mapping['resources'])) { |
||
302 | return $defaults; |
||
303 | } |
||
304 | |||
305 | $resources = $mapping['resources']; |
||
306 | unset($resources['defaults']); |
||
307 | unset($resources['requirements']); |
||
308 | unset($resources['options']); |
||
309 | |||
310 | if (0 == count($resources)) { |
||
311 | return $defaults; |
||
312 | } |
||
313 | |||
314 | if (false === $resources) { |
||
315 | return array(); |
||
316 | } |
||
317 | |||
318 | $routes = array(); |
||
319 | foreach ($resources as $action => $params) { |
||
320 | if (is_integer($action)) { |
||
321 | $action = $params; |
||
322 | $params = null; |
||
323 | } |
||
324 | |||
325 | $routeName = $this->getRouteName($bundle, $class, $action); |
||
326 | if (isset($defaults[$routeName])) { |
||
327 | $route = $defaults[$routeName]; |
||
328 | } else { |
||
329 | $route = $this->getCustomResourceRoute($bundle, $class, $action); |
||
330 | } |
||
331 | |||
332 | $this->overrideRouteParams($shortname, $route, $params); |
||
333 | |||
334 | $routes[$routeName] = $route; |
||
335 | } |
||
336 | |||
337 | return $routes; |
||
338 | } |
||
339 | |||
340 | private function overrideRouteParams($shortname, Route $route, $params) |
||
341 | { |
||
342 | if (is_array($params)) { |
||
343 | foreach ($params as $key => $val) { |
||
344 | if (in_array($key, self::$supportedActionKeys)) { |
||
345 | continue; |
||
346 | } |
||
347 | |||
348 | throw new \InvalidArgumentException(sprintf( |
||
349 | '`%s` parameter is not supported by `%s` action route. Use one of [%s].', |
||
350 | $key, $shortname, implode(', ', self::$supportedActionKeys) |
||
351 | )); |
||
352 | } |
||
353 | } |
||
354 | |||
355 | if (is_string($params)) { |
||
356 | $route->setPattern($params); |
||
357 | } |
||
358 | if (is_array($params)) { |
||
359 | if (isset($params['pattern'])) { |
||
360 | $route->setPattern($params['pattern']); |
||
361 | } |
||
362 | if (isset($params['defaults'])) { |
||
363 | $route->setDefaults(array_merge( |
||
364 | $route->getDefaults(), $params['defaults'] |
||
365 | )); |
||
366 | } |
||
367 | if (isset($params['requirements'])) { |
||
368 | $route->setRequirements($params['requirements']); |
||
369 | } |
||
370 | if (isset($params['options'])) { |
||
371 | $route->setOptions($params['options']); |
||
372 | } |
||
373 | if (isset($params['methods'])) { |
||
374 | $route->setMethods($params['methods']); |
||
375 | } |
||
376 | } |
||
377 | } |
||
378 | |||
379 | private function getDefaultCollectionRoutes($bundle, $class) |
||
399 | |||
400 | private function getCustomCollectionRoute($bundle, $class, $action) |
||
408 | |||
409 | private function getDefaultResourceRoutes($bundle, $class) |
||
434 | |||
435 | private function getCustomResourceRoute($bundle, $class, $action) |
||
443 | |||
444 | private function getPatternPrefix($class, $mapping) |
||
454 | |||
455 | private function getRouteName($bundle, $class, $action) |
||
461 | } |
||
462 |