Drupal investigation

PagedRouteCollection.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /**
  12. * Provides a route collection which avoids having all routes in memory.
  13. *
  14. * Internally, this does load multiple routes over time using a
  15. * PagedRouteProviderInterface $route_provider.
  16. */
  17. class PagedRouteCollection implements \Iterator, \Countable
  18. {
  19. /**
  20. * @var PagedRouteProviderInterface
  21. */
  22. protected $provider;
  23. /**
  24. * Stores the amount of routes which are loaded in parallel and kept in
  25. * memory.
  26. *
  27. * @var int
  28. */
  29. protected $routesBatchSize;
  30. /**
  31. * Contains the current item the iterator points to.
  32. *
  33. * @var int
  34. */
  35. protected $current = -1;
  36. /**
  37. * Stores the current loaded routes.
  38. *
  39. * @var \Symfony\Component\Routing\Route[]
  40. */
  41. protected $currentRoutes;
  42. public function __construct(PagedRouteProviderInterface $pagedRouteProvider, $routesBatchSize = 50)
  43. {
  44. $this->provider = $pagedRouteProvider;
  45. $this->routesBatchSize = $routesBatchSize;
  46. }
  47. /**
  48. * Loads the next routes into the elements array.
  49. *
  50. * @param int $offset The offset used in the db query.
  51. */
  52. protected function loadNextElements($offset)
  53. {
  54. // If the last batch was smaller than the batch size, this means there
  55. // are no more routes available.
  56. if (isset($this->currentRoutes) && count($this->currentRoutes) < $this->routesBatchSize) {
  57. $this->currentRoutes = array();
  58. } else {
  59. $this->currentRoutes = $this->provider->getRoutesPaged($offset, $this->routesBatchSize);
  60. }
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function current()
  66. {
  67. return current($this->currentRoutes);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function next()
  73. {
  74. $result = next($this->currentRoutes);
  75. if (false === $result) {
  76. $this->loadNextElements($this->current + 1);
  77. }
  78. ++$this->current;
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function key()
  84. {
  85. return key($this->currentRoutes);
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. public function valid()
  91. {
  92. return key($this->currentRoutes);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function rewind()
  98. {
  99. $this->current = 0;
  100. $this->currentRoutes = null;
  101. $this->loadNextElements($this->current);
  102. }
  103. /**
  104. * Gets the number of Routes in this collection.
  105. *
  106. * @return int The number of routes
  107. */
  108. public function count()
  109. {
  110. return $this->provider->getRoutesCount();
  111. }
  112. }