37f8812225ae03738e49a640a8370d03ebe8960c
[yaffs-website] / web / core / modules / system / src / Tests / Routing / MockRouteProvider.php
1 <?php
2
3 namespace Drupal\system\Tests\Routing;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\Routing\Exception\RouteNotFoundException;
7 use Symfony\Component\Routing\RouteCollection;
8 use Drupal\Core\Routing\RouteProviderInterface;
9
10 /**
11  * Easily configurable mock route provider.
12  */
13 class MockRouteProvider implements RouteProviderInterface {
14
15   /**
16    * A collection of routes for this route provider.
17    *
18    * @var RouteCollection
19    */
20   protected $routes;
21
22   /**
23    * Constructs a new MockRouteProvider.
24    *
25    * @param \Symfony\Component\Routing\RouteCollection $routes
26    *   The route collection to use for this provider.
27    */
28   public function __construct(RouteCollection $routes) {
29     $this->routes = $routes;
30   }
31
32   /**
33    * Implements \Symfony\Cmf\Component\Routing\RouteProviderInterface::getRouteCollectionForRequest().
34    *
35    * Simply return all routes to prevent
36    * \Symfony\Component\Routing\Exception\ResourceNotFoundException.
37    */
38   public function getRouteCollectionForRequest(Request $request) {
39     return $this->routes;
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public function getRouteByName($name) {
46     $routes = $this->getRoutesByNames([$name]);
47     if (empty($routes)) {
48       throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
49     }
50
51     return reset($routes);
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function preLoadRoutes($names) {
58     // Nothing to do.
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function getRoutesByNames($names) {
65     $routes = [];
66     foreach ($names as $name) {
67       $routes[] = $this->routes->get($name);
68     }
69
70     return $routes;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function getRoutesByPattern($pattern) {
77     return new RouteCollection();
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function getAllRoutes() {
84     return $this->routes->all();
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function reset() {
91     $this->routes = [];
92   }
93
94 }