2010-04-21 08:31:18 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-21 08:31:18 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-04-21 08:31:18 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Finder;
|
|
|
|
|
2010-04-21 08:31:18 +01:00
|
|
|
/**
|
|
|
|
* Finder allows to build rules to find files and directories.
|
|
|
|
*
|
|
|
|
* It is a thin wrapper around several specialized iterator classes.
|
|
|
|
*
|
2010-05-21 12:22:34 +01:00
|
|
|
* All rules may be invoked several times.
|
2010-04-21 08:31:18 +01:00
|
|
|
*
|
|
|
|
* All methods return the current Finder object to allow easy chaining:
|
|
|
|
*
|
|
|
|
* $finder = new Finder();
|
2010-05-21 12:22:34 +01:00
|
|
|
* $finder = $finder->files()->name('*.php')->in(__DIR__);
|
2010-04-21 08:31:18 +01:00
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-04-21 08:31:18 +01:00
|
|
|
*/
|
2010-05-04 10:34:29 +01:00
|
|
|
class Finder implements \IteratorAggregate
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $mode = 0;
|
|
|
|
protected $names = array();
|
|
|
|
protected $notNames = array();
|
|
|
|
protected $exclude = array();
|
|
|
|
protected $filters = array();
|
2010-05-21 12:22:34 +01:00
|
|
|
protected $depths = array();
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $sizes = array();
|
|
|
|
protected $followLinks = false;
|
|
|
|
protected $sort = false;
|
|
|
|
protected $ignoreVCS = true;
|
|
|
|
protected $dirs = array();
|
2010-05-21 11:50:13 +01:00
|
|
|
protected $dates = array();
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Restricts the matching to directories only.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function directories()
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES;
|
|
|
|
|
|
|
|
return $this;
|
2010-05-04 10:34:29 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Restricts the matching to files only.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function files()
|
2010-05-04 10:34:29 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES;
|
|
|
|
|
|
|
|
return $this;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
2010-05-21 12:22:34 +01:00
|
|
|
* Adds tests for the directory depth.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-05-21 12:22:34 +01:00
|
|
|
* Usage:
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-05-21 12:22:34 +01:00
|
|
|
* $finder->depth('> 1') // the Finder will start matching at level 1.
|
|
|
|
* $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-05-21 12:22:34 +01:00
|
|
|
* @param int $level The depth level expression
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\DepthRangeFilterIterator
|
|
|
|
* @see Symfony\Component\Finder\Comparator\NumberComparator
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2010-05-21 12:22:34 +01:00
|
|
|
public function depth($level)
|
2010-05-04 10:34:29 +01:00
|
|
|
{
|
2010-05-21 12:22:34 +01:00
|
|
|
$this->depths[] = new Comparator\NumberComparator($level);
|
2010-05-14 08:29:04 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-05-21 11:50:13 +01:00
|
|
|
* Adds tests for file dates (last modified).
|
2010-05-14 08:29:04 +01:00
|
|
|
*
|
|
|
|
* The date must be something that strtotime() is able to parse:
|
|
|
|
*
|
2010-05-21 11:50:13 +01:00
|
|
|
* $finder->date('since yesterday');
|
|
|
|
* $finder->date('until 2 days ago');
|
|
|
|
* $finder->date('> now - 2 hours');
|
|
|
|
* $finder->date('>= 2005-10-15');
|
2010-05-14 08:29:04 +01:00
|
|
|
*
|
2010-05-21 11:50:13 +01:00
|
|
|
* @param string $date A date rage string
|
2010-05-14 08:29:04 +01:00
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-14 08:29:04 +01:00
|
|
|
*
|
2010-05-21 11:50:13 +01:00
|
|
|
* @see strtotime
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\DateRangeFilterIterator
|
|
|
|
* @see Symfony\Component\Finder\Comparator\DateComparator
|
2010-05-14 08:29:04 +01:00
|
|
|
*/
|
2010-05-21 11:50:13 +01:00
|
|
|
public function date($date)
|
2010-05-14 08:29:04 +01:00
|
|
|
{
|
2010-05-21 11:50:13 +01:00
|
|
|
$this->dates[] = new Comparator\DateComparator($date);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
return $this;
|
2010-05-04 10:34:29 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Adds rules that files must match.
|
|
|
|
*
|
|
|
|
* You can use patterns (delimited with / sign), globs or simple strings.
|
|
|
|
*
|
|
|
|
* $finder->name('*.php')
|
|
|
|
* $finder->name('/\.php$/') // same as above
|
|
|
|
* $finder->name('test.php')
|
|
|
|
*
|
|
|
|
* @param string $pattern A pattern (a regexp, a glob, or a string)
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\FilenameFilterIterator
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function name($pattern)
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->names[] = $pattern;
|
|
|
|
|
|
|
|
return $this;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Adds rules that files must not match.
|
|
|
|
*
|
|
|
|
* @param string $pattern A pattern (a regexp, a glob, or a string)
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\FilenameFilterIterator
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function notName($pattern)
|
|
|
|
{
|
|
|
|
$this->notNames[] = $pattern;
|
2010-05-04 10:34:29 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2010-04-21 08:31:18 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Adds tests for file sizes.
|
|
|
|
*
|
|
|
|
* $finder->size('> 10K');
|
|
|
|
* $finder->size('<= 1Ki');
|
|
|
|
* $finder->size(4);
|
|
|
|
*
|
|
|
|
* @param string $size A size range string
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\SizeRangeFilterIterator
|
|
|
|
* @see Symfony\Component\Finder\Comparator\NumberComparator
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function size($size)
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-21 11:50:13 +01:00
|
|
|
$this->sizes[] = new Comparator\NumberComparator($size);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
return $this;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Excludes directories.
|
|
|
|
*
|
|
|
|
* @param string $dir A directory to exclude
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function exclude($dir)
|
|
|
|
{
|
|
|
|
$this->exclude[] = $dir;
|
2010-04-21 08:31:18 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Forces the finder to ignore version control directories.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\IgnoreVcsFilterIterator
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function ignoreVCS($ignoreVCS)
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->ignoreVCS = (Boolean) $ignoreVCS;
|
|
|
|
|
|
|
|
return $this;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sorts files and directories by an anonymous function.
|
|
|
|
*
|
|
|
|
* The anonymous function receives two \SplFileInfo instances to compare.
|
|
|
|
*
|
|
|
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
|
*
|
|
|
|
* @param Closure $closure An anonymous function
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\SortableIterator
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function sort(\Closure $closure)
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->sort = $closure;
|
|
|
|
|
|
|
|
return $this;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sorts files and directories by name.
|
|
|
|
*
|
|
|
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\SortableIterator
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function sortByName()
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->sort = Iterator\SortableIterator::SORT_BY_NAME;
|
|
|
|
|
|
|
|
return $this;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Sorts files and directories by type (directories before files), then by name.
|
|
|
|
*
|
|
|
|
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\SortableIterator
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function sortByType()
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->sort = Iterator\SortableIterator::SORT_BY_TYPE;
|
|
|
|
|
|
|
|
return $this;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Filters the iterator with an anonymous function.
|
|
|
|
*
|
|
|
|
* The anonymous function receives a \SplFileInfo and must return false
|
|
|
|
* to remove files.
|
|
|
|
*
|
|
|
|
* @param Closure $closure An anonymous function
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2010-08-20 22:09:55 +01:00
|
|
|
* @see Symfony\Component\Finder\Iterator\CustomFilterIterator
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function filter(\Closure $closure)
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->filters[] = $closure;
|
|
|
|
|
|
|
|
return $this;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Forces the following of symlinks.
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function followLinks()
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->followLinks = true;
|
|
|
|
|
|
|
|
return $this;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Searches files and directories which match defined rules.
|
|
|
|
*
|
|
|
|
* @param string|array $dirs A directory path or an array of directories
|
|
|
|
*
|
2010-07-27 14:33:28 +01:00
|
|
|
* @return Finder The current Finder instance
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException if one of the directory does not exist
|
|
|
|
*/
|
|
|
|
public function in($dirs)
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!is_array($dirs)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$dirs = array($dirs);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($dirs as $dir) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!is_dir($dir)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->dirs = array_merge($this->dirs, $dirs);
|
|
|
|
|
|
|
|
return $this;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns an Iterator for the current Finder configuration.
|
|
|
|
*
|
|
|
|
* This method implements the IteratorAggregate interface.
|
|
|
|
*
|
|
|
|
* @return \Iterator An iterator
|
|
|
|
*
|
|
|
|
* @throws \LogicException if the in() method has not been called
|
|
|
|
*/
|
|
|
|
public function getIterator()
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (0 === count($this->dirs)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \LogicException('You must call the in() method before iterating over a Finder.');
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (1 === count($this->dirs)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->searchInDirectory($this->dirs[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$iterator = new \AppendIterator();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($this->dirs as $dir) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$iterator->append($this->searchInDirectory($dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $iterator;
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function searchInDirectory($dir)
|
|
|
|
{
|
|
|
|
$flags = \FilesystemIterator::SKIP_DOTS;
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->followLinks) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
|
|
|
|
|
2010-05-21 12:22:34 +01:00
|
|
|
if ($this->depths) {
|
|
|
|
$iterator = new Iterator\DepthRangeFilterIterator($iterator, $this->depths);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->mode) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->exclude) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->ignoreVCS) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$iterator = new Iterator\IgnoreVcsFilterIterator($iterator);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->names || $this->notNames) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$iterator = new Iterator\FilenameFilterIterator($iterator, $this->names, $this->notNames);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->sizes) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes);
|
|
|
|
}
|
|
|
|
|
2010-05-21 11:50:13 +01:00
|
|
|
if ($this->dates) {
|
|
|
|
$iterator = new Iterator\DateRangeFilterIterator($iterator, $this->dates);
|
2010-05-14 08:29:04 +01:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->filters) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->sort) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$iterator = new Iterator\SortableIterator($iterator, $this->sort);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $iterator;
|
|
|
|
}
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|