Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\EventSubscriber;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Extension\ModuleHandlerInterface;
|
Chris@0
|
6 use Drupal\Core\Routing\RouteSubscriberBase;
|
Chris@0
|
7 use Symfony\Component\Routing\RouteCollection;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * A route subscriber to remove routes that depend on modules being enabled.
|
Chris@0
|
11 */
|
Chris@0
|
12 class ModuleRouteSubscriber extends RouteSubscriberBase {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * The module handler.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @var \Drupal\Core\Extension\ModuleHandlerInterface
|
Chris@0
|
18 */
|
Chris@0
|
19 protected $moduleHandler;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Constructs a ModuleRouteSubscriber object.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
Chris@0
|
25 * The module handler.
|
Chris@0
|
26 */
|
Chris@0
|
27 public function __construct(ModuleHandlerInterface $module_handler) {
|
Chris@0
|
28 $this->moduleHandler = $module_handler;
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * {@inheritdoc}
|
Chris@0
|
33 */
|
Chris@0
|
34 protected function alterRoutes(RouteCollection $collection) {
|
Chris@0
|
35 foreach ($collection as $name => $route) {
|
Chris@0
|
36 if ($route->hasRequirement('_module_dependencies')) {
|
Chris@0
|
37 $modules = $route->getRequirement('_module_dependencies');
|
Chris@0
|
38
|
Chris@0
|
39 $explode_and = $this->explodeString($modules, '+');
|
Chris@0
|
40 if (count($explode_and) > 1) {
|
Chris@0
|
41 foreach ($explode_and as $module) {
|
Chris@0
|
42 // If any moduleExists() call returns FALSE, remove the route and
|
Chris@0
|
43 // move on to the next.
|
Chris@0
|
44 if (!$this->moduleHandler->moduleExists($module)) {
|
Chris@0
|
45 $collection->remove($name);
|
Chris@0
|
46 continue 2;
|
Chris@0
|
47 }
|
Chris@0
|
48 }
|
Chris@0
|
49 }
|
Chris@0
|
50 else {
|
Chris@0
|
51 // OR condition, exploding on ',' character.
|
Chris@0
|
52 foreach ($this->explodeString($modules, ',') as $module) {
|
Chris@0
|
53 if ($this->moduleHandler->moduleExists($module)) {
|
Chris@0
|
54 continue 2;
|
Chris@0
|
55 }
|
Chris@0
|
56 }
|
Chris@0
|
57 // If no modules are found, and we get this far, remove the route.
|
Chris@0
|
58 $collection->remove($name);
|
Chris@0
|
59 }
|
Chris@0
|
60 }
|
Chris@0
|
61 }
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Explodes a string based on a separator.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @param string $string
|
Chris@0
|
68 * The string to explode.
|
Chris@0
|
69 * @param string $separator
|
Chris@0
|
70 * The string separator to explode with.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @return array
|
Chris@0
|
73 * An array of exploded (and trimmed) values.
|
Chris@0
|
74 */
|
Chris@0
|
75 protected function explodeString($string, $separator = ',') {
|
Chris@0
|
76 return array_filter(array_map('trim', explode($separator, $string)));
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 }
|