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