Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[2.2][Finder] ->pathContains(), ->pathNotContains() methods (with basic tests) #4739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 30, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions src/Symfony/Component/Finder/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ abstract class AbstractAdapter implements AdapterInterface
protected $dates = array();
protected $filters = array();
protected $sort = false;
protected $paths = array();
protected $notPaths = array();

/**
* {@inheritdoc}
Expand All @@ -40,7 +42,7 @@ public function setFollowLinks($followLinks)
$this->followLinks = $followLinks;

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -50,7 +52,7 @@ public function setMode($mode)
$this->mode = $mode;

return $this;
}
}

/**
* {@inheritdoc}
Expand Down Expand Up @@ -80,7 +82,7 @@ public function setDepths(array $depths)
}

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -90,7 +92,7 @@ public function setExclude(array $exclude)
$this->exclude = $exclude;

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -100,7 +102,7 @@ public function setNames(array $names)
$this->names = $names;

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -110,7 +112,7 @@ public function setNotNames(array $notNames)
$this->notNames = $notNames;

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -120,7 +122,7 @@ public function setContains(array $contains)
$this->contains = $contains;

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -130,7 +132,7 @@ public function setNotContains(array $notContains)
$this->notContains = $notContains;

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -140,7 +142,7 @@ public function setSizes(array $sizes)
$this->sizes = $sizes;

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -150,7 +152,7 @@ public function setDates(array $dates)
$this->dates = $dates;

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -160,7 +162,7 @@ public function setFilters(array $filters)
$this->filters = $filters;

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -170,5 +172,25 @@ public function setSort($sort)
$this->sort = $sort;

return $this;
}
}

/**
* {@inheritdoc}
*/
public function setPath(array $paths)
{
$this->paths = $paths;

return $this;
}

/**
* {@inheritdoc}
*/
public function setNotPath(array $notPaths)
{
$this->notPaths = $notPaths;

return $this;
}
}
44 changes: 29 additions & 15 deletions src/Symfony/Component/Finder/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,103 +21,117 @@ interface AdapterInterface
*
* @return AdapterInterface Current instance
*/
function setFollowLinks($followLinks);
public function setFollowLinks($followLinks);

/**
* @param int $mode
*
* @return AdapterInterface Current instance
*/
function setMode($mode);
public function setMode($mode);

/**
* @param array $exclude
*
* @return AdapterInterface Current instance
*/
function setExclude(array $exclude);
public function setExclude(array $exclude);

/**
* @param array $depths
*
* @return AdapterInterface Current instance
*/
function setDepths(array $depths);
public function setDepths(array $depths);

/**
* @param array $names
*
* @return AdapterInterface Current instance
*/
function setNames(array $names);
public function setNames(array $names);

/**
* @param array $notNames
*
* @return AdapterInterface Current instance
*/
function setNotNames(array $notNames);
public function setNotNames(array $notNames);

/**
* @param array $contains
*
* @return AdapterInterface Current instance
*/
function setContains(array $contains);
public function setContains(array $contains);

/**
* @param array $notContains
*
* @return AdapterInterface Current instance
*/
function setNotContains(array $notContains);
public function setNotContains(array $notContains);

/**
* @param array $sizes
*
* @return AdapterInterface Current instance
*/
function setSizes(array $sizes);
public function setSizes(array $sizes);

/**
* @param array $dates
*
* @return AdapterInterface Current instance
*/
function setDates(array $dates);
public function setDates(array $dates);

/**
* @param array $filters
*
* @return AdapterInterface Current instance
*/
function setFilters(array $filters);
public function setFilters(array $filters);

/**
* @param \Closure|int $sort
*
* @return AdapterInterface Current instance
*/
function setSort($sort);
public function setSort($sort);

/**
* @param array $path
*
* @return AdapterInterface Current instance
*/
public function setPath(array $paths);

/**
* @param array $notPaths
*
* @return AdapterInterface Current instance
*/
public function setNotPath(array $notPaths);

/**
* @param string $dir
*
* @return \Iterator Result iterator
*/
function searchInDirectory($dir);
public function searchInDirectory($dir);

/**
* Tests adapter support for current platform.
*
* @return bool
*/
function isSupported();
public function isSupported();

/**
* Returns adapter name.
*
* @return string
*/
function getName();
public function getName();
}
42 changes: 41 additions & 1 deletion src/Symfony/Component/Finder/Adapter/GnuFindAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public function searchInDirectory($dir)

$this->buildNamesFiltering($find, $this->names);
$this->buildNamesFiltering($find, $this->notNames, true);
$this->buildPathsFiltering($find, $dir, $this->paths);
$this->buildPathsFiltering($find, $dir, $this->notPaths, true);
$this->buildSizesFiltering($find, $this->sizes);
$this->buildDatesFiltering($find, $this->dates);

Expand Down Expand Up @@ -151,7 +153,7 @@ private function buildNamesFiltering(Command $command, array $names, $not = fals
foreach ($names as $i => $name) {
$expr = Expression::create($name);

// Fixes 'not search' and 'fuls path matching' regex problems.
// Fixes 'not search' and 'full path matching' regex problems.
// - Jokers '.' are replaced by [^/].
// - We add '[^/]*' before and after regex (if no ^|$ flags are present).
if ($expr->isRegex()) {
Expand All @@ -177,6 +179,44 @@ private function buildNamesFiltering(Command $command, array $names, $not = fals
$command->cmd(')');
}

/**
* @param Command $command
* @param string $dir
* @param string[] $paths
* @param bool $not
* @return void
*/
private function buildPathsFiltering(Command $command, $dir, array $paths, $not = false)
{
if (0 === count($paths)) {
return;
}

$command->add($not ? '-not' : null)->cmd('(');

foreach ($paths as $i => $path) {
$expr = Expression::create($path);

// Fixes 'not search' regex problems.
if ($expr->isRegex()) {
$regex = $expr->getRegex();
$regex->prepend($regex->hasStartFlag() ? '' : '.*')->setEndJoker(!$regex->hasEndFlag());
} else {
$expr->prepend('*')->append('*');
}

$command
->add($i > 0 ? '-or' : null)
->add($expr->isRegex()
? ($expr->isCaseSensitive() ? '-regex' : '-iregex')
: ($expr->isCaseSensitive() ? '-path' : '-ipath')
)
->arg($expr->prepend($dir.DIRECTORY_SEPARATOR)->renderPattern());
}

$command->cmd(')');
}

/**
* @param Command $command
* @param NumberComparator[] $sizes
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Finder/Adapter/PhpAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public function searchInDirectory($dir)
$iterator = $iteratorAggregate->getIterator();
}

if ($this->paths || $this->notPaths) {
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $this->notPaths);
}

return $iterator;
}

Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/Finder/Expression/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ public function getType()
return $this->value->getType();
}

/**
* {@inheritdoc}
*/
public function prepend($expr)
{
$this->value->prepend($expr);

return $this;
}

/**
* {@inheritdoc}
*/
public function append($expr)
{
$this->value->append($expr);

return $this;
}

/**
* @return bool
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/Finder/Expression/Glob.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ public function isCaseSensitive()
return true;
}

/**
* {@inheritdoc}
*/
public function prepend($expr)
{
$this->pattern = $expr.$this->pattern;

return $this;
}

/**
* {@inheritdoc}
*/
public function append($expr)
{
$this->pattern .= $expr;

return $this;
}

/**
* @param bool $strictLeadingDot
* @param bool $strictWildcardSlash
Expand Down
Loading