/**
  * {@inheritdoc}
  */
 public function searchInDirectory($dir)
 {
     // having "/../" in path make find fail
     $dir = realpath($dir);
     // searching directories containing or not containing strings leads to no result
     if (Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode && ($this->contains || $this->notContains)) {
         return new Iterator\FilePathsIterator(array(), $dir);
     }
     $command = Command::create();
     $find = $this->buildFindCommand($command, $dir);
     if ($this->followLinks) {
         $find->add('-follow');
     }
     $find->add('-mindepth')->add($this->minDepth + 1);
     if (PHP_INT_MAX !== $this->maxDepth) {
         $find->add('-maxdepth')->add($this->maxDepth + 1);
     }
     if (Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode) {
         $find->add('-type d');
     } elseif (Iterator\FileTypeFilterIterator::ONLY_FILES === $this->mode) {
         $find->add('-type f');
     }
     $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);
     $useGrep = $this->shell->testCommand('grep') && $this->shell->testCommand('xargs');
     $useSort = is_int($this->sort) && $this->shell->testCommand('sort') && $this->shell->testCommand('cut');
     if ($useGrep && ($this->contains || $this->notContains)) {
         $grep = $command->ins('grep');
         $this->buildContentFiltering($grep, $this->contains);
         $this->buildContentFiltering($grep, $this->notContains, true);
     }
     if ($useSort) {
         $this->buildSorting($command, $this->sort);
     }
     $command->setErrorHandler($this->ignoreUnreadableDirs ? function ($stderr) {
         return;
     } : function ($stderr) {
         throw new AccessDeniedException($stderr);
     });
     $paths = $this->shell->testCommand('uniq') ? $command->add('| uniq')->execute() : array_unique($command->execute());
     $iterator = new Iterator\FilePathsIterator($paths, $dir);
     if ($this->exclude) {
         $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
     }
     if (!$useGrep && ($this->contains || $this->notContains)) {
         $iterator = new Iterator\FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
     }
     if ($this->filters) {
         $iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
     }
     if (!$useSort && $this->sort) {
         $iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort);
         $iterator = $iteratorAggregate->getIterator();
     }
     return $iterator;
 }
Exemple #2
0
 public function testCastToString()
 {
     $cmd = Command::create();
     $cmd->add('--force');
     $cmd->add('--run');
     $this->assertSame('--force --run', (string) $cmd);
 }
Exemple #3
0
    /**
     * {@inheritdoc}
     */
    public function searchInDirectory($dir)
    {
        // having "/../" in path make find fail
        $dir = realpath($dir);

        // searching directories containing or not containing strings leads to no result
        if (Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode && ($this->contains || $this->notContains)) {
            return new Iterator\FilePathsIterator(array(), $dir);
        }

        $command = Command::create();

        $find = $command
            ->ins('find')
            ->add('find ')
            ->arg($dir)
            ->add('-noleaf') // -noleaf option is required for filesystems who doesn't follow '.' and '..' convention
            ->add('-regextype posix-extended');

        if ($this->followLinks) {
            $find->add('-follow');
        }

        $find->add('-mindepth')->add($this->minDepth+1);
        // warning! INF < INF => true ; INF == INF => false ; INF === INF => true
        // https://bugs.php.net/bug.php?id=9118
        if (INF !== $this->maxDepth) {
            $find->add('-maxdepth')->add($this->maxDepth+1);
        }

        if (Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode) {
            $find->add('-type d');
        } elseif (Iterator\FileTypeFilterIterator::ONLY_FILES === $this->mode) {
            $find->add('-type f');
        }

        $this->buildNamesFiltering($find, $this->names);
        $this->buildNamesFiltering($find, $this->notNames, true);
        $this->buildSizesFiltering($find, $this->sizes);
        $this->buildDatesFiltering($find, $this->dates);

        $useGrep = $this->shell->testCommand('grep') && $this->shell->testCommand('xargs');
        $useSort = is_int($this->sort) && $this->shell->testCommand('sort') && $this->shell->testCommand('awk');

        if ($useGrep && ($this->contains || $this->notContains)) {
            $grep = $command->ins('grep');
            $this->buildContentFiltering($grep, $this->contains);
            $this->buildContentFiltering($grep, $this->notContains, true);
        }

        if ($useSort) {
            $this->buildSorting($command, $this->sort);
        }

        $paths = $this->shell->testCommand('uniq') ? $command->add('| uniq')->execute() : array_unique($command->execute());
        $iterator = new Iterator\FilePathsIterator($paths, $dir);

        if ($this->exclude) {
            $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
        }

        if (!$useGrep && ($this->contains || $this->notContains)) {
            $iterator = new Iterator\FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
        }

        if ($this->filters) {
            $iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
        }

        if (!$useSort && $this->sort) {
            $iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort);
            $iterator = $iteratorAggregate->getIterator();
        }

        return $iterator;
    }