/**
  * {@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 (ehough_finder_iterator_FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode && ($this->contains || $this->notContains)) {
         return new ehough_finder_iterator_FilePathsIterator(array(), $dir);
     }
     $command = ehough_finder_shell_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 (ehough_finder_iterator_FileTypeFilterIterator::ONLY_DIRECTORIES === $this->mode) {
         $find->add('-type d');
     } elseif (ehough_finder_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(array($this, 'callbackAccessDeniedThrower'));
     $paths = $this->shell->testCommand('uniq') ? $command->add('| uniq')->execute() : array_unique($command->execute());
     $iterator = new ehough_finder_iterator_FilePathsIterator($paths, $dir);
     if ($this->exclude) {
         $iterator = new ehough_finder_iterator_ExcludeDirectoryFilterIterator($iterator, $this->exclude);
     }
     if (!$useGrep && ($this->contains || $this->notContains)) {
         $iterator = new ehough_finder_iterator_FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
     }
     if ($this->filters) {
         $iterator = new ehough_finder_iterator_CustomFilterIterator($iterator, $this->filters);
     }
     if (!$useSort && $this->sort) {
         $iteratorAggregate = new ehough_finder_iterator_SortableIterator($iterator, $this->sort);
         $iterator = $iteratorAggregate->getIterator();
     }
     return $iterator;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function searchInDirectory($dir)
 {
     if (version_compare(PHP_VERSION, '5.3') < 0) {
         $flags = 0;
     } else {
         $flags = RecursiveDirectoryIterator::SKIP_DOTS;
         if ($this->followLinks) {
             $flags |= RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
         }
     }
     $iterator = new ehough_finder_iterator_RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs);
     if ($this->exclude) {
         $iterator = new ehough_finder_iterator_ExcludeDirectoryFilterIterator($iterator, $this->exclude);
     }
     $iterator = new \RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
     if ($this->minDepth > 0 || $this->maxDepth < PHP_INT_MAX) {
         $iterator = new ehough_finder_iterator_DepthRangeFilterIterator($iterator, $this->minDepth, $this->maxDepth);
     }
     if ($this->mode) {
         $iterator = new ehough_finder_iterator_FileTypeFilterIterator($iterator, $this->mode);
     }
     if ($this->names || $this->notNames) {
         $iterator = new ehough_finder_iterator_FilenameFilterIterator($iterator, $this->names, $this->notNames);
     }
     if ($this->contains || $this->notContains) {
         $iterator = new ehough_finder_iterator_FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
     }
     if ($this->sizes) {
         $iterator = new ehough_finder_iterator_SizeRangeFilterIterator($iterator, $this->sizes);
     }
     if ($this->dates) {
         $iterator = new ehough_finder_iterator_DateRangeFilterIterator($iterator, $this->dates);
     }
     if ($this->filters) {
         $iterator = new ehough_finder_iterator_CustomFilterIterator($iterator, $this->filters);
     }
     if ($this->paths || $this->notPaths) {
         $iterator = new ehough_finder_iterator_PathFilterIterator($iterator, $this->paths, $this->notPaths);
     }
     if ($this->sort) {
         $iteratorAggregate = new ehough_finder_iterator_SortableIterator($iterator, $this->sort);
         $iterator = $iteratorAggregate->getIterator();
     }
     return $iterator;
 }
Exemplo n.º 3
0
 /**
  * @param $dir
  *
  * @return Iterator
  */
 private function searchInDirectory($dir)
 {
     if (self::IGNORE_VCS_FILES === (self::IGNORE_VCS_FILES & $this->ignore)) {
         $this->exclude = array_merge($this->exclude, self::$vcsPatterns);
     }
     if (self::IGNORE_DOT_FILES === (self::IGNORE_DOT_FILES & $this->ignore)) {
         $this->notPaths[] = '#(^|/)\\..+(/|$)#';
     }
     if ($this->adapters) {
         foreach ($this->adapters as $adapter) {
             if ($adapter['adapter']->isSupported()) {
                 try {
                     return $this->buildAdapter($adapter['adapter'])->searchInDirectory($dir);
                 } catch (ehough_finder_exception_ExceptionInterface $e) {
                 }
             }
         }
     }
     $minDepth = 0;
     $maxDepth = PHP_INT_MAX;
     foreach ($this->depths as $comparator) {
         switch ($comparator->getOperator()) {
             case '>':
                 $minDepth = $comparator->getTarget() + 1;
                 break;
             case '>=':
                 $minDepth = $comparator->getTarget();
                 break;
             case '<':
                 $maxDepth = $comparator->getTarget() - 1;
                 break;
             case '<=':
                 $maxDepth = $comparator->getTarget();
                 break;
             default:
                 $minDepth = $maxDepth = $comparator->getTarget();
         }
     }
     if (version_compare(PHP_VERSION, '5.3') < 0) {
         $flags = 0;
     } else {
         $flags = RecursiveDirectoryIterator::SKIP_DOTS;
         if ($this->followLinks) {
             $flags |= RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
         }
     }
     $iterator = new ehough_finder_iterator_RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs);
     if ($this->exclude) {
         $iterator = new ehough_finder_iterator_ExcludeDirectoryFilterIterator($iterator, $this->exclude);
     }
     $iterator = new \RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::SELF_FIRST);
     if ($minDepth > 0 || $maxDepth < PHP_INT_MAX) {
         $iterator = new ehough_finder_iterator_DepthRangeFilterIterator($iterator, $minDepth, $maxDepth);
     }
     if ($this->mode) {
         $iterator = new ehough_finder_iterator_FileTypeFilterIterator($iterator, $this->mode);
     }
     if ($this->names || $this->notNames) {
         $iterator = new ehough_finder_iterator_FilenameFilterIterator($iterator, $this->names, $this->notNames);
     }
     if ($this->contains || $this->notContains) {
         $iterator = new ehough_finder_iterator_FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
     }
     if ($this->sizes) {
         $iterator = new ehough_finder_iterator_SizeRangeFilterIterator($iterator, $this->sizes);
     }
     if ($this->dates) {
         $iterator = new ehough_finder_iterator_DateRangeFilterIterator($iterator, $this->dates);
     }
     if ($this->filters) {
         $iterator = new ehough_finder_iterator_CustomFilterIterator($iterator, $this->filters);
     }
     if ($this->paths || $this->notPaths) {
         $iterator = new ehough_finder_iterator_PathFilterIterator($iterator, $this->paths, $this->notPaths);
     }
     if ($this->sort) {
         $iteratorAggregate = new ehough_finder_iterator_SortableIterator($iterator, $this->sort);
         $iterator = $iteratorAggregate->getIterator();
     }
     return $iterator;
 }