LazyRouteCollection.php 1.6 KB

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