namespace Symfony\Component\Finder;
-use Symfony\Component\Finder\Adapter\AdapterInterface;
-use Symfony\Component\Finder\Adapter\GnuFindAdapter;
-use Symfony\Component\Finder\Adapter\BsdFindAdapter;
-use Symfony\Component\Finder\Adapter\PhpAdapter;
use Symfony\Component\Finder\Comparator\DateComparator;
use Symfony\Component\Finder\Comparator\NumberComparator;
-use Symfony\Component\Finder\Exception\ExceptionInterface;
use Symfony\Component\Finder\Iterator\CustomFilterIterator;
use Symfony\Component\Finder\Iterator\DateRangeFilterIterator;
use Symfony\Component\Finder\Iterator\DepthRangeFilterIterator;
private $iterators = array();
private $contains = array();
private $notContains = array();
- private $adapters = null;
private $paths = array();
private $notPaths = array();
private $ignoreUnreadableDirs = false;
return new static();
}
- /**
- * Registers a finder engine implementation.
- *
- * @param AdapterInterface $adapter An adapter instance
- * @param int $priority Highest is selected first
- *
- * @return $this
- *
- * @deprecated since 2.8, to be removed in 3.0.
- */
- public function addAdapter(AdapterInterface $adapter, $priority = 0)
- {
- @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
-
- $this->initDefaultAdapters();
-
- $this->adapters[$adapter->getName()] = array(
- 'adapter' => $adapter,
- 'priority' => $priority,
- 'selected' => false,
- );
-
- return $this->sortAdapters();
- }
-
- /**
- * Sets the selected adapter to the best one according to the current platform the code is run on.
- *
- * @return $this
- *
- * @deprecated since 2.8, to be removed in 3.0.
- */
- public function useBestAdapter()
- {
- @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
-
- $this->initDefaultAdapters();
-
- $this->resetAdapterSelection();
-
- return $this->sortAdapters();
- }
-
- /**
- * Selects the adapter to use.
- *
- * @param string $name
- *
- * @return $this
- *
- * @throws \InvalidArgumentException
- *
- * @deprecated since 2.8, to be removed in 3.0.
- */
- public function setAdapter($name)
- {
- @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
-
- $this->initDefaultAdapters();
-
- if (!isset($this->adapters[$name])) {
- throw new \InvalidArgumentException(sprintf('Adapter "%s" does not exist.', $name));
- }
-
- $this->resetAdapterSelection();
- $this->adapters[$name]['selected'] = true;
-
- return $this->sortAdapters();
- }
-
- /**
- * Removes all adapters registered in the finder.
- *
- * @return $this
- *
- * @deprecated since 2.8, to be removed in 3.0.
- */
- public function removeAdapters()
- {
- @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
-
- $this->adapters = array();
-
- return $this;
- }
-
- /**
- * Returns registered adapters ordered by priority without extra information.
- *
- * @return AdapterInterface[]
- *
- * @deprecated since 2.8, to be removed in 3.0.
- */
- public function getAdapters()
- {
- @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
-
- $this->initDefaultAdapters();
-
- return array_values(array_map(function (array $adapter) {
- return $adapter['adapter'];
- }, $this->adapters));
- }
-
/**
* Restricts the matching to directories only.
*
/**
* Excludes directories.
*
+ * Directories passed as argument must be relative to the ones defined with the `in()` method. For example:
+ *
+ * $finder->in(__DIR__)->exclude('ruby');
+ *
* @param string|array $dirs A directory path or an array of directories
*
* @return $this
foreach ((array) $dirs as $dir) {
if (is_dir($dir)) {
- $resolvedDirs[] = $dir;
+ $resolvedDirs[] = $this->normalizeDir($dir);
} elseif ($glob = glob($dir, (defined('GLOB_BRACE') ? GLOB_BRACE : 0) | GLOB_ONLYDIR)) {
- $resolvedDirs = array_merge($resolvedDirs, $glob);
+ $resolvedDirs = array_merge($resolvedDirs, array_map(array($this, 'normalizeDir'), $glob));
} else {
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
}
}
/**
- * Counts all the results collected by the iterators.
+ * Check if the any results were found.
*
- * @return int
+ * @return bool
*/
- public function count()
+ public function hasResults()
{
- return iterator_count($this->getIterator());
+ foreach ($this->getIterator() as $_) {
+ return true;
+ }
+
+ return false;
}
/**
- * @return $this
+ * Counts all the results collected by the iterators.
+ *
+ * @return int
*/
- private function sortAdapters()
+ public function count()
{
- uasort($this->adapters, function (array $a, array $b) {
- if ($a['selected'] || $b['selected']) {
- return $a['selected'] ? -1 : 1;
- }
-
- return $a['priority'] > $b['priority'] ? -1 : 1;
- });
-
- return $this;
+ return iterator_count($this->getIterator());
}
/**
$this->notPaths[] = '#(^|/)\..+(/|$)#';
}
- if ($this->adapters) {
- foreach ($this->adapters as $adapter) {
- if ($adapter['adapter']->isSupported()) {
- try {
- return $this
- ->buildAdapter($adapter['adapter'])
- ->searchInDirectory($dir);
- } catch (ExceptionInterface $e) {
- }
- }
- }
- }
-
$minDepth = 0;
$maxDepth = PHP_INT_MAX;
}
/**
- * @return AdapterInterface
- */
- private function buildAdapter(AdapterInterface $adapter)
- {
- return $adapter
- ->setFollowLinks($this->followLinks)
- ->setDepths($this->depths)
- ->setMode($this->mode)
- ->setExclude($this->exclude)
- ->setNames($this->names)
- ->setNotNames($this->notNames)
- ->setContains($this->contains)
- ->setNotContains($this->notContains)
- ->setSizes($this->sizes)
- ->setDates($this->dates)
- ->setFilters($this->filters)
- ->setSort($this->sort)
- ->setPath($this->paths)
- ->setNotPath($this->notPaths)
- ->ignoreUnreadableDirs($this->ignoreUnreadableDirs);
- }
-
- /**
- * Unselects all adapters.
+ * Normalizes given directory names by removing trailing slashes.
+ *
+ * @param string $dir
+ *
+ * @return string
*/
- private function resetAdapterSelection()
+ private function normalizeDir($dir)
{
- $this->adapters = array_map(function (array $properties) {
- $properties['selected'] = false;
-
- return $properties;
- }, $this->adapters);
- }
-
- private function initDefaultAdapters()
- {
- if (null === $this->adapters) {
- $this->adapters = array();
- $this
- ->addAdapter(new GnuFindAdapter())
- ->addAdapter(new BsdFindAdapter())
- ->addAdapter(new PhpAdapter(), -50)
- ->setAdapter('php')
- ;
- }
+ return rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
}
}