[Finder] tweak code

This commit is contained in:
Victor Berchet 2011-02-11 10:19:53 +01:00 committed by Fabien Potencier
parent a5bac4b5a3
commit bad3a97ad6
12 changed files with 75 additions and 32 deletions

View File

@ -39,13 +39,11 @@ class DateComparator extends Comparator
}
$operator = isset($matches[1]) ? $matches[1] : '==';
if ('since' === $operator || 'after' === $operator)
{
if ('since' === $operator || 'after' === $operator) {
$operator = '>';
}
if ('until' === $operator || 'before' === $operator)
{
if ('until' === $operator || 'before' === $operator) {
$operator = '<';
}

View File

@ -338,10 +338,10 @@ class Finder implements \IteratorAggregate
protected function searchInDirectory($dir)
{
$flags = \FilesystemIterator::SKIP_DOTS;
$flags = \RecursiveDirectoryIterator::SKIP_DOTS;
if ($this->followLinks) {
$flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
$flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
}
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST);

View File

@ -43,7 +43,7 @@ class CustomFilterIterator extends \FilterIterator
*/
public function accept()
{
$fileinfo = $this->getInnerIterator()->current();
$fileinfo = $this->current();
foreach ($this->filters as $filter) {
if (false === $filter($fileinfo)) {

View File

@ -40,7 +40,7 @@ class DateRangeFilterIterator extends \FilterIterator
*/
public function accept()
{
$fileinfo = $this->getInnerIterator()->current();
$fileinfo = $this->current();
if (!$fileinfo->isFile()) {
return true;

View File

@ -43,14 +43,10 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator
*/
public function accept()
{
$inner = $this;
while ($inner && !$inner->getInnerIterator() instanceof \RecursiveIteratorIterator) {
$inner = $inner->getInnerIterator();
}
$method = $inner->current()->isDir() ? 'getSubPathname' : 'getSubPath';
$path = $this->isDir() ? $this->getSubPathname() : $this->getSubPath();
$path = strtr($path, '\\', '/');
foreach ($this->patterns as $pattern) {
if (preg_match($pattern, strtr($this->getInnerIterator()->$method(), '\\', '/'))) {
if (preg_match($pattern, $path)) {
return false;
}
}

View File

@ -43,11 +43,9 @@ class FileTypeFilterIterator extends \FilterIterator
*/
public function accept()
{
$fileinfo = $this->getInnerIterator()->current();
if (self::ONLY_DIRECTORIES === (self::ONLY_DIRECTORIES & $this->mode) && $fileinfo->isFile()) {
if (self::ONLY_DIRECTORIES === (self::ONLY_DIRECTORIES & $this->mode) && $this->isFile()) {
return false;
} elseif (self::ONLY_FILES === (self::ONLY_FILES & $this->mode) && $fileinfo->isDir()) {
} elseif (self::ONLY_FILES === (self::ONLY_FILES & $this->mode) && $this->isDir()) {
return false;
}

View File

@ -52,13 +52,11 @@ class FilenameFilterIterator extends \FilterIterator
*/
public function accept()
{
$fileinfo = $this->getInnerIterator()->current();
// should at least match one rule
if ($this->matchRegexps) {
$match = false;
foreach ($this->matchRegexps as $regex) {
if (preg_match($regex, $fileinfo->getFilename())) {
if (preg_match($regex, $this->getFilename())) {
$match = true;
break;
}
@ -71,7 +69,7 @@ class FilenameFilterIterator extends \FilterIterator
if ($this->noMatchRegexps) {
$exclude = false;
foreach ($this->noMatchRegexps as $regex) {
if (preg_match($regex, $fileinfo->getFilename())) {
if (preg_match($regex, $this->getFilename())) {
$exclude = true;
break;
}

View File

@ -40,13 +40,11 @@ class SizeRangeFilterIterator extends \FilterIterator
*/
public function accept()
{
$fileinfo = $this->getInnerIterator()->current();
if (!$fileinfo->isFile()) {
if (!$this->isFile()) {
return true;
}
$filesize = $fileinfo->getSize();
$filesize = $this->getSize();
foreach ($this->comparators as $compare) {
if (!$compare->test($filesize)) {
return false;

View File

@ -29,12 +29,12 @@ class SortableIterator extends \ArrayIterator
*/
public function __construct(\Iterator $iterator, $sort)
{
if (!$sort instanceof \Closure && self::SORT_BY_NAME == $sort) {
if (self::SORT_BY_NAME === $sort) {
$sort = function ($a, $b)
{
return strcmp($a->getRealpath(), $b->getRealpath());
};
} elseif (!$sort instanceof \Closure && self::SORT_BY_TYPE == $sort) {
} elseif (self::SORT_BY_TYPE === $sort) {
$sort = function ($a, $b)
{
if ($a->isDir() && $b->isFile()) {

View File

@ -22,7 +22,7 @@ class FileTypeFilterIteratorTest extends RealIteratorTestCase
*/
public function testAccept($mode, $expected)
{
$inner = new Iterator(self::$files);
$inner = new InnerTypeIterator(self::$files);
$iterator = new FileTypeFilterIterator($inner, $mode);
@ -37,3 +37,21 @@ class FileTypeFilterIteratorTest extends RealIteratorTestCase
);
}
}
class InnerTypeIterator extends \ArrayIterator
{
public function current()
{
return new \SplFileInfo(parent::current());
}
public function isFile()
{
return $this->current()->isFile();
}
public function isDir()
{
return $this->current()->isDir();
}
}

View File

@ -22,7 +22,7 @@ class FilenameFilterIteratorTest extends IteratorTestCase
*/
public function testAccept($matchPatterns, $noMatchPatterns, $expected)
{
$inner = new Iterator(array('test.php', 'test.py', 'foo.php'));
$inner = new InnerNameIterator(array('test.php', 'test.py', 'foo.php'));
$iterator = new FilenameFilterIterator($inner, $matchPatterns, $noMatchPatterns);
@ -40,4 +40,18 @@ class FilenameFilterIteratorTest extends IteratorTestCase
array(array(), array('/\.php$/'), array('test.py')),
);
}
}
class InnerNameIterator extends \ArrayIterator
{
public function current()
{
return new \SplFileInfo(parent::current());
}
public function getFilename()
{
return parent::current();
}
}

View File

@ -23,7 +23,7 @@ class SizeRangeFilterIteratorTest extends RealIteratorTestCase
*/
public function testAccept($size, $expected)
{
$inner = new Iterator(self::$files);
$inner = new InnerSizeIterator(self::$files);
$iterator = new SizeRangeFilterIterator($inner, $size);
@ -37,3 +37,26 @@ class SizeRangeFilterIteratorTest extends RealIteratorTestCase
);
}
}
class InnerSizeIterator extends \ArrayIterator
{
public function current()
{
return new \SplFileInfo(parent::current());
}
public function getFilename()
{
return parent::current();
}
public function isFile()
{
return $this->current()->isFile();
}
public function getSize()
{
return $this->current()->getSize();
}
}