Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Finder] Added path & notPath support to gnu find adapter.
  • Loading branch information
jfsimon committed Oct 29, 2012
1 parent 6258d12 commit 4e21bf2
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 29 deletions.
25 changes: 12 additions & 13 deletions src/Symfony/Component/Finder/Adapter/AbstractAdapter.php
Expand Up @@ -42,7 +42,7 @@ public function setFollowLinks($followLinks)
$this->followLinks = $followLinks;

return $this;
}
}

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

return $this;
}
}

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

return $this;
}
}

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

return $this;
}
}

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

return $this;
}
}

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

return $this;
}
}

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

return $this;
}
}

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

return $this;
}
}

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

return $this;
}
}

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

return $this;
}
}

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

return $this;
}
}

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

return $this;
}
}

/**
* {@inheritdoc}
Expand All @@ -193,5 +193,4 @@ public function setNotPath(array $notPaths)

return $this;
}

}
44 changes: 29 additions & 15 deletions src/Symfony/Component/Finder/Adapter/AdapterInterface.php
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
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

0 comments on commit 4e21bf2

Please sign in to comment.