Permalink
Browse files

[Finder] Added path & notPath support to gnu find adapter.

  • Loading branch information...
1 parent 62cef44 commit 11b4b28354728ece65949c28e6d1bb43f505d218 @jfsimon jfsimon committed Oct 29, 2012
Showing with 82 additions and 29 deletions.
  1. +12 −13 Adapter/AbstractAdapter.php
  2. +29 −15 Adapter/AdapterInterface.php
  3. +41 −1 Adapter/GnuFindAdapter.php
@@ -42,7 +42,7 @@ public function setFollowLinks($followLinks)
$this->followLinks = $followLinks;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -52,7 +52,7 @@ public function setMode($mode)
$this->mode = $mode;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -82,7 +82,7 @@ public function setDepths(array $depths)
}
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -92,7 +92,7 @@ public function setExclude(array $exclude)
$this->exclude = $exclude;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -102,7 +102,7 @@ public function setNames(array $names)
$this->names = $names;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -112,7 +112,7 @@ public function setNotNames(array $notNames)
$this->notNames = $notNames;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -122,7 +122,7 @@ public function setContains(array $contains)
$this->contains = $contains;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -132,7 +132,7 @@ public function setNotContains(array $notContains)
$this->notContains = $notContains;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -142,7 +142,7 @@ public function setSizes(array $sizes)
$this->sizes = $sizes;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -152,7 +152,7 @@ public function setDates(array $dates)
$this->dates = $dates;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -162,7 +162,7 @@ public function setFilters(array $filters)
$this->filters = $filters;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -172,7 +172,7 @@ public function setSort($sort)
$this->sort = $sort;
return $this;
- }
+ }
/**
* {@inheritdoc}
@@ -193,5 +193,4 @@ public function setNotPath(array $notPaths)
return $this;
}
-
}
@@ -21,103 +21,117 @@
*
* @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();
}
View
@@ -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);
@@ -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()) {
@@ -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

0 comments on commit 11b4b28

Please sign in to comment.