3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 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
;
13 use Zend\Cache\Storage\IteratorInterface
;
15 class FilesystemIterator
implements IteratorInterface
19 * The Filesystem storage instance
30 protected $mode = IteratorInterface
::CURRENT_AS_KEY
;
33 * The GlobIterator instance
37 protected $globIterator;
40 * The namespace sprefix
47 * String length of namespace prefix
51 protected $prefixLength;
56 * @param Filesystem $storage
58 * @param string $prefix
60 public function __construct(Filesystem
$storage, $path, $prefix)
62 $this->storage
= $storage;
63 $this->globIterator
= new GlobIterator($path, GlobIterator
::KEY_AS_FILENAME
);
64 $this->prefix
= $prefix;
65 $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 FilesystemIterator Fluent interface
94 public function setMode($mode)
96 $this->mode
= (int) $mode;
103 * Get current key, value or metadata.
107 public function current()
109 if ($this->mode
== IteratorInterface
::CURRENT_AS_SELF
) {
115 if ($this->mode
== IteratorInterface
::CURRENT_AS_VALUE
) {
116 return $this->storage
->getItem($key);
117 } elseif ($this->mode
== IteratorInterface
::CURRENT_AS_METADATA
) {
118 return $this->storage
->getMetadata($key);
129 public function key()
131 $filename = $this->globIterator
->key();
133 // return without namespace prefix and file suffix
134 return substr($filename, $this->prefixLength
, -4);
138 * Move forward to next element
142 public function next()
144 $this->globIterator
->next();
148 * Checks if current position is valid
152 public function valid()
155 return $this->globIterator
->valid();
156 } catch (\LogicException
$e) {
157 // @link https://bugs.php.net/bug.php?id=55701
158 // GlobIterator throws LogicException with message
159 // 'The parent constructor was not called: the object is in an invalid state'
165 * Rewind the Iterator to the first element.
167 * @return bool false if the operation failed.
169 public function rewind()
172 return $this->globIterator
->rewind();
173 } catch (\LogicException
$e) {
174 // @link https://bugs.php.net/bug.php?id=55701
175 // GlobIterator throws LogicException with message
176 // 'The parent constructor was not called: the object is in an invalid state'