3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Cache\Storage\Adapter
;
12 use Zend\Cache\Exception
;
13 use Zend\Cache\Storage\IteratorInterface
;
15 class DbaIterator
implements IteratorInterface
18 * The apc storage instance
29 protected $mode = IteratorInterface
::CURRENT_AS_KEY
;
32 * The dba resource handle
39 * The length of the namespace prefix
43 protected $prefixLength;
46 * The current internal key
50 protected $currentInternalKey;
56 * @param resource $handle
57 * @param string $prefix
59 public function __construct(Dba
$storage, $handle, $prefix)
61 $this->storage
= $storage;
62 $this->handle
= $handle;
63 $this->prefixLength
= strlen($prefix);
69 * Get storage instance
73 public function getStorage()
75 return $this->storage
;
81 * @return int Value of IteratorInterface::CURRENT_AS_*
83 public function getMode()
92 * @return DbaIterator Provides a fluent interface
94 public function setMode($mode)
96 $this->mode
= (int) $mode;
103 * Get current key, value or metadata.
106 * @throws Exception\RuntimeException
108 public function current()
110 if ($this->mode
== IteratorInterface
::CURRENT_AS_SELF
) {
116 if ($this->mode
== IteratorInterface
::CURRENT_AS_VALUE
) {
117 return $this->storage
->getItem($key);
118 } elseif ($this->mode
== IteratorInterface
::CURRENT_AS_METADATA
) {
119 return $this->storage
->getMetadata($key);
129 * @throws Exception\RuntimeException
131 public function key()
133 if ($this->currentInternalKey
=== false) {
134 throw new Exception\
RuntimeException("Iterator is on an invalid state");
137 // remove namespace prefix
138 return substr($this->currentInternalKey
, $this->prefixLength
);
142 * Move forward to next element
145 * @throws Exception\RuntimeException
147 public function next()
149 if ($this->currentInternalKey
=== false) {
150 throw new Exception\
RuntimeException("Iterator is on an invalid state");
153 $this->currentInternalKey
= dba_nextkey($this->handle
);
155 // Workaround for PHP-Bug #62492
156 if ($this->currentInternalKey
=== null) {
157 $this->currentInternalKey
= false;
162 * Checks if current position is valid
166 public function valid()
168 return ($this->currentInternalKey
!== false);
172 * Rewind the Iterator to the first element.
175 * @throws Exception\RuntimeException
177 public function rewind()
179 if ($this->currentInternalKey
=== false) {
180 throw new Exception\
RuntimeException("Iterator is on an invalid state");
183 $this->currentInternalKey
= dba_firstkey($this->handle
);
185 // Workaround for PHP-Bug #62492
186 if ($this->currentInternalKey
=== null) {
187 $this->currentInternalKey
= false;