You are here

class ModuleRouteSubscriber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php \Drupal\Core\EventSubscriber\ModuleRouteSubscriber

A route subscriber to remove routes that depend on modules being enabled.

Hierarchy

Expanded class hierarchy of ModuleRouteSubscriber

1 file declares its use of ModuleRouteSubscriber
ModuleRouteSubscriberTest.php in core/tests/Drupal/Tests/Core/EventSubscriber/ModuleRouteSubscriberTest.php
Contains \Drupal\Tests\Core\EventSubscriber\ModuleRouteSubscriberTest.
1 string reference to 'ModuleRouteSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses ModuleRouteSubscriber
route_subscriber.module in core/core.services.yml
Drupal\Core\EventSubscriber\ModuleRouteSubscriber

File

core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php, line 17
Contains \Drupal\Core\EventSubscriber\ModuleRouteSubscriber.

Namespace

Drupal\Core\EventSubscriber
View source
class ModuleRouteSubscriber extends RouteSubscriberBase {

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a ModuleRouteSubscriber object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   */
  public function __construct(ModuleHandlerInterface $module_handler) {
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  protected function alterRoutes(RouteCollection $collection) {
    foreach ($collection as $name => $route) {
      if ($route
        ->hasRequirement('_module_dependencies')) {
        $modules = $route
          ->getRequirement('_module_dependencies');
        $explode_and = $this
          ->explodeString($modules, '+');
        if (count($explode_and) > 1) {
          foreach ($explode_and as $module) {

            // If any moduleExists() call returns FALSE, remove the route and
            // move on to the next.
            if (!$this->moduleHandler
              ->moduleExists($module)) {
              $collection
                ->remove($name);
              continue 2;
            }
          }
        }
        else {

          // OR condition, exploding on ',' character.
          foreach ($this
            ->explodeString($modules, ',') as $module) {
            if ($this->moduleHandler
              ->moduleExists($module)) {
              continue 2;
            }
          }

          // If no modules are found, and we get this far, remove the route.
          $collection
            ->remove($name);
        }
      }
    }
  }

  /**
   * Explodes a string based on a separator.
   *
   * @param string $string
   *   The string to explode.
   * @param string $separator
   *   The string separator to explode with.
   *
   * @return array
   *   An array of exploded (and trimmed) values.
   */
  protected function explodeString($string, $separator = ',') {
    return array_filter(array_map('trim', explode($separator, $string)));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ModuleRouteSubscriber::$moduleHandler protected property The module handler.
ModuleRouteSubscriber::alterRoutes protected function Alters existing routes for a specific collection. Overrides RouteSubscriberBase::alterRoutes
ModuleRouteSubscriber::explodeString protected function Explodes a string based on a separator.
ModuleRouteSubscriber::__construct public function Constructs a ModuleRouteSubscriber object.
RouteSubscriberBase::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents 5
RouteSubscriberBase::onAlterRoutes public function Delegates the route altering to self::alterRoutes(). 1