This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
DbaIterator.php
196 lines (170 loc) · 4.17 KB
/
DbaIterator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Cache
*/
namespace Zend\Cache\Storage\Adapter;
use Zend\Cache\Exception;
use Zend\Cache\Storage\IteratorInterface;
/**
* @category Zend
* @package Zend_Cache
* @subpackage Storage
*/
class DbaIterator implements IteratorInterface
{
/**
* The apc storage instance
*
* @var Apc
*/
protected $storage;
/**
* The iterator mode
*
* @var int
*/
protected $mode = IteratorInterface::CURRENT_AS_KEY;
/**
* The dba resource handle
*
* @var resource
*/
protected $handle;
/**
* The length of the namespace prefix
*
* @var int
*/
protected $prefixLength;
/**
* The current internal key
*
* @var string|bool
*/
protected $currentInternalKey;
/**
* Constructor
*
* @param Dba $storage
* @param resource $handle
* @param string $prefix
*/
public function __construct(Dba $storage, $handle, $prefix)
{
$this->storage = $storage;
$this->handle = $handle;
$this->prefixLength = strlen($prefix);
$this->rewind();
}
/**
* Get storage instance
*
* @return Dba
*/
public function getStorage()
{
return $this->storage;
}
/**
* Get iterator mode
*
* @return int Value of IteratorInterface::CURRENT_AS_*
*/
public function getMode()
{
return $this->mode;
}
/**
* Set iterator mode
*
* @param int $mode
* @return ApcIterator Fluent interface
*/
public function setMode($mode)
{
$this->mode = (int) $mode;
return $this;
}
/* Iterator */
/**
* Get current key, value or metadata.
*
* @return mixed
* @throws Exception\RuntimeException
*/
public function current()
{
if ($this->mode == IteratorInterface::CURRENT_AS_SELF) {
return $this;
}
$key = $this->key();
if ($this->mode == IteratorInterface::CURRENT_AS_VALUE) {
return $this->storage->getItem($key);
} elseif ($this->mode == IteratorInterface::CURRENT_AS_METADATA) {
return $this->storage->getMetadata($key);
}
return $key;
}
/**
* Get current key
*
* @return string
* @throws Exception\RuntimeException
*/
public function key()
{
if ($this->currentInternalKey === false) {
throw new Exception\RuntimeException("Iterater is on an invalid state");
}
// remove namespace prefix
return substr($this->currentInternalKey, $this->prefixLength);
}
/**
* Move forward to next element
*
* @return void
* @throws Exception\RuntimeException
*/
public function next()
{
if ($this->currentInternalKey === false) {
throw new Exception\RuntimeException("Iterater is on an invalid state");
}
$this->currentInternalKey = dba_nextkey($this->handle);
// Workaround for PHP-Bug #62492
if ($this->currentInternalKey === null) {
$this->currentInternalKey = false;
}
}
/**
* Checks if current position is valid
*
* @return bool
*/
public function valid()
{
return ($this->currentInternalKey !== false);
}
/**
* Rewind the Iterator to the first element.
*
* @return void
* @throws Exception\RuntimeException
*/
public function rewind()
{
if ($this->currentInternalKey === false) {
throw new Exception\RuntimeException("Iterator is on an invalid state");
}
$this->currentInternalKey = dba_firstkey($this->handle);
// Workaround for PHP-Bug #62492
if ($this->currentInternalKey === null) {
$this->currentInternalKey = false;
}
}
}