add FilesystemIterator

This was inserted as a class inbetween RecursiveArrayIterator and DirectoryIterator. Sadly our RecursiveArrayIterator implementaiton is crazy and calls into C++ which can't call back up to `parent::method()`. I'll fix RecursiveArrayIterator in another diff.
Esse commit está contido em:
Paul Tarjan
2013-05-23 00:41:27 -07:00
commit de sgolemon
commit 992cd4efb6
17 arquivos alterados com 337 adições e 17 exclusões
+2 -6
Ver Arquivo
@@ -203,8 +203,7 @@ static const int64_t LEAVES_ONLY = 0L;
static const int64_t SELF_FIRST = 1L;
static const int64_t CHILD_FIRST = 2L;
// Class constants that we use from RecursiveDirectoryIterator
static const int64_t CURRENT_AS_FILEINFO = 16L;
// Class constants that we use from FilesystemIterator
static const int64_t CURRENT_AS_PATHNAME = 32L;
static const int64_t KEY_AS_FILENAME = 256L;
@@ -419,10 +418,7 @@ Variant f_hphp_recursivedirectoryiterator_current(CObjRef obj) {
if (rdi->m_flags & CURRENT_AS_PATHNAME) {
return pathName;
}
if (rdi->m_flags & CURRENT_AS_FILEINFO) {
return SystemLib::AllocSplFileInfoObject(pathName);
}
return obj;
return SystemLib::AllocSplFileInfoObject(pathName);
}
bool f_hphp_recursivedirectoryiterator_haschildren(CObjRef obj) {
+161 -11
Ver Arquivo
@@ -130,6 +130,161 @@ class DirectoryIterator extends SplFileInfo implements Traversable,
}
// This doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/class.filesystemiterator.php )
*
* The Filesystem iterator
*
*/
class FilesystemIterator extends DirectoryIterator
implements SeekableIterator, Traversable, Iterator {
const CURRENT_AS_PATHNAME = 32;
const CURRENT_AS_FILEINFO = 0;
const CURRENT_AS_SELF = 16;
const CURRENT_MODE_MASK = 240;
const KEY_AS_PATHNAME = 0;
const KEY_AS_FILENAME = 256;
const FOLLOW_SYMLINKS = 512;
const KEY_MODE_MASK = 3840;
const NEW_CURRENT_AND_KEY = 256;
const SKIP_DOTS = 4096;
const UNIX_PATHS = 8192;
private $flags;
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/filesystemiterator.construct.php
* )
*
* Constructs a new filesystem iterator from the path.
*
* @path mixed The path of the filesystem item to be iterated over.
* @flags mixed Flags may be provided which will affect the behavior
* of some methods. A list of the flags can found under
* FilesystemIterator predefined constants. They can
* also be set later with
* FilesystemIterator::setFlags()
*
* @return mixed No value is returned.
*/
public function __construct(string $path, int $flags = null) {
parent::__construct($path);
if ($flags === null) {
$flags = FilesystemIterator::KEY_AS_PATHNAME |
FilesystemIterator::CURRENT_AS_FILEINFO |
FilesystemIterator::SKIP_DOTS;
}
$this->flags = $flags;
$this->goPastDotsIfNeeded();
}
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/filesystemiterator.current.php )
*
* Get file information of the current element.
*
* @return mixed The filename, file information, or $this depending
* on the set flags. See the FilesystemIterator
* constants.
*/
public function current() {
$f = parent::current();
if ($this->flags & FilesystemIterator::CURRENT_AS_PATHNAME) {
return $f->getPathname();
} else if ($this->flags & FilesystemIterator::CURRENT_AS_SELF) {
return $this;
}
// FilesystemIterator::CURRENT_AS_FILEINFO == 0
return $f;
}
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/filesystemiterator.getflags.php
* )
*
* Gets the handling flags, as set in FilesystemIterator::__construct() or
* FilesystemIterator::setFlags().
*
* @return mixed The integer value of the set flags.
*/
public function getFlags() {
return $this->flags;
}
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/filesystemiterator.key.php )
*
*
* @return mixed Returns the pathname or filename depending on the
* set flags. See the FilesystemIterator constants.
*/
public function key() {
if ($this->flags & FilesystemIterator::KEY_AS_FILENAME) {
return parent::current()->getFileName();
}
// FilesystemIterator::KEY_AS_PATHNAME == 0
return parent::current()->getPathName();
}
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/filesystemiterator.next.php )
*
* Move to the next file.
*
* @return mixed No value is returned.
*/
public function next() {
parent::next();
$this->goPastDotsIfNeeded();
}
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/filesystemiterator.rewind.php )
*
* Rewinds the directory back to the start.
*
* @return mixed No value is returned.
*/
public function rewind() {
parent::rewind();
$this->goPastDotsIfNeeded();
}
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from http://php.net/manual/en/filesystemiterator.setflags.php
* )
*
* Sets handling flags.
*
* @flags mixed The handling flags to set. See the
* FilesystemIterator constants.
*
* @return mixed No value is returned.
*/
public function setFlags(int $flags) {
$this->flags = $flags;
}
private function goPastDotsIfNeeded() {
if ($this->flags & FilesystemIterator::SKIP_DOTS) {
$f = parent::current();
while ($f && $f->isDot()) {
parent::next();
$f = parent::current();
}
}
}
}
// Do NOT modifiy this doc comment block generated by idl/sysdoc.php
/**
* ( excerpt from
* http://php.net/manual/en/class.recursivedirectoryiterator.php )
@@ -138,15 +293,8 @@ class DirectoryIterator extends SplFileInfo implements Traversable,
* recursively over filesystem directories.
*
*/
class RecursiveDirectoryIterator extends DirectoryIterator
class RecursiveDirectoryIterator extends FilesystemIterator
implements RecursiveIterator {
const CURRENT_AS_SELF = 0x0;
const CURRENT_AS_FILEINFO = 0x00000010;
const CURRENT_AS_PATHNAME = 0x00000020;
const KEY_AS_PATHNAME = 0x0;
const KEY_AS_FILENAME = 0x00000100;
const NEW_CURRENT_AND_KEY = 0x00000110;
// This doc comment block generated by idl/sysdoc.php
/**
@@ -165,8 +313,11 @@ class RecursiveDirectoryIterator extends DirectoryIterator
* @return mixed Returns the newly created
* RecursiveDirectoryIterator.
*/
function __construct($path,
$flags = RecursiveDirectoryIterator::CURRENT_AS_FILEINFO) {
function __construct($path, $flags = null) {
if ($flags === null) {
$flags = FilesystemIterator::KEY_AS_PATHNAME |
FilesystemIterator::CURRENT_AS_FILEINFO;
}
if (!hphp_recursivedirectoryiterator___construct($this, $path, $flags)) {
throw new UnexpectedValueException(
"RecursiveDirectoryIterator::__construct($path): failed to open dir");
@@ -280,4 +431,3 @@ class RecursiveDirectoryIterator extends DirectoryIterator
return hphp_recursivedirectoryiterator_getsubpathname($this);
}
}
+1
Ver Arquivo
@@ -44,6 +44,7 @@ namespace Eval {
x(RecursiveDirectoryIterator) \
x(RecursiveIteratorIterator) \
x(DirectoryIterator) \
x(FilesystemIterator) \
x(SplFileInfo) \
x(SplFileObject) \
x(DOMDocument) \
@@ -0,0 +1,9 @@
<?php
$sample_dir = __DIR__.'/../../sample_dir';
$it = new FilesystemIterator($sample_dir);
$ret = array();
foreach ($it as $fileinfo) {
$ret[] = $fileinfo->getFilename();
}
asort($ret);
var_dump(array_values($ret));
@@ -0,0 +1,12 @@
array(5) {
[0]=>
string(3) "dir"
[1]=>
string(5) "empty"
[2]=>
string(4) "file"
[3]=>
string(14) "fix_mtimes.inc"
[4]=>
string(7) "symlink"
}
@@ -0,0 +1,12 @@
<?php
$sample_dir = __DIR__.'/../../sample_dir';
$iterator = new FilesystemIterator(
$sample_dir,
FilesystemIterator::CURRENT_AS_PATHNAME
);
$ret = array();
foreach ($iterator as $fileinfo) {
$ret[] = $iterator->current();
}
asort($ret);
var_dump(array_values($ret));
@@ -0,0 +1,16 @@
array(7) {
[0]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/."
[1]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/.."
[2]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/dir"
[3]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/empty"
[4]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/file"
[5]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/fix_mtimes.inc"
[6]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/symlink"
}
+12
Ver Arquivo
@@ -0,0 +1,12 @@
<?php
$sample_dir = __DIR__.'/../../sample_dir';
$iterator = new FilesystemIterator(
$sample_dir,
FilesystemIterator::KEY_AS_FILENAME
);
$ret = array();
foreach ($iterator as $fileinfo) {
$ret[] = $iterator->key();
}
asort($ret);
var_dump(array_values($ret));
@@ -0,0 +1,16 @@
array(7) {
[0]=>
string(1) "."
[1]=>
string(2) ".."
[2]=>
string(3) "dir"
[3]=>
string(5) "empty"
[4]=>
string(4) "file"
[5]=>
string(14) "fix_mtimes.inc"
[6]=>
string(7) "symlink"
}
+10
Ver Arquivo
@@ -0,0 +1,10 @@
<?php
$sample_dir = __DIR__.'/../../sample_dir';
$iterator = new FilesystemIterator($sample_dir);
$ret = array();
while($iterator->valid()) {
$ret[] = $iterator->getFilename();
$iterator->next();
}
asort($ret);
var_dump(array_values($ret));
@@ -0,0 +1,12 @@
array(5) {
[0]=>
string(3) "dir"
[1]=>
string(5) "empty"
[2]=>
string(4) "file"
[3]=>
string(14) "fix_mtimes.inc"
[4]=>
string(7) "symlink"
}
@@ -0,0 +1,15 @@
<?php
$sample_dir = __DIR__.'/../../sample_dir';
$iterator = new FilesystemIterator(
$sample_dir,
FilesystemIterator::KEY_AS_FILENAME
);
$a = $iterator->key();
$iterator->next();
$b = $iterator->key();
$iterator->rewind();
$c = $iterator->key();
var_dump($a == $b);
var_dump($a == $c);
@@ -0,0 +1,2 @@
bool(false)
bool(true)
@@ -0,0 +1,22 @@
<?php
$sample_dir = __DIR__.'/../../sample_dir';
$iterator = new FilesystemIterator(
$sample_dir,
FilesystemIterator::KEY_AS_PATHNAME
);
echo "Key as Pathname:\n";
$ret = array();
foreach ($iterator as $key => $fileinfo) {
$ret[] = $key;
}
asort($ret);
var_dump(array_values($ret));
$iterator->setFlags(FilesystemIterator::KEY_AS_FILENAME);
echo "\nKey as Filename:\n";
$ret = array();
foreach ($iterator as $key => $fileinfo) {
$ret[] = $key;
}
asort($ret);
var_dump(array_values($ret));
@@ -0,0 +1,35 @@
Key as Pathname:
array(7) {
[0]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/."
[1]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/.."
[2]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/dir"
[3]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/empty"
[4]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/file"
[5]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/fix_mtimes.inc"
[6]=>
string(%d) "%s/hphp/test/slow/filesystem_iterator/../../sample_dir/symlink"
}
Key as Filename:
array(7) {
[0]=>
string(1) "."
[1]=>
string(2) ".."
[2]=>
string(3) "dir"
[3]=>
string(5) "empty"
[4]=>
string(4) "file"
[5]=>
string(14) "fix_mtimes.inc"
[6]=>
string(7) "symlink"
}