[Finder] tweak code
This commit is contained in:
parent
a5bac4b5a3
commit
bad3a97ad6
@ -39,13 +39,11 @@ class DateComparator extends Comparator
|
|||||||
}
|
}
|
||||||
|
|
||||||
$operator = isset($matches[1]) ? $matches[1] : '==';
|
$operator = isset($matches[1]) ? $matches[1] : '==';
|
||||||
if ('since' === $operator || 'after' === $operator)
|
if ('since' === $operator || 'after' === $operator) {
|
||||||
{
|
|
||||||
$operator = '>';
|
$operator = '>';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('until' === $operator || 'before' === $operator)
|
if ('until' === $operator || 'before' === $operator) {
|
||||||
{
|
|
||||||
$operator = '<';
|
$operator = '<';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -338,10 +338,10 @@ class Finder implements \IteratorAggregate
|
|||||||
|
|
||||||
protected function searchInDirectory($dir)
|
protected function searchInDirectory($dir)
|
||||||
{
|
{
|
||||||
$flags = \FilesystemIterator::SKIP_DOTS;
|
$flags = \RecursiveDirectoryIterator::SKIP_DOTS;
|
||||||
|
|
||||||
if ($this->followLinks) {
|
if ($this->followLinks) {
|
||||||
$flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
|
$flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
|
||||||
}
|
}
|
||||||
|
|
||||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
|
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
|
||||||
|
@ -43,7 +43,7 @@ class CustomFilterIterator extends \FilterIterator
|
|||||||
*/
|
*/
|
||||||
public function accept()
|
public function accept()
|
||||||
{
|
{
|
||||||
$fileinfo = $this->getInnerIterator()->current();
|
$fileinfo = $this->current();
|
||||||
|
|
||||||
foreach ($this->filters as $filter) {
|
foreach ($this->filters as $filter) {
|
||||||
if (false === $filter($fileinfo)) {
|
if (false === $filter($fileinfo)) {
|
||||||
|
@ -40,7 +40,7 @@ class DateRangeFilterIterator extends \FilterIterator
|
|||||||
*/
|
*/
|
||||||
public function accept()
|
public function accept()
|
||||||
{
|
{
|
||||||
$fileinfo = $this->getInnerIterator()->current();
|
$fileinfo = $this->current();
|
||||||
|
|
||||||
if (!$fileinfo->isFile()) {
|
if (!$fileinfo->isFile()) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -43,14 +43,10 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator
|
|||||||
*/
|
*/
|
||||||
public function accept()
|
public function accept()
|
||||||
{
|
{
|
||||||
$inner = $this;
|
$path = $this->isDir() ? $this->getSubPathname() : $this->getSubPath();
|
||||||
while ($inner && !$inner->getInnerIterator() instanceof \RecursiveIteratorIterator) {
|
$path = strtr($path, '\\', '/');
|
||||||
$inner = $inner->getInnerIterator();
|
|
||||||
}
|
|
||||||
|
|
||||||
$method = $inner->current()->isDir() ? 'getSubPathname' : 'getSubPath';
|
|
||||||
foreach ($this->patterns as $pattern) {
|
foreach ($this->patterns as $pattern) {
|
||||||
if (preg_match($pattern, strtr($this->getInnerIterator()->$method(), '\\', '/'))) {
|
if (preg_match($pattern, $path)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -43,11 +43,9 @@ class FileTypeFilterIterator extends \FilterIterator
|
|||||||
*/
|
*/
|
||||||
public function accept()
|
public function accept()
|
||||||
{
|
{
|
||||||
$fileinfo = $this->getInnerIterator()->current();
|
if (self::ONLY_DIRECTORIES === (self::ONLY_DIRECTORIES & $this->mode) && $this->isFile()) {
|
||||||
|
|
||||||
if (self::ONLY_DIRECTORIES === (self::ONLY_DIRECTORIES & $this->mode) && $fileinfo->isFile()) {
|
|
||||||
return false;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,13 +52,11 @@ class FilenameFilterIterator extends \FilterIterator
|
|||||||
*/
|
*/
|
||||||
public function accept()
|
public function accept()
|
||||||
{
|
{
|
||||||
$fileinfo = $this->getInnerIterator()->current();
|
|
||||||
|
|
||||||
// should at least match one rule
|
// should at least match one rule
|
||||||
if ($this->matchRegexps) {
|
if ($this->matchRegexps) {
|
||||||
$match = false;
|
$match = false;
|
||||||
foreach ($this->matchRegexps as $regex) {
|
foreach ($this->matchRegexps as $regex) {
|
||||||
if (preg_match($regex, $fileinfo->getFilename())) {
|
if (preg_match($regex, $this->getFilename())) {
|
||||||
$match = true;
|
$match = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -71,7 +69,7 @@ class FilenameFilterIterator extends \FilterIterator
|
|||||||
if ($this->noMatchRegexps) {
|
if ($this->noMatchRegexps) {
|
||||||
$exclude = false;
|
$exclude = false;
|
||||||
foreach ($this->noMatchRegexps as $regex) {
|
foreach ($this->noMatchRegexps as $regex) {
|
||||||
if (preg_match($regex, $fileinfo->getFilename())) {
|
if (preg_match($regex, $this->getFilename())) {
|
||||||
$exclude = true;
|
$exclude = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -40,13 +40,11 @@ class SizeRangeFilterIterator extends \FilterIterator
|
|||||||
*/
|
*/
|
||||||
public function accept()
|
public function accept()
|
||||||
{
|
{
|
||||||
$fileinfo = $this->getInnerIterator()->current();
|
if (!$this->isFile()) {
|
||||||
|
|
||||||
if (!$fileinfo->isFile()) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$filesize = $fileinfo->getSize();
|
$filesize = $this->getSize();
|
||||||
foreach ($this->comparators as $compare) {
|
foreach ($this->comparators as $compare) {
|
||||||
if (!$compare->test($filesize)) {
|
if (!$compare->test($filesize)) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -29,12 +29,12 @@ class SortableIterator extends \ArrayIterator
|
|||||||
*/
|
*/
|
||||||
public function __construct(\Iterator $iterator, $sort)
|
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)
|
$sort = function ($a, $b)
|
||||||
{
|
{
|
||||||
return strcmp($a->getRealpath(), $b->getRealpath());
|
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)
|
$sort = function ($a, $b)
|
||||||
{
|
{
|
||||||
if ($a->isDir() && $b->isFile()) {
|
if ($a->isDir() && $b->isFile()) {
|
||||||
|
@ -22,7 +22,7 @@ class FileTypeFilterIteratorTest extends RealIteratorTestCase
|
|||||||
*/
|
*/
|
||||||
public function testAccept($mode, $expected)
|
public function testAccept($mode, $expected)
|
||||||
{
|
{
|
||||||
$inner = new Iterator(self::$files);
|
$inner = new InnerTypeIterator(self::$files);
|
||||||
|
|
||||||
$iterator = new FileTypeFilterIterator($inner, $mode);
|
$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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -22,7 +22,7 @@ class FilenameFilterIteratorTest extends IteratorTestCase
|
|||||||
*/
|
*/
|
||||||
public function testAccept($matchPatterns, $noMatchPatterns, $expected)
|
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);
|
$iterator = new FilenameFilterIterator($inner, $matchPatterns, $noMatchPatterns);
|
||||||
|
|
||||||
@ -40,4 +40,18 @@ class FilenameFilterIteratorTest extends IteratorTestCase
|
|||||||
array(array(), array('/\.php$/'), array('test.py')),
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ class SizeRangeFilterIteratorTest extends RealIteratorTestCase
|
|||||||
*/
|
*/
|
||||||
public function testAccept($size, $expected)
|
public function testAccept($size, $expected)
|
||||||
{
|
{
|
||||||
$inner = new Iterator(self::$files);
|
$inner = new InnerSizeIterator(self::$files);
|
||||||
|
|
||||||
$iterator = new SizeRangeFilterIterator($inner, $size);
|
$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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user