summaryrefslogtreecommitdiffstatshomepage
path: root/core/lib/Drupal/Core/Routing/LazyRouteCollection.php
blob: 183bf04775e4859ae2d1467509cc7167684dc485 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php

namespace Drupal\Core\Routing;

use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

/**
 * The lazy route collection.
 */
class LazyRouteCollection extends RouteCollection {
  /**
   * The route provider for this generator.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $provider;

  /**
   * Constructs a LazyRouteCollection.
   */
  public function __construct(RouteProviderInterface $provider) {
    $this->provider = $provider;
  }

  /**
   * {@inheritdoc}
   */
  public function getIterator(): \ArrayIterator {
    return new \ArrayIterator($this->all());
  }

  /**
   * Gets the number of Routes in this collection.
   *
   * @return int
   *   The number of routes
   */
  public function count(): int {
    return count($this->all());
  }

  /**
   * Returns all routes in this collection.
   *
   * @return \Symfony\Component\Routing\Route[]
   *   An array of routes
   */
  public function all(): array {
    return $this->provider->getRoutesByNames(NULL);
  }

  /**
   * Gets a route by name.
   *
   * @param string $name
   *   The route name.
   *
   * @return \Symfony\Component\Routing\Route|null
   *   A Route instance or null when not found
   */
  public function get($name): ?Route {
    try {
      return $this->provider->getRouteByName($name);
    }
    catch (RouteNotFoundException) {
      return NULL;
    }
  }

}