/** * 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(); } } }
/** * @param $path * @param $extensions * @return Finder */ protected function getFileIterator($path, $extensions) { $fileInfo = new SplFileInfo($path); $finder = new Finder(); if ($fileInfo->isFile()) { return $finder->append(array($fileInfo)); } return $finder->files()->in($path)->name('/\\.(' . implode('|', $extensions) . ')$/i'); }
public function serveMarkdown($mdFile) { $basePath = dirname($mdFile) . '/' . basename($mdFile, '.md'); $source = new Finder(); $source->append([$mdFile]); if (is_dir($basePath)) { $children = new Finder(); $children->in($basePath)->name('*.md')->sortByName(); $source->append($children); } $merger = new Merge(); $content = $merger->mergeFiles($source, $basePath); $converter = new Converter(); $html = $converter->toHtml($content); EventManager::dispatch('preHeader'); header('Content-Type: text/html; charset=utf-8'); EventManager::dispatch('html', $html); }
/** * Find all php files by path. * * @param string $path Path * * @return Finder Finder iterable object with all PHP found files in path */ public function findPHPFilesByPath($path) { $finder = new Finder(); if (file_exists($path) && !is_dir($path)) { $finder->append([0 => $path]); } else { $finder->files()->in($path)->name('*.php'); } return $finder; }
/** * Build a Symfony Finder object that scans the given $directory. * * @param string|array|Finder $directory The directory(s) or filename(s) * @param null|string|array $exclude The directory(s) or filename(s) to exclude (as absolute or relative paths) * @throws InvalidArgumentException */ public static function finder($directory, $exclude = null) { if ($directory instanceof Finder) { return $directory; } else { $finder = new Finder(); $finder->sortByName(); } $finder->files(); if (is_string($directory)) { if (is_file($directory)) { // Scan a single file? $finder->append([$directory]); } else { // Scan a directory $finder->in($directory); } } elseif (is_array($directory)) { foreach ($directory as $path) { if (is_file($path)) { // Scan a file? $finder->append([$path]); } else { $finder->in($path); } } } else { throw new InvalidArgumentException('Unexpected $directory value:' . gettype($directory)); } if ($exclude !== null) { if (is_string($exclude)) { $finder->notPath(Util::getRelativePath($exclude, $directory)); } elseif (is_array($exclude)) { foreach ($exclude as $path) { $finder->notPath(Util::getRelativePath($path, $directory)); } } else { throw new InvalidArgumentException('Unexpected $exclude value:' . gettype($exclude)); } } return $finder; }
public function getFiles() { $finder = new Finder(); try { $finder->in($this->getFindPath())->files(); } catch (\InvalidArgumentException $e) { //catch non-existing directory exception. //This can happen if getFiles is called and no file has yet been uploaded //push empty array into the finder so we can emulate no files found $finder->append(array()); } return $finder; }
protected function createFromArray(array $paths) { $finder = new Finder(); $files = []; $dirs = []; foreach ($paths as $path) { if (is_file($path)) { $files[] = $path; continue; } $dirs[] = $path; } return $finder->append($files)->in($dirs); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $finder = new Finder(); $path = $input->getOption('cwd'); if (file_exists($path) && !is_dir($path)) { $finder->append([0 => $path]); } else { $finder->files()->in($path)->name('*.php'); } $license = $this->getLicenseText($input->getOption('package'), $input->getOption('author')); $t = 0; foreach ($finder as $file) { $data = file_get_contents($file->getRealpath()); $tokens = token_get_all($data); $content = ''; $need_license = true; for ($i = 0, $c = count($tokens); $i < $c; ++$i) { if (!is_array($tokens[$i])) { $content .= $tokens[$i]; continue; } if (T_DOC_COMMENT === $tokens[$i][0] && $this->isPHPStormLicense($tokens[$i][1])) { // Delete Auto Generated PHPStorm comments continue; } else { if (T_COMMENT === $tokens[$i][0] && $this->isOldLicense($tokens[$i][1])) { $content .= $license; $need_license = false; } else { $content .= $tokens[$i][1]; } } } if ($need_license) { $content = preg_replace('/<\\?php/', "<?php\n\n" . trim($license) . "\n\n", $data, 1); } file_put_contents($file->getRealpath(), $content); $output->writeln(sprintf('[Modify] <comment>%s</comment>', $file->getPathname())); ++$t; } $output->writeln("<info>Command finished successfully. Licensified {$t} files.</info>"); }
/** * Copy the rest files to destination * * @return array Filenames affected */ public function copyRestToDestination() { $fs = new Filesystem(); $result = array(); $includedFiles = array(); $dir = $this->getSourceDir(); $include = $this->configuration->getRepository()->get('include'); $exclude = $this->configuration->getRepository()->get('exclude'); $processableExt = $this->getProcessableExtention(); $finder = new Finder(); $finder->in($dir)->exclude($this->getSpecialDir()); $finder->notName($this->configuration->getConfigFilename()); $finder->notName($this->configuration->getConfigEnvironmentFilenameWildcard()); $finder->notName($this->fileExtToRegExpr($processableExt)); foreach ($include as $item) { if (is_dir($item)) { $finder->in($item); } else { if (is_file($item) && !in_array(pathinfo($item, PATHINFO_EXTENSION), $processableExt)) { $includedFiles[] = new SplFileInfo($this->resolvePath($item), "", pathinfo($item, PATHINFO_BASENAME)); } } } foreach ($exclude as $item) { $finder->notPath($item); } $finder->append($includedFiles); foreach ($finder as $file) { if ($file->isDir()) { $this->mkDirIfNotExists($this->getDestinationDir() . '/' . $file->getRelativePath()); } else { if ($file->isFile()) { $result[] = $file->getRealpath(); $fs->copy($file->getRealpath(), $this->getDestinationDir() . '/' . $file->getRelativePathname()); } } } return $result; }
private function processContentFiles() { $includedFiles = []; $finder = new Finder(); $finder->in($this->composeSubPath('content'))->notName('*.meta')->files(); foreach ($this->params['include'] as $item) { if (is_dir($item)) { $finder->in($item); } elseif (is_file($item)) { $includedFiles[] = new SplFileInfo($item, '', pathinfo($item, PATHINFO_BASENAME)); } } $finder->append($includedFiles); foreach ($this->params['exclude'] as $item) { $finder->notPath($item); } $this->processItems($finder, Item::TYPE_ITEM); }
public function testAppendWithAnArray() { $finder = new Finder(); $finder->files()->in(self::$tmpDir . DIRECTORY_SEPARATOR . 'foo'); $finder->append($this->toAbsolute(array('foo', 'toto'))); $this->assertIterator($this->toAbsolute(array('foo', 'foo/bar.tmp', 'toto')), $finder->getIterator()); }
/** * * @return Finder */ protected function getFinder() { if (null === $this->finder) { $finder = new Finder(); $settings = $this->getSettings(); foreach ($settings->getSrc() as $source) { if (is_dir($source)) { $finder->in($source); } if (is_file($source)) { $finder->append(array($source)); } } if (true === $settings->getFollowLinks()) { $finder->followLinks(); } $finder->ignoreDotFiles($settings->getIgnoreDotFiles()); $finder->name($settings->getFileSuffix()); $finder->exclude($settings->getExclude()); if (NULL !== $settings->getDate()) { $finder->date($settings->getDate()); } $this->setFinder($finder); } return $this->finder; }
/** * @return Finder */ public function append($iterator) { return parent::append($iterator); }