You are here

class MongodbRouterRouteProvider in MongoDB 8

A Route Provider front-end for all Drupal-stored routes.

Hierarchy

Expanded class hierarchy of MongodbRouterRouteProvider

1 string reference to 'MongodbRouterRouteProvider'
mongodb.services.yml in ./mongodb.services.yml
mongodb.services.yml
1 service uses MongodbRouterRouteProvider
mongodb.router.route_provider in ./mongodb.services.yml
Drupal\mongodb\MongodbRouterRouteProvider

File

src/MongodbRouterRouteProvider.php, line 20
Contains Drupal\mongodb\MongodbRouterRouteProvider.

Namespace

Drupal\mongodb
View source
class MongodbRouterRouteProvider extends RouteProvider {

  /**
   * The name of the table.
   *
   * Warning: this is used by SqlRouterProvider::getCandidateOutlines().
   *
   * @var string
   */
  protected $tableName = 'routing';

  /**
   * @var \Drupal\mongoDb\MongoCollectionFactory $mongo
   */
  protected $mongo;

  /**
   * @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder
   *   The route builder.
   * @param \Drupal\Core\State\State $state
   *   The state.
   * @param string $table
   *   The table in the database to use for matching.
   */
  function __construct(MongoCollectionFactory $mongo, RouteBuilderInterface $route_builder, StateInterface $state, CurrentPathStack $current_path) {
    $this->mongo = $mongo;
    $this->routeBuilder = $route_builder;
    $this->state = $state;
    $this->currentPath = $current_path;
  }

  /**
   * {@inheritdoc}
   */
  public function getRoutesByNames($names, $parameters = []) {
    if (empty($names)) {
      throw new \InvalidArgumentException('You must specify the route names to load');
    }
    $routes_to_load = array_values(array_diff($names, array_keys($this->routes)));
    if ($routes_to_load) {
      $routes = $this->mongo
        ->get($this->tableName)
        ->find(array(
        '_id' => array(
          '$in' => $routes_to_load,
        ),
      ));
      foreach ($routes as $name => $route_array) {
        $this->routes[$name] = $this
          ->getRouteFromArray($route_array);
      }
    }
    return array_intersect_key($this->routes, array_flip($names));
  }

  /**
   * {@inheritdoc}
   */
  protected function getRoutesByPath($path) {

    // Filter out each empty value, though allow '0' and 0, which would be
    // filtered out by empty().
    $parts = array_values(array_filter(explode('/', $path), function ($value) {
      return $value !== NULL && $value !== '';
    }));
    $ancestors = $this
      ->getCandidateOutlines($parts);
    $routes = $this->mongo
      ->get($this->tableName)
      ->find(array(
      'pattern_outline' => array(
        '$in' => $ancestors,
      ),
    ))
      ->sort(array(
      'fit' => -1,
      '_id' => 1,
    ));
    $collection = new RouteCollection();
    foreach ($routes as $name => $route_array) {
      $route = $this
        ->getRouteFromArray($route_array);
      if (preg_match($route
        ->compile()
        ->getRegex(), $path, $matches)) {
        $collection
          ->add($name, $route);
      }
    }
    return $collection;
  }

  /**
   * Creates a Route object from an array.
   *
   * @param array $r
   *
   * @return \Symfony\Component\Routing\Route
   */
  protected function getRouteFromArray(array $r) {
    $r += array(
      'defaults' => array(),
      'requirements' => array(),
      'options' => array(),
      'host' => '',
      'schemes' => array(),
      'methods' => array(
        'GET',
        'POST',
      ),
      'condition' => '',
      'path' => $r['pattern_outline'],
    );
    return new Route($r['path'], $r['defaults'], $r['requirements'], $r['options'], $r['host'], $r['schemes'], $r['methods'], $r['condition']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MongodbRouterRouteProvider::$mongo protected property
MongodbRouterRouteProvider::$tableName protected property The name of the table. Overrides RouteProvider::$tableName
MongodbRouterRouteProvider::getRouteFromArray protected function Creates a Route object from an array.
MongodbRouterRouteProvider::getRoutesByNames public function Find many routes by their names using the provided list of names. Overrides RouteProvider::getRoutesByNames
MongodbRouterRouteProvider::getRoutesByPath protected function Get all routes which match a certain pattern. Overrides RouteProvider::getRoutesByPath
MongodbRouterRouteProvider::__construct function Overrides RouteProvider::__construct
RouteProvider::$cache protected property The cache backend.
RouteProvider::$cacheTagInvalidator protected property The cache tag invalidator.
RouteProvider::$connection protected property The database connection from which to read route information.
RouteProvider::$currentPath protected property The current path.
RouteProvider::$extraCacheKeyParts protected property An array of cache key parts to be used for the route match cache.
RouteProvider::$languageManager protected property The language manager.
RouteProvider::$pathProcessor protected property A path processor manager for resolving the system path.
RouteProvider::$routes protected property A cache of already-loaded routes, keyed by route name.
RouteProvider::$serializedRoutes protected property A cache of already-loaded serialized routes, keyed by route name.
RouteProvider::$state protected property The state.
RouteProvider::addExtraCacheKeyPart public function Adds a cache key part to be used in the cache ID of the route collection. Overrides CacheableRouteProviderInterface::addExtraCacheKeyPart
RouteProvider::getAllRoutes public function Returns all the routes on the system. Overrides RouteProviderInterface::getAllRoutes
RouteProvider::getCandidateOutlines protected function Returns an array of path pattern outlines that could match the path parts. 1
RouteProvider::getCurrentLanguageCacheIdPart protected function Returns the language identifier for the route collection cache.
RouteProvider::getRouteByName public function Find the route using the provided route name.
RouteProvider::getRouteCollectionCacheId protected function Returns the cache ID for the route collection cache.
RouteProvider::getRouteCollectionForRequest public function Finds routes that may potentially match the request.
RouteProvider::getRoutesByPattern public function Get all routes which match a certain pattern. Overrides RouteProviderInterface::getRoutesByPattern
RouteProvider::getRoutesCount public function Determines the total amount of routes.
RouteProvider::getRoutesPaged public function Find an amount of routes with an offset and possible a limit.
RouteProvider::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RouteProvider::preLoadRoutes public function Pre-load routes by their names using the provided list of names. Overrides PreloadableRouteProviderInterface::preLoadRoutes
RouteProvider::reset public function Resets the route provider object. Overrides RouteProviderInterface::reset
RouteProvider::routeProviderRouteCompare protected function Comparison function for usort on routes.
RouteProvider::ROUTE_LOAD_CID_PREFIX constant Cache ID prefix used to load routes.