Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony CMF package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) 2011-2015 Symfony CMF
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Cmf\Component\Routing;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
Chris@0
|
15 use Symfony\Component\Routing\RouteCollection;
|
Chris@0
|
16 use Symfony\Component\Routing\Route;
|
Chris@0
|
17
|
Chris@0
|
18 class LazyRouteCollection extends RouteCollection
|
Chris@0
|
19 {
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The route provider for this generator.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var RouteProviderInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $provider;
|
Chris@0
|
26
|
Chris@0
|
27 public function __construct(RouteProviderInterface $provider)
|
Chris@0
|
28 {
|
Chris@0
|
29 $this->provider = $provider;
|
Chris@0
|
30 }
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * {@inheritdoc}
|
Chris@0
|
34 */
|
Chris@0
|
35 public function getIterator()
|
Chris@0
|
36 {
|
Chris@0
|
37 return new \ArrayIterator($this->all());
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Gets the number of Routes in this collection.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @return int The number of routes
|
Chris@0
|
44 */
|
Chris@0
|
45 public function count()
|
Chris@0
|
46 {
|
Chris@0
|
47 return count($this->all());
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 /**
|
Chris@0
|
51 * Returns all routes in this collection.
|
Chris@0
|
52 *
|
Chris@0
|
53 * @return Route[] An array of routes
|
Chris@0
|
54 */
|
Chris@0
|
55 public function all()
|
Chris@0
|
56 {
|
Chris@0
|
57 return $this->provider->getRoutesByNames(null);
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * Gets a route by name.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @param string $name The route name
|
Chris@0
|
64 *
|
Chris@0
|
65 * @return Route|null A Route instance or null when not found
|
Chris@0
|
66 */
|
Chris@0
|
67 public function get($name)
|
Chris@0
|
68 {
|
Chris@0
|
69 try {
|
Chris@0
|
70 return $this->provider->getRouteByName($name);
|
Chris@0
|
71 } catch (RouteNotFoundException $e) {
|
Chris@0
|
72 return;
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|
Chris@0
|
75 }
|