Skip to content

Commit

Permalink
[Finder] fixed exclude iterator (now only match with the relative path)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jul 12, 2010
1 parent c605d7f commit 44a16fc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
Expand Up @@ -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)
{
Expand Down
Expand Up @@ -32,7 +32,7 @@ public function __construct(\Iterator $iterator, array $directories)
{
$this->patterns = array();
foreach ($directories as $directory) {
$this->patterns[] = '#/'.preg_quote($directory, '#').'(/|$)#';
$this->patterns[] = '#(^|/)'.preg_quote($directory, '#').'(/|$)#';
}

parent::__construct($iterator);
Expand All @@ -45,15 +45,14 @@ public function __construct(\Iterator $iterator, array $directories)
*/
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;
}
}
Expand Down
Expand Up @@ -13,16 +13,16 @@

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);

Expand All @@ -31,9 +31,23 @@ public function testAccept($directories, $expected)

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'
)),
);
}
}
Expand Up @@ -13,16 +13,24 @@

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);
}
}

0 comments on commit 44a16fc

Please sign in to comment.