Symfony\Component\Finder\Finder::append PHP Method

append() public method

The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
public append ( mixed $iterator ) : Finder | Symfony\Component\Finder\SplFileInfo[]
$iterator mixed
return Finder | Symfony\Component\Finder\SplFileInfo[] The finder
    public function append($iterator)
    {
        if ($iterator instanceof \IteratorAggregate) {
            $this->iterators[] = $iterator->getIterator();
        } elseif ($iterator instanceof \Iterator) {
            $this->iterators[] = $iterator;
        } elseif ($iterator instanceof \Traversable || is_array($iterator)) {
            $it = new \ArrayIterator();
            foreach ($iterator as $file) {
                $it->append($file instanceof \SplFileInfo ? $file : new \SplFileInfo($file));
            }
            $this->iterators[] = $it;
        } else {
            throw new \InvalidArgumentException('Finder::append() method wrong argument type.');
        }

        return $this;
    }

Usage Example

Example #1
0
 /**
  * Track a set of paths
  *
  * @param array $paths the list of directories or files to track / follow
  */
 public function __construct(array $paths)
 {
     //Filter out the files, the Finder class can not handle files in the ->in() call.
     $files = array_filter($paths, function ($possible_file) {
         return is_file($possible_file);
     });
     //Filter out the directories to be used for searching using the Filter class.
     $dirs = array_filter($paths, function ($possible_dir) {
         return is_dir($possible_dir);
     });
     $finder = new Finder();
     //Add the given 'stand-alone-files'
     $finder->append($files);
     //Add the Directores recursively
     $finder = $finder->in($dirs);
     //Filter out non readable files
     $finder = $finder->filter(function (SplFileInfo $finder) {
         return $finder->isReadable();
     });
     //Loop through all the files and save the latest modification time.
     foreach ($finder->files() as $file) {
         /*@var $file \SplFileInfo */
         if ($this->modification_time < $file->getMTime()) {
             $this->modification_time = $file->getMTime();
         }
     }
 }
All Usage Examples Of Symfony\Component\Finder\Finder::append