| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Poor man's PHP 5.2 port of FilesystemIterator from PHP 5.3. |
| 5 |
* |
| 6 |
* WARNING: Do not use this one in PHP >=5.3, use native FilesystemIterator instead. |
| 7 |
* |
| 8 |
* @see http://php.net/manual/en/class.filesystemiterator.php |
| 9 |
* @see https://github.com/php/php-src/blob/master/ext/spl/spl_directory.c |
| 10 |
*/ |
| 11 |
class Symfony_Filesystem_FilesystemIterator extends DirectoryIterator implements SeekableIterator |
| 12 |
{ |
| 13 |
const CURRENT_AS_PATHNAME = 32; |
| 14 |
const CURRENT_AS_FILEINFO = 0; |
| 15 |
const CURRENT_AS_SELF = 16; |
| 16 |
const CURRENT_MODE_MASK = 240; |
| 17 |
const KEY_AS_PATHNAME = 0; |
| 18 |
const KEY_AS_FILENAME = 256; |
| 19 |
const FOLLOW_SYMLINKS = 512; |
| 20 |
const KEY_MODE_MASK = 3840; |
| 21 |
const NEW_CURRENT_AND_KEY = 256; |
| 22 |
const SKIP_DOTS = 4096; |
| 23 |
const UNIX_PATHS = 8192; |
| 24 |
|
| 25 |
private $flags; |
| 26 |
|
| 27 |
/** |
| 28 |
* @param string $path |
| 29 |
* @param int $flags |
| 30 |
*/ |
| 31 |
public function __construct($path, $flags = null) |
| 32 |
{ |
| 33 |
if ($flags === null) { |
| 34 |
$flags = self::KEY_AS_PATHNAME | self::CURRENT_AS_FILEINFO | self::SKIP_DOTS; |
| 35 |
} |
| 36 |
$this->flags = $flags; |
| 37 |
parent::__construct($path); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @return mixed |
| 42 |
*/ |
| 43 |
public function current() |
| 44 |
{ |
| 45 |
if ($this->flags & self::CURRENT_AS_PATHNAME) { |
| 46 |
return $this->getPathname(); |
| 47 |
} elseif ($this->flags & self::CURRENT_AS_SELF) { |
| 48 |
return $this; |
| 49 |
} else { |
| 50 |
return $this->getFileInfo(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public function key() |
| 58 |
{ |
| 59 |
if ($this->flags & self::KEY_AS_FILENAME) { |
| 60 |
return $this->getFilename(); |
| 61 |
} |
| 62 |
|
| 63 |
return $this->getPathname(); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
*/ |
| 68 |
public function next() |
| 69 |
{ |
| 70 |
do { |
| 71 |
parent::next(); |
| 72 |
} while ($this->isDot()); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
*/ |
| 77 |
public function rewind() |
| 78 |
{ |
| 79 |
parent::rewind(); |
| 80 |
while ($this->isDot()) { |
| 81 |
$this->next(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* @param int $flags |
| 87 |
*/ |
| 88 |
public function setFlags($flags) |
| 89 |
{ |
| 90 |
$this->flags = $flags; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @return int |
| 95 |
*/ |
| 96 |
public function getFlags() |
| 97 |
{ |
| 98 |
return $this->flags; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Overridden to not crash PHP 5.2, because DirectoryIterator::seek was added in 5.3. |
| 103 |
* |
| 104 |
* @link http://lxr.php.net/xref/PHP_5_3/ext/spl/spl_directory.c#807 |
| 105 |
*/ |
| 106 |
public function seek($position) |
| 107 |
{ |
| 108 |
if (is_callable(array('parent', 'seek'))) { |
| 109 |
parent::seek($position); |
| 110 |
|
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
if ($this->key() > $position) { |
| 115 |
$this->rewind(); |
| 116 |
} |
| 117 |
|
| 118 |
while ($this->key() < $position) { |
| 119 |
if (!$this->valid()) { |
| 120 |
return; |
| 121 |
} |
| 122 |
$this->next(); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|