ae02b6e309470102f4f829ce472b788d3dddb619
[yaffs-website] / vendor / symfony-cmf / routing / PagedRouteCollection.php
1 <?php
2
3 /*
4  * This file is part of the Symfony CMF package.
5  *
6  * (c) 2011-2015 Symfony CMF
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Cmf\Component\Routing;
13
14 /**
15  * Provides a route collection which avoids having all routes in memory.
16  *
17  * Internally, this does load multiple routes over time using a
18  * PagedRouteProviderInterface $route_provider.
19  */
20 class PagedRouteCollection implements \Iterator, \Countable
21 {
22     /**
23      * @var PagedRouteProviderInterface
24      */
25     protected $provider;
26
27     /**
28      * Stores the amount of routes which are loaded in parallel and kept in
29      * memory.
30      *
31      * @var int
32      */
33     protected $routesBatchSize;
34
35     /**
36      * Contains the current item the iterator points to.
37      *
38      * @var int
39      */
40     protected $current = -1;
41
42     /**
43      * Stores the current loaded routes.
44      *
45      * @var \Symfony\Component\Routing\Route[]
46      */
47     protected $currentRoutes;
48
49     public function __construct(PagedRouteProviderInterface $pagedRouteProvider, $routesBatchSize = 50)
50     {
51         $this->provider = $pagedRouteProvider;
52         $this->routesBatchSize = $routesBatchSize;
53     }
54
55     /**
56      * Loads the next routes into the elements array.
57      *
58      * @param int $offset The offset used in the db query.
59      */
60     protected function loadNextElements($offset)
61     {
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();
66         } else {
67             $this->currentRoutes = $this->provider->getRoutesPaged($offset, $this->routesBatchSize);
68         }
69     }
70
71     /**
72      * {@inheritdoc}
73      */
74     public function current()
75     {
76         return current($this->currentRoutes);
77     }
78
79     /**
80      * {@inheritdoc}
81      */
82     public function next()
83     {
84         $result = next($this->currentRoutes);
85         if (false === $result) {
86             $this->loadNextElements($this->current + 1);
87         }
88         ++$this->current;
89     }
90
91     /**
92      * {@inheritdoc}
93      */
94     public function key()
95     {
96         return key($this->currentRoutes);
97     }
98
99     /**
100      * {@inheritdoc}
101      */
102     public function valid()
103     {
104         return key($this->currentRoutes);
105     }
106
107     /**
108      * {@inheritdoc}
109      */
110     public function rewind()
111     {
112         $this->current = 0;
113         $this->currentRoutes = null;
114         $this->loadNextElements($this->current);
115     }
116
117     /**
118      * Gets the number of Routes in this collection.
119      *
120      * @return int The number of routes
121      */
122     public function count()
123     {
124         return $this->provider->getRoutesCount();
125     }
126 }