protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln('Sync our local folder with the S3 Bucket...'); $sync = $this->getApplication()->make('operations.s3-sync'); $sync->sync(); $output->writeln('Getting the list of files to be processed...'); $finder = new Finder(); $finder->files('*.json')->in($this->directories['files_dir'])->sort(function ($a, $b) { return strnatcmp($a->getRealPath(), $b->getRealPath()); }); if (file_exists($this->directories['last_read_file'])) { $lastFile = file_get_contents($this->directories['last_read_file']); $finder->filter(function ($file) use($lastFile) { if (0 >= strnatcmp($file->getFilename(), $lastFile)) { return false; } return true; }); } $importer = $this->getApplication()->make('operations.file-importer'); foreach ($finder as $file) { $output->writeln('Processing the events from ' . $file->getFilename() . '...'); $importer->from($file); file_put_contents($this->directories['last_read_file'], $file->getFilename()); } }
/** * {@inheritDoc} * * @param callable $callable * * @return self * * @throws \InvalidArgumentException When the $callable isn't a callable */ public function filter($callable) { if (!is_callable($callable)) { throw new \InvalidArgumentException('The filter should be a PHP callable'); } parent::filter(function (\SplFileInfo $file) use($callable) { return call_user_func_array($callable, array($file)); }); return $this; }
protected function getFinderObject($isLibrary, $dirs) { foreach ($dirs as $id => $dir) { $dirs[$id] = $this->getPath($dir); } $finder = new Finder(); $finder->files()->name("*.php")->in($dirs); if ($isLibrary) { $relative = true; $include = false; $finder->filter(function ($file) { return preg_match("/test/i", $file) ? false : true; }); } return $finder; }
/** * @param array $directoriesWhiteList * @return Finder */ protected function getConfigFinder(array $directoriesWhiteList = array()) { $configDirectories = $this->getConfigDirectories($directoriesWhiteList); // prepare finder $finder = new Finder(); $finder->in($configDirectories)->name($this->getConfigFilePattern()); if ($directoriesWhiteList) { $finder->filter(function ($file) use($directoriesWhiteList) { foreach ($directoriesWhiteList as $allowedDirectory) { if ($allowedDirectory && strpos($file, realpath($allowedDirectory) . DIRECTORY_SEPARATOR) === 0) { return true; } } return false; }); } return $finder; }
/** * Returns a Finder instance for the files that will be included in the * backup. * * By default we ignore unreadable files and directories as well as, common * version control folders / files, "Dot" files and anything matching the * exclude rules. * * @uses Finder * @return Finder The Finder iterator of all files to be included */ public function get_files() { $finder = new Finder(); $finder->followLinks(true); $finder->ignoreDotFiles(false); $finder->ignoreVCS(true); $finder->ignoreUnreadableDirs(true); // Skip unreadable files too $finder->filter(function (\SplFileInfo $file) { if (!$file->isReadable()) { return false; } }); // Finder expects exclude rules to be in a regex format $exclude_rules = $this->excludes->get_excludes_for_regex(); // Skips folders/files that match default exclude patterns foreach ($exclude_rules as $exclude) { $finder->notPath($exclude); } return $finder->in(Path::get_root()); }
/** * Scans the directory for files and directories recursively. * * @param mixed $filter Array of methods to arguments for `Finder`, * \Closure instance for filter, strings 'directories' or 'files', * regular expression, or glob string. * * @return array Array of `File` and `Directory` objects. * * @see Finder * * @replaces #scanRecursive */ public function scanRecursive($filter = null) { $this->tossIfDeleted(); $objects = array(); $finder = new Finder(); $finder->in($this->directory)->ignoreDotFiles(false)->ignoreVCS(false); if ($filter) { if (is_array($filter)) { foreach ($filter as $method => $arg) { if (!isset(static::$allowedFinderMethodsForFilter[$method])) { continue; } $finder->{$method}($arg); } } else { if ($filter instanceof \Closure) { $finder->filter($filter); } else { if ($filter === 'directories') { $finder->directories(); } else { if ($filter === 'files') { $finder->files(); } else { $finder->name($filter); } } } } } foreach ($finder as $file) { $objects[] = static::$fs->createObject($file); } return $objects; }
/** * Returns directory contents * * @param string $dir * @param mixed $names * @param mixed $notNames * @param boolean $dirs * @param boolean $files * @param integer $sort * @return array */ public static function readDir($dir, $names = '*', $notNames = false, $dirs = true, $files = true, $sort = SORT_DESC) { $found = array(); $finder = new Finder(); $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir); if ($dirs && !$files) { $finder->directories(); } elseif (!$dirs && $files) { $finder->files(); } foreach ((array) $names as $name) { $finder->name($name); } if ($notNames !== false) { $regExps = array(); foreach ((array) $notNames as $notName) { $regExps[] = Glob::toRegex($notName, false, false); } $finder->filter(function (\SplFileInfo $fileinfo) use($dir, $regExps) { $localName = str_replace(array($dir . DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), array(NULL, '/'), $fileinfo->getRealpath()); foreach ($regExps as $regExp) { if (preg_match($regExp, $localName)) { return false; } } return true; }); } foreach ($finder->in($dir) as $path) { $found[] = realpath($path->getRealpath()); } return self::sortByDepth($found, $sort); }
/** * @return Finder */ public function filter(Closure $closure) { return parent::filter($closure); }