|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Storage\Adapter; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Storage\Adapter\Exception\PoolIdentifierDoesNotExistException; |
|
28
|
|
|
use Brickoo\Component\Storage\Adapter\Exception\PoolIsEmptyException; |
|
29
|
|
|
use Brickoo\Component\Validation\Constraint\ContainsInstancesOfConstraint; |
|
30
|
|
|
use Brickoo\Component\Common\Assert; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* AdapterPoolIterator |
|
34
|
|
|
* |
|
35
|
|
|
* Implementation of an iterable caching adapter pool. |
|
36
|
|
|
* @author Celestino Diaz <[email protected]> |
|
37
|
|
|
*/ |
|
38
|
|
|
class AdapterPoolIterator implements \Iterator, \Countable, AdapterPool { |
|
39
|
|
|
|
|
40
|
|
|
/** @var array containing entries of type \Brickoo\Component\Storage\Adapter\Adapter */ |
|
41
|
|
|
private $poolEntries; |
|
42
|
|
|
|
|
43
|
|
|
/** @var array */ |
|
44
|
|
|
private $mappingKeys; |
|
45
|
|
|
|
|
46
|
|
|
/** @var integer */ |
|
47
|
|
|
private $currentPointerPosition; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Class constructor. |
|
51
|
|
|
* @param array $poolEntries |
|
52
|
|
|
* @throws \InvalidArgumentException if a pool entry does not match expected type |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function __construct(array $poolEntries) { |
|
55
|
2 |
|
if ((!empty($poolEntries)) |
|
56
|
2 |
|
&& (!(new ContainsInstancesOfConstraint("\\Brickoo\\Component\\Storage\\Adapter\\Adapter"))->matches($poolEntries))) { |
|
57
|
1 |
|
throw new \InvalidArgumentException(sprintf("%s: The pool entries must implement the Adapter interface.", __CLASS__)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
$this->poolEntries = array_values($poolEntries); |
|
61
|
1 |
|
$this->mappingKeys = array_keys($poolEntries); |
|
62
|
1 |
|
$this->currentPointerPosition = 0; |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Checks if the current pool adapter entry is ready. |
|
67
|
|
|
* @throws \Brickoo\Component\Storage\Adapter\Exception\PoolIsEmptyException |
|
68
|
|
|
* @return boolean check result |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function isCurrentReady() { |
|
71
|
2 |
|
return $this->current()->isReady(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
* @throws \Brickoo\Component\Storage\Adapter\Exception\PoolIsEmptyException |
|
77
|
|
|
* @return \Brickoo\Component\Storage\Adapter\Adapter |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function current() { |
|
80
|
2 |
|
if ($this->isEmpty() || (!isset($this->poolEntries[$this->currentPointerPosition]))) { |
|
81
|
1 |
|
throw new PoolIsEmptyException(); |
|
82
|
|
|
} |
|
83
|
2 |
|
return $this->poolEntries[$this->currentPointerPosition]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritDoc} |
|
88
|
|
|
* @return string the current pool key |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function key() { |
|
91
|
1 |
|
if (isset($this->mappingKeys[$this->currentPointerPosition])) { |
|
92
|
1 |
|
return $this->mappingKeys[$this->currentPointerPosition]; |
|
93
|
|
|
} |
|
94
|
1 |
|
return (string)$this->currentPointerPosition; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** {@inheritDoc} */ |
|
98
|
1 |
|
public function next() { |
|
99
|
1 |
|
$this->currentPointerPosition++; |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** {@inheritDoc} */ |
|
103
|
1 |
|
public function rewind() { |
|
104
|
1 |
|
$this->currentPointerPosition = 0; |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** {@inheritDoc} */ |
|
108
|
1 |
|
public function valid() { |
|
109
|
1 |
|
return isset($this->poolEntries[$this->currentPointerPosition]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** {@inheritDoc} */ |
|
113
|
2 |
|
public function select($adapterIdentifier) { |
|
114
|
2 |
|
Assert::isStringOrInteger($adapterIdentifier); |
|
115
|
|
|
|
|
116
|
2 |
|
if (!$this->has($adapterIdentifier)) { |
|
117
|
1 |
|
throw new PoolIdentifierDoesNotExistException($adapterIdentifier); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
$this->currentPointerPosition = $this->getMappingPosition($adapterIdentifier); |
|
121
|
1 |
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** {@inheritDoc} */ |
|
125
|
4 |
|
public function remove($adapterIdentifier) { |
|
126
|
4 |
|
if (!$this->has($adapterIdentifier)) { |
|
127
|
1 |
|
throw new PoolIdentifierDoesNotExistException($adapterIdentifier); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
3 |
|
$mappingPosition = $this->getMappingPosition($adapterIdentifier); |
|
131
|
3 |
|
unset($this->poolEntries[$mappingPosition]); |
|
132
|
3 |
|
unset($this->mappingKeys[$mappingPosition]); |
|
133
|
|
|
|
|
134
|
3 |
|
$this->poolEntries = array_values($this->poolEntries); |
|
135
|
3 |
|
$this->mappingKeys = array_values($this->mappingKeys); |
|
136
|
|
|
|
|
137
|
3 |
|
if ($this->currentPointerPosition > 0 && $this->currentPointerPosition >= $mappingPosition) { |
|
138
|
1 |
|
--$this->currentPointerPosition; |
|
139
|
1 |
|
} |
|
140
|
|
|
|
|
141
|
3 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** {@inheritDoc} */ |
|
145
|
1 |
|
public function has($adapterIdentifier) { |
|
146
|
1 |
|
Assert::isStringOrInteger($adapterIdentifier); |
|
147
|
1 |
|
return in_array($adapterIdentifier, $this->mappingKeys, false); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** {@inheritDoc} */ |
|
151
|
3 |
|
public function isEmpty() { |
|
152
|
3 |
|
return empty($this->poolEntries); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** {@inheritDoc} */ |
|
156
|
1 |
|
public function count() { |
|
157
|
1 |
|
return count($this->poolEntries); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Returns the position inside the adapter pool. |
|
162
|
|
|
* @param string $adapterIdentifier |
|
163
|
|
|
* @return integer the position |
|
164
|
|
|
*/ |
|
165
|
1 |
|
private function getMappingPosition($adapterIdentifier) { |
|
166
|
1 |
|
return intval(array_search($adapterIdentifier, $this->mappingKeys, false)); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
|