Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_classes_ResourceIterator | |
0.00% |
0 / 38 |
|
0.00% |
0 / 9 |
420 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| rewind | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| current | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| next | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| valid | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| ensureNotEmpty | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| load | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| loadResources | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * Iterates over the resources of classes |
| 25 | * |
| 26 | * @author Joel Bout <joel@taotesting.com> |
| 27 | */ |
| 28 | class core_kernel_classes_ResourceIterator implements \Iterator |
| 29 | { |
| 30 | public const CACHE_SIZE = 100; |
| 31 | |
| 32 | private $classIterator; |
| 33 | |
| 34 | /** |
| 35 | * Id of the current instance |
| 36 | * |
| 37 | * @var int |
| 38 | */ |
| 39 | private $currentInstance = 0; |
| 40 | |
| 41 | /** |
| 42 | * List of resource uris currently being iterated over |
| 43 | * |
| 44 | * @var array |
| 45 | */ |
| 46 | private $instanceCache = null; |
| 47 | |
| 48 | /** |
| 49 | * Indicator whenever the end of the current cache is also the end of the current class |
| 50 | * |
| 51 | * @var boolean |
| 52 | */ |
| 53 | private $endOfClass = false; |
| 54 | |
| 55 | /** |
| 56 | * Whenever we already moved the pointer, used to prevent unnecessary rewinds |
| 57 | * |
| 58 | * @var boolean |
| 59 | */ |
| 60 | private $unmoved = true; |
| 61 | |
| 62 | /** |
| 63 | * Constructor of the iterator expecting a class or classes as argument |
| 64 | * |
| 65 | * @param mixed $classes array/instance of class(es) to iterate over |
| 66 | */ |
| 67 | public function __construct($classes) |
| 68 | { |
| 69 | $this->classIterator = new core_kernel_classes_ClassIterator($classes); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * (non-PHPdoc) |
| 74 | * @see Iterator::rewind() |
| 75 | */ |
| 76 | public function rewind() |
| 77 | { |
| 78 | if (!$this->unmoved) { |
| 79 | $this->classIterator->rewind(); |
| 80 | $this->ensureNotEmpty(); |
| 81 | $this->unmoved = true; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @return core_kernel_classes_Resource|null |
| 87 | * @throws common_exception_Error |
| 88 | */ |
| 89 | public function current() |
| 90 | { |
| 91 | if ($this->instanceCache === null) { |
| 92 | $this->ensureNotEmpty(); |
| 93 | } |
| 94 | |
| 95 | return isset($this->instanceCache[$this->currentInstance]) ? |
| 96 | new \core_kernel_classes_Resource($this->instanceCache[$this->currentInstance]) : |
| 97 | null; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * (non-PHPdoc) |
| 102 | * @see Iterator::key() |
| 103 | */ |
| 104 | public function key() |
| 105 | { |
| 106 | return $this->classIterator->key() . '#' . $this->currentInstance; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * (non-PHPdoc) |
| 111 | * @see Iterator::next() |
| 112 | */ |
| 113 | public function next() |
| 114 | { |
| 115 | $this->unmoved = false; |
| 116 | if ($this->valid()) { |
| 117 | $this->currentInstance++; |
| 118 | if (!isset($this->instanceCache[$this->currentInstance])) { |
| 119 | // try to load next block (unless we know it's empty) |
| 120 | $remainingInstances = !$this->endOfClass |
| 121 | && $this->load($this->classIterator->current(), $this->currentInstance); |
| 122 | |
| 123 | // endOfClass or failed loading |
| 124 | if (!$remainingInstances) { |
| 125 | $this->classIterator->next(); |
| 126 | $this->ensureNotEmpty(); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * While there are remaining classes there are instances to load |
| 134 | * |
| 135 | * (non-PHPdoc) |
| 136 | * @see Iterator::valid() |
| 137 | */ |
| 138 | public function valid() |
| 139 | { |
| 140 | if ($this->instanceCache === null) { |
| 141 | $this->ensureNotEmpty(); |
| 142 | } |
| 143 | return $this->classIterator->valid(); |
| 144 | } |
| 145 | |
| 146 | // Helpers |
| 147 | |
| 148 | /** |
| 149 | * Ensure the class iterator is pointin to a non empty class |
| 150 | * Loads the first resource block to test this |
| 151 | */ |
| 152 | protected function ensureNotEmpty() |
| 153 | { |
| 154 | $this->currentInstance = 0; |
| 155 | while ($this->classIterator->valid() && !$this->load($this->classIterator->current(), 0)) { |
| 156 | $this->classIterator->next(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Load instances into cache |
| 162 | * |
| 163 | * @param core_kernel_classes_Class $class |
| 164 | * @param int $offset |
| 165 | * @return boolean |
| 166 | */ |
| 167 | protected function load(core_kernel_classes_Class $class, $offset) |
| 168 | { |
| 169 | $results = $this->loadResources($class, $offset); |
| 170 | $this->instanceCache = []; |
| 171 | foreach ($results as $resource) { |
| 172 | $this->instanceCache[$offset] = $resource->getUri(); |
| 173 | $offset++; |
| 174 | } |
| 175 | |
| 176 | $this->endOfClass = count($results) < self::CACHE_SIZE; |
| 177 | |
| 178 | return count($results) > 0; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Load resources from storage |
| 183 | * |
| 184 | * @param core_kernel_classes_Class $class |
| 185 | * @param integer $offset |
| 186 | * @return core_kernel_classes_Resource[] |
| 187 | */ |
| 188 | protected function loadResources(core_kernel_classes_Class $class, $offset) |
| 189 | { |
| 190 | return $class->searchInstances([], [ |
| 191 | 'recursive' => false, |
| 192 | 'limit' => self::CACHE_SIZE, |
| 193 | 'offset' => $offset, |
| 194 | ]); |
| 195 | } |
| 196 | } |