4 * This file is part of the Symfony CMF package.
6 * (c) 2011-2015 Symfony CMF
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Cmf\Component\Routing;
15 * Provides a route collection which avoids having all routes in memory.
17 * Internally, this does load multiple routes over time using a
18 * PagedRouteProviderInterface $route_provider.
20 class PagedRouteCollection implements \Iterator, \Countable
23 * @var PagedRouteProviderInterface
28 * Stores the amount of routes which are loaded in parallel and kept in
33 protected $routesBatchSize;
36 * Contains the current item the iterator points to.
40 protected $current = -1;
43 * Stores the current loaded routes.
45 * @var \Symfony\Component\Routing\Route[]
47 protected $currentRoutes;
49 public function __construct(PagedRouteProviderInterface $pagedRouteProvider, $routesBatchSize = 50)
51 $this->provider = $pagedRouteProvider;
52 $this->routesBatchSize = $routesBatchSize;
56 * Loads the next routes into the elements array.
58 * @param int $offset The offset used in the db query.
60 protected function loadNextElements($offset)
62 // If the last batch was smaller than the batch size, this means there
63 // are no more routes available.
64 if (isset($this->currentRoutes) && count($this->currentRoutes) < $this->routesBatchSize) {
65 $this->currentRoutes = array();
67 $this->currentRoutes = $this->provider->getRoutesPaged($offset, $this->routesBatchSize);
74 public function current()
76 return current($this->currentRoutes);
82 public function next()
84 $result = next($this->currentRoutes);
85 if (false === $result) {
86 $this->loadNextElements($this->current + 1);
96 return key($this->currentRoutes);
102 public function valid()
104 return key($this->currentRoutes);
110 public function rewind()
113 $this->currentRoutes = null;
114 $this->loadNextElements($this->current);
118 * Gets the number of Routes in this collection.
120 * @return int The number of routes
122 public function count()
124 return $this->provider->getRoutesCount();