/** * @param array $filters * @return $this */ public function withFilters(array $filters) { if (!empty($filters)) { foreach ($filters as $currentFilter) { if (!strpos($currentFilter, ':')) { throw new \InvalidArgumentException(sprintf('The filter "%s" is not a valid filter. A valid filter has the format <name>:<value>.', $currentFilter)); } $currentFilterElements = explode(':', $currentFilter, 2); switch (trim($currentFilterElements[0])) { case 'exclude': $this->finder->exclude($currentFilterElements[1]); break; case 'name': $this->finder->name($currentFilterElements[1]); break; case 'notName': $this->finder->notName($currentFilterElements[1]); break; case 'path': $this->finder->path($currentFilterElements[1]); break; case 'size': $this->finder->size($currentFilterElements[1]); } } } return $this; }
public function index() { /* |-------------------------------------------------------------------------- | Paramers |-------------------------------------------------------------------------- | | Match overrides Extension. Exclusion applies in both cases. | */ $match = $this->fetchParam('match', false); $exclude = $this->fetchParam('exclude', false); $extension = $this->fetchParam('extension', false); $in = $this->fetchParam('in', false); $not_in = $this->fetchParam('not_in', false); $file_size = $this->fetchParam('file_size', false); $file_date = $this->fetchParam('file_date', false); $depth = $this->fetchParam('depth', false); if ($file_size) { $file_size = Helper::explodeOptions($file_size); } if ($extension) { $extension = Helper::explodeOptions($extension); } /* |-------------------------------------------------------------------------- | Finder |-------------------------------------------------------------------------- | | Get_Files implements most of the Symfony Finder component as a clean | tag wrapper mapped to matched filenames. | */ $finder = new Finder(); $finder->in($in); // Finder doesn't respect multiple glob options, // so this will need to wait until later. // // $match = str_replace('{{', '{', $match); // $match = str_replace('}}', '}', $match); /* |-------------------------------------------------------------------------- | Name |-------------------------------------------------------------------------- | | Match is the "native" Finder name() method, which is supposed to | implement string, glob, and regex. The glob support is only partial, | so "extension" is a looped *single* glob rule iterator. | */ if ($match) { $finder->name($match); } elseif ($extension) { foreach ($extension as $ext) { $finder->name("*.{$ext}"); } } /* |-------------------------------------------------------------------------- | Exclude |-------------------------------------------------------------------------- | | Exclude directories from matching. Remapped to "not in" to allow more | intuitive differentiation between filename and directory matching. | */ if ($not_in) { $finder->exclude($not_in); } /* |-------------------------------------------------------------------------- | Not Name |-------------------------------------------------------------------------- | | Exclude files matching a given pattern: string, regex, or glob. | */ if ($exclude) { $finder->notName($exclude); } /* |-------------------------------------------------------------------------- | File Size |-------------------------------------------------------------------------- | | Restrict files by size. Can be chained and allows comparison operators. | */ if ($file_size) { foreach ($file_size as $size) { $finder->size($size); } } /* |-------------------------------------------------------------------------- | File Date |-------------------------------------------------------------------------- | | Restrict files by last modified date. Can use comparison operators, and | since/after is aliased to >, and until/before to <. | */ if ($file_date) { $finder->date($file_date); } /* |-------------------------------------------------------------------------- | Depth |-------------------------------------------------------------------------- | | Recursively traverse directories, starting at 0. | */ if ($depth) { $finder->depth($depth); } $matches = $finder->files(); /* |-------------------------------------------------------------------------- | Return and Parse |-------------------------------------------------------------------------- | | This tag returns the matched filenames mapped to {{ file }}. | */ $files = array(); foreach ($matches as $file) { $files[] = array('file' => '/' . $in . '/' . $file->getRelativePathname()); // $files[] = YAML::parse($file->getContents()); } return Parse::tagLoop($this->content, $files); }
public function index() { /* |-------------------------------------------------------------------------- | Paramers |-------------------------------------------------------------------------- | | Match overrides Extension. Exclusion applies in both cases. | */ $match = $this->fetchParam('match', false); $exclude = $this->fetchParam('exclude', false); $extension = $this->fetchParam(array('extension', 'type'), false); $in = $this->fetchParam(array('in', 'folder', 'from'), false); $not_in = $this->fetchParam('not_in', false); $file_size = $this->fetchParam('file_size', false); $file_date = $this->fetchParam('file_date', false); $depth = $this->fetchParam('depth', false); $sort_by = $this->fetchParam(array('sort_by', 'order_by'), false); $sort_dir = $this->fetchParam(array('sort_dir', 'sort_direction'), 'asc'); $limit = $this->fetchParam('limit', false); if ($in) { $in = Helper::explodeOptions($in); } if ($not_in) { $not_in = Helper::explodeOptions($not_in); } if ($file_size) { $file_size = Helper::explodeOptions($file_size); } if ($extension) { $extension = Helper::explodeOptions($extension); } /* |-------------------------------------------------------------------------- | Finder |-------------------------------------------------------------------------- | | Get_Files implements most of the Symfony Finder component as a clean | tag wrapper mapped to matched filenames. | */ $finder = new Finder(); if ($in) { foreach ($in as $location) { $finder->in(Path::fromAsset($location)); } } /* |-------------------------------------------------------------------------- | Name |-------------------------------------------------------------------------- | | Match is the "native" Finder name() method, which is supposed to | implement string, glob, and regex. The glob support is only partial, | so "extension" is a looped *single* glob rule iterator. | */ if ($match) { $finder->name($match); } elseif ($extension) { foreach ($extension as $ext) { $finder->name("*.{$ext}"); } } /* |-------------------------------------------------------------------------- | Exclude |-------------------------------------------------------------------------- | | Exclude directories from matching. Remapped to "not in" to allow more | intuitive differentiation between filename and directory matching. | */ if ($not_in) { foreach ($not_in as $location) { $finder->exclude($location); } } /* |-------------------------------------------------------------------------- | Not Name |-------------------------------------------------------------------------- | | Exclude files matching a given pattern: string, regex, or glob. | By default we don't allow looking for PHP files. Be smart. | */ if ($this->fetchParam('allow_php', false) !== TRUE) { $finder->notName("*.php"); } if ($exclude) { $finder->notName($exclude); } /* |-------------------------------------------------------------------------- | File Size |-------------------------------------------------------------------------- | | Restrict files by size. Can be chained and allows comparison operators. | */ if ($file_size) { foreach ($file_size as $size) { $finder->size($size); } } /* |-------------------------------------------------------------------------- | File Date |-------------------------------------------------------------------------- | | Restrict files by last modified date. Can use comparison operators, and | since/after is aliased to >, and until/before to <. | */ if ($file_date) { $finder->date($file_date); } /* |-------------------------------------------------------------------------- | Depth |-------------------------------------------------------------------------- | | Recursively traverse directories, starting at 0. | */ if ($depth) { $finder->depth($depth); } /* |-------------------------------------------------------------------------- | Sort By |-------------------------------------------------------------------------- | | Sort by name, file, or type | */ if ($sort_by) { if ($sort_by === 'file' || $sort_by === 'name') { $finder->sortByName(); } elseif ($sort_by === 'type') { $finder->sortByType(); } } /* |-------------------------------------------------------------------------- | Assemble File Array |-------------------------------------------------------------------------- | | Select the important bits of data on the list of files. | */ $matches = $finder->files()->followLinks(); $files = array(); foreach ($matches as $file) { $files[] = array('extension' => $file->getExtension(), 'filename' => $file->getFilename(), 'file' => Path::toAsset($file->getPathname()), 'name' => Path::toAsset($file->getPathname()), 'size' => File::getHumanSize($file->getSize()), 'size_bytes' => $file->getSize(), 'size_kilobytes' => number_format($file->getSize() / 1024, 2), 'size_megabytes' => number_format($file->getSize() / 1048576, 2), 'size_gigabytes' => number_format($file->getSize() / 1073741824, 2), 'is_image' => File::isImage($file->getPathname())); } /* |-------------------------------------------------------------------------- | Sort Direction |-------------------------------------------------------------------------- | | Set the sort direction, defaulting to "asc" (ascending) | */ if ($sort_dir === 'desc') { $files = array_reverse($files); } /* |-------------------------------------------------------------------------- | Randomizing |-------------------------------------------------------------------------- | | You can't sort randomly using Symfony finder, so we'll do it manually. | */ if ($sort_by === 'random') { shuffle($files); } /* |-------------------------------------------------------------------------- | Limit Files |-------------------------------------------------------------------------- | | Limit the number of files returned. Needs to be run after sort_dir to | ensure consistency. | */ if ($limit) { $files = array_slice($files, 0, $limit); } return Parse::tagLoop($this->content, $files, true, $this->context); }
/** * @param InputInterface $input * @return Finder */ public function createFinder(InputInterface $input) { $finder = new Finder(); $finder->files(); foreach ($input->getArgument('directory') as $dir) { $finder->in($dir); } foreach ($input->getOption('not-dir') as $ignoreDir) { $finder->exclude($ignoreDir); } foreach ($input->getOption('file-name') as $pattern) { $finder->name($pattern); } foreach ($input->getOption('not-file-name') as $pattern) { $finder->notName($pattern); } foreach ($input->getOption('contains') as $pattern) { $finder->contains($pattern); } foreach ($input->getOption('not-contains') as $pattern) { $finder->notContains($pattern); } foreach ($input->getOption('path') as $pattern) { $finder->path($pattern); } foreach ($input->getOption('not-path') as $pattern) { $finder->notPath($pattern); } if ($size = $input->getOption('size')) { $finder->size($size); } if ($modified = $input->getOption('modified')) { $finder->date($modified); } if ($depth = $input->getOption('depth')) { $finder->depth($depth); } return $finder; }
/** * @param \DOMNode $node * @return \Iterator|null */ protected function _parseIterator(DOMNode $node) { $finder = new Finder(); $finder->files(); foreach ($node->childNodes as $option) { if ($option->nodeType === XML_ELEMENT_NODE) { $nodeName = strtolower($option->nodeName); $value = $option->nodeValue; switch ($nodeName) { case 'name': $finder->name($value); break; case 'notname': $finder->notName($value); break; case 'path': $finder->in($value); break; case 'size': $finder->size($value); break; case 'exclude': $finder->exclude($value); break; } } } return $finder->getIterator(); }
/** * @return Finder */ public function size($size) { return parent::size($size); }