4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Adapter;
14 @trigger_error('The '.__NAMESPACE__.'\AbstractFindAdapter class is deprecated since version 2.8 and will be removed in 3.0. Use directly the Finder class instead.', E_USER_DEPRECATED);
16 use Symfony\Component\Finder\Exception\AccessDeniedException;
17 use Symfony\Component\Finder\Iterator;
18 use Symfony\Component\Finder\Shell\Shell;
19 use Symfony\Component\Finder\Expression\Expression;
20 use Symfony\Component\Finder\Shell\Command;
21 use Symfony\Component\Finder\Comparator\NumberComparator;
22 use Symfony\Component\Finder\Comparator\DateComparator;
25 * Shell engine implementation using GNU find command.
27 * @author Jean-François Simon <contact@jfsimon.fr>
29 * @deprecated since 2.8, to be removed in 3.0. Use Finder instead.
31 abstract class AbstractFindAdapter extends AbstractAdapter
41 public function __construct()
43 $this->shell = new Shell();
49 public function searchInDirectory($dir)
51 // having "/../" in path make find fail
52 $dir = realpath($dir);
54 // searching directories containing or not containing strings leads to no result
55 if (Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode && ($this->contains || $this->notContains)) {
56 return new Iterator\FilePathsIterator(array(), $dir);
59 $command = Command::create();
60 $find = $this->buildFindCommand($command, $dir);
62 if ($this->followLinks) {
63 $find->add('-follow');
66 $find->add('-mindepth')->add($this->minDepth + 1);
68 if (PHP_INT_MAX !== $this->maxDepth) {
69 $find->add('-maxdepth')->add($this->maxDepth + 1);
72 if (Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode) {
73 $find->add('-type d');
74 } elseif (Iterator\FileTypeFilterIterator::ONLY_FILES === $this->mode) {
75 $find->add('-type f');
78 $this->buildNamesFiltering($find, $this->names);
79 $this->buildNamesFiltering($find, $this->notNames, true);
80 $this->buildPathsFiltering($find, $dir, $this->paths);
81 $this->buildPathsFiltering($find, $dir, $this->notPaths, true);
82 $this->buildSizesFiltering($find, $this->sizes);
83 $this->buildDatesFiltering($find, $this->dates);
85 $useGrep = $this->shell->testCommand('grep') && $this->shell->testCommand('xargs');
86 $useSort = is_int($this->sort) && $this->shell->testCommand('sort') && $this->shell->testCommand('cut');
88 if ($useGrep && ($this->contains || $this->notContains)) {
89 $grep = $command->ins('grep');
90 $this->buildContentFiltering($grep, $this->contains);
91 $this->buildContentFiltering($grep, $this->notContains, true);
95 $this->buildSorting($command, $this->sort);
98 $command->setErrorHandler(
99 $this->ignoreUnreadableDirs
100 // If directory is unreadable and finder is set to ignore it, `stderr` is ignored.
101 ? function ($stderr) { }
102 : function ($stderr) { throw new AccessDeniedException($stderr); }
105 $paths = $this->shell->testCommand('uniq') ? $command->add('| uniq')->execute() : array_unique($command->execute());
106 $iterator = new Iterator\FilePathsIterator($paths, $dir);
108 if ($this->exclude) {
109 $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
112 if (!$useGrep && ($this->contains || $this->notContains)) {
113 $iterator = new Iterator\FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
116 if ($this->filters) {
117 $iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
120 if (!$useSort && $this->sort) {
121 $iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort);
122 $iterator = $iteratorAggregate->getIterator();
131 protected function canBeUsed()
133 return $this->shell->testCommand('find');
137 * @param Command $command
142 protected function buildFindCommand(Command $command, $dir)
148 ->add('-noleaf'); // the -noleaf option is required for filesystems that don't follow the '.' and '..' conventions
152 * @param Command $command
153 * @param string[] $names
156 private function buildNamesFiltering(Command $command, array $names, $not = false)
158 if (0 === count($names)) {
162 $command->add($not ? '-not' : null)->cmd('(');
164 foreach ($names as $i => $name) {
165 $expr = Expression::create($name);
167 // Find does not support expandable globs ("*.{a,b}" syntax).
168 if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
169 $expr = Expression::create($expr->getGlob()->toRegex(false));
172 // Fixes 'not search' and 'full path matching' regex problems.
173 // - Jokers '.' are replaced by [^/].
174 // - We add '[^/]*' before and after regex (if no ^|$ flags are present).
175 if ($expr->isRegex()) {
176 $regex = $expr->getRegex();
177 $regex->prepend($regex->hasStartFlag() ? '/' : '/[^/]*')
178 ->setStartFlag(false)
179 ->setStartJoker(true)
180 ->replaceJokers('[^/]');
181 if (!$regex->hasEndFlag() || $regex->hasEndJoker()) {
182 $regex->setEndJoker(false)->append('[^/]*');
187 ->add($i > 0 ? '-or' : null)
188 ->add($expr->isRegex()
189 ? ($expr->isCaseSensitive() ? '-regex' : '-iregex')
190 : ($expr->isCaseSensitive() ? '-name' : '-iname')
192 ->arg($expr->renderPattern());
199 * @param Command $command
201 * @param string[] $paths
204 private function buildPathsFiltering(Command $command, $dir, array $paths, $not = false)
206 if (0 === count($paths)) {
210 $command->add($not ? '-not' : null)->cmd('(');
212 foreach ($paths as $i => $path) {
213 $expr = Expression::create($path);
215 // Find does not support expandable globs ("*.{a,b}" syntax).
216 if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
217 $expr = Expression::create($expr->getGlob()->toRegex(false));
220 // Fixes 'not search' regex problems.
221 if ($expr->isRegex()) {
222 $regex = $expr->getRegex();
223 $regex->prepend($regex->hasStartFlag() ? preg_quote($dir).DIRECTORY_SEPARATOR : '.*')->setEndJoker(!$regex->hasEndFlag());
225 $expr->prepend('*')->append('*');
229 ->add($i > 0 ? '-or' : null)
230 ->add($expr->isRegex()
231 ? ($expr->isCaseSensitive() ? '-regex' : '-iregex')
232 : ($expr->isCaseSensitive() ? '-path' : '-ipath')
234 ->arg($expr->renderPattern());
241 * @param Command $command
242 * @param NumberComparator[] $sizes
244 private function buildSizesFiltering(Command $command, array $sizes)
246 foreach ($sizes as $i => $size) {
247 $command->add($i > 0 ? '-and' : null);
249 switch ($size->getOperator()) {
251 $command->add('-size -'.($size->getTarget() + 1).'c');
254 $command->add('-size +'.($size->getTarget() - 1).'c');
257 $command->add('-size +'.$size->getTarget().'c');
260 $command->add('-size -'.$size->getTarget().'c');
261 $command->add('-size +'.$size->getTarget().'c');
265 $command->add('-size -'.$size->getTarget().'c');
271 * @param Command $command
272 * @param DateComparator[] $dates
274 private function buildDatesFiltering(Command $command, array $dates)
276 foreach ($dates as $i => $date) {
277 $command->add($i > 0 ? '-and' : null);
279 $mins = (int) round((time() - $date->getTarget()) / 60);
282 // mtime is in the future
283 $command->add(' -mmin -0');
284 // we will have no result so we don't need to continue
288 switch ($date->getOperator()) {
290 $command->add('-mmin +'.($mins - 1));
293 $command->add('-mmin -'.($mins + 1));
296 $command->add('-mmin -'.$mins);
299 $command->add('-mmin +'.$mins.' -or -mmin -'.$mins);
303 $command->add('-mmin +'.$mins);
309 * @param Command $command
310 * @param string $sort
312 * @throws \InvalidArgumentException
314 private function buildSorting(Command $command, $sort)
316 $this->buildFormatSorting($command, $sort);
320 * @param Command $command
321 * @param string $sort
323 abstract protected function buildFormatSorting(Command $command, $sort);
326 * @param Command $command
327 * @param array $contains
330 abstract protected function buildContentFiltering(Command $command, array $contains, $not = false);