12 namespace Symfony\Component\Finder\Adapter;
15 use Symfony\Component\Finder\Iterator;
39 $this->shell =
new Shell();
48 $dir = realpath($dir);
58 if ($this->followLinks) {
59 $find->add(
'-follow');
62 $find->add(
'-mindepth')->add($this->minDepth + 1);
64 if (PHP_INT_MAX !== $this->maxDepth) {
65 $find->add(
'-maxdepth')->add($this->maxDepth + 1);
69 $find->add(
'-type d');
71 $find->add(
'-type f');
81 $useGrep = $this->shell->testCommand(
'grep') && $this->shell->testCommand(
'xargs');
82 $useSort = is_int($this->sort) && $this->shell->testCommand(
'sort') && $this->shell->testCommand(
'cut');
84 if ($useGrep && ($this->contains || $this->notContains)) {
85 $grep = $command->ins(
'grep');
94 $command->setErrorHandler(
97 ?
function ($stderr) {
return; }
101 $paths = $this->shell->testCommand(
'uniq') ? $command->add(
'| uniq')->execute() : array_unique($command->execute());
104 if ($this->exclude) {
108 if (!$useGrep && ($this->contains || $this->notContains)) {
112 if ($this->filters) {
116 if (!$useSort && $this->sort) {
118 $iterator = $iteratorAggregate->getIterator();
129 return $this->shell->testCommand(
'find');
154 if (0 === count($names)) {
158 $command->
add($not ?
'-not' : null)->cmd(
'(');
160 foreach ($names as $i => $name) {
164 if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
171 if ($expr->isRegex()) {
172 $regex = $expr->getRegex();
173 $regex->prepend($regex->hasStartFlag() ?
'/' :
'/[^/]*')
174 ->setStartFlag(
false)
175 ->setStartJoker(
true)
176 ->replaceJokers(
'[^/]');
177 if (!$regex->hasEndFlag() || $regex->hasEndJoker()) {
178 $regex->setEndJoker(
false)->append(
'[^/]*');
183 ->
add($i > 0 ?
'-or' : null)
184 ->add($expr->isRegex()
185 ? ($expr->isCaseSensitive() ?
'-regex' :
'-iregex')
186 : ($expr->isCaseSensitive() ?
'-name' :
'-iname')
188 ->arg($expr->renderPattern());
202 if (0 === count($paths)) {
206 $command->
add($not ?
'-not' : null)->cmd(
'(');
208 foreach ($paths as $i => $path) {
212 if ($expr->isGlob() && $expr->getGlob()->isExpandable()) {
217 if ($expr->isRegex()) {
218 $regex = $expr->getRegex();
219 $regex->prepend($regex->hasStartFlag() ? preg_quote($dir).DIRECTORY_SEPARATOR :
'.*')->setEndJoker(!$regex->hasEndFlag());
221 $expr->prepend(
'*')->append(
'*');
225 ->
add($i > 0 ?
'-or' : null)
226 ->add($expr->isRegex()
227 ? ($expr->isCaseSensitive() ?
'-regex' :
'-iregex')
228 : ($expr->isCaseSensitive() ?
'-path' :
'-ipath')
230 ->arg($expr->renderPattern());
242 foreach ($sizes as $i => $size) {
243 $command->
add($i > 0 ?
'-and' : null);
245 switch ($size->getOperator()) {
247 $command->
add(
'-size -'.($size->getTarget() + 1).
'c');
250 $command->
add(
'-size +'.($size->getTarget() - 1).
'c');
253 $command->
add(
'-size +'.$size->getTarget().
'c');
256 $command->
add(
'-size -'.$size->getTarget().
'c');
257 $command->
add(
'-size +'.$size->getTarget().
'c');
261 $command->
add(
'-size -'.$size->getTarget().
'c');
272 foreach ($dates as $i => $date) {
273 $command->
add($i > 0 ?
'-and' : null);
275 $mins = (int) round((time() - $date->getTarget()) / 60);
279 $command->
add(
' -mmin -0');
284 switch ($date->getOperator()) {
286 $command->
add(
'-mmin +'.($mins - 1));
289 $command->
add(
'-mmin -'.($mins + 1));
292 $command->
add(
'-mmin -'.$mins);
295 $command->
add(
'-mmin +'.$mins.
' -or -mmin -'.$mins);
299 $command->
add(
'-mmin +'.$mins);