[Finder] fixed exclude iterator (now only match with the relative path)
This commit is contained in:
parent
c605d7f9fb
commit
44a16fc8c4
@ -25,8 +25,8 @@ class DepthRangeFilterIterator extends \FilterIterator
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param \Iterator $iterator The Iterator to filter
|
||||
* @param array $comparators An array of \NumberComparator instances
|
||||
* @param \RecursiveIteratorIterator $iterator The Iterator to filter
|
||||
* @param array $comparators An array of \NumberComparator instances
|
||||
*/
|
||||
public function __construct(\RecursiveIteratorIterator $iterator, array $comparators)
|
||||
{
|
||||
|
@ -32,7 +32,7 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator
|
||||
{
|
||||
$this->patterns = array();
|
||||
foreach ($directories as $directory) {
|
||||
$this->patterns[] = '#/'.preg_quote($directory, '#').'(/|$)#';
|
||||
$this->patterns[] = '#(^|/)'.preg_quote($directory, '#').'(/|$)#';
|
||||
}
|
||||
|
||||
parent::__construct($iterator);
|
||||
@ -45,15 +45,14 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator
|
||||
*/
|
||||
public function accept()
|
||||
{
|
||||
$fileinfo = $this->getInnerIterator()->current();
|
||||
$inner = $this;
|
||||
while ($inner && !$inner->getInnerIterator() instanceof \RecursiveIteratorIterator) {
|
||||
$inner = $inner->getInnerIterator();
|
||||
}
|
||||
|
||||
foreach ($this->patterns as $pattern) {
|
||||
$path = $fileinfo->getPathname();
|
||||
if ($fileinfo->isDir()) {
|
||||
$path .= '/';
|
||||
}
|
||||
|
||||
if (preg_match($pattern, $path)) {
|
||||
$method = $inner->current()->isDir() ? 'getSubPathname' : 'getSubPath';
|
||||
if (preg_match($pattern, $this->getInnerIterator()->$method())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -13,16 +13,16 @@ namespace Symfony\Tests\Components\Finder\Iterator;
|
||||
|
||||
use Symfony\Components\Finder\Iterator\ExcludeDirectoryFilterIterator;
|
||||
|
||||
require_once __DIR__.'/IteratorTestCase.php';
|
||||
require_once __DIR__.'/RealIteratorTestCase.php';
|
||||
|
||||
class ExcludeDirectoryFilterIteratorTest extends IteratorTestCase
|
||||
class ExcludeDirectoryFilterIteratorTest extends RealIteratorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getAcceptData
|
||||
*/
|
||||
public function testAccept($directories, $expected)
|
||||
{
|
||||
$inner = new Iterator(array('/foo/test.php', '/foo/test.py', '/bar/foo.php'));
|
||||
$inner = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/symfony2_finder', \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
|
||||
|
||||
$iterator = new ExcludeDirectoryFilterIterator($inner, $directories);
|
||||
|
||||
@ -31,9 +31,23 @@ class ExcludeDirectoryFilterIteratorTest extends IteratorTestCase
|
||||
|
||||
public function getAcceptData()
|
||||
{
|
||||
$tmpDir = sys_get_temp_dir().'/symfony2_finder';
|
||||
|
||||
return array(
|
||||
array(array('foo'), array('/bar/foo.php')),
|
||||
array(array('fo'), array('/foo/test.php', '/foo/test.py', '/bar/foo.php')),
|
||||
array(array('foo'), array(
|
||||
$tmpDir.'/.git',
|
||||
$tmpDir.'/test.py',
|
||||
$tmpDir.'/test.php',
|
||||
$tmpDir.'/toto'
|
||||
)),
|
||||
array(array('fo'), array(
|
||||
$tmpDir.'/.git',
|
||||
$tmpDir.'/test.py',
|
||||
$tmpDir.'/foo',
|
||||
$tmpDir.'/foo/bar.tmp',
|
||||
$tmpDir.'/test.php',
|
||||
$tmpDir.'/toto'
|
||||
)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -13,16 +13,24 @@ namespace Symfony\Tests\Components\Finder\Iterator;
|
||||
|
||||
use Symfony\Components\Finder\Iterator\IgnoreVcsFilterIterator;
|
||||
|
||||
require_once __DIR__.'/IteratorTestCase.php';
|
||||
require_once __DIR__.'/RealIteratorTestCase.php';
|
||||
|
||||
class IgnoreVcsFilterIteratorTest extends IteratorTestCase
|
||||
class IgnoreVcsFilterIteratorTest extends RealIteratorTestCase
|
||||
{
|
||||
public function testAccept()
|
||||
{
|
||||
$inner = new Iterator(array('/.git/test.php', '/foo/test.py', '/bar/foo.php'));
|
||||
$inner = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(sys_get_temp_dir().'/symfony2_finder', \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
|
||||
//$inner = new Iterator(array('/.git/test.php', '/foo/test.py', '/bar/foo.php'));
|
||||
|
||||
$iterator = new IgnoreVcsFilterIterator($inner);
|
||||
$tmpDir = sys_get_temp_dir().'/symfony2_finder';
|
||||
|
||||
$this->assertIterator(array('/foo/test.py', '/bar/foo.php'), $iterator);
|
||||
$this->assertIterator(array(
|
||||
$tmpDir.'/test.py',
|
||||
$tmpDir.'/foo',
|
||||
$tmpDir.'/foo/bar.tmp',
|
||||
$tmpDir.'/test.php',
|
||||
$tmpDir.'/toto'
|
||||
), $iterator);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user