Exemplo n.º 1
0
 public function scan($dir, $exclude = [])
 {
     $contexts = [];
     // Crawl directory and parse all files
     $finder = Util::finder($dir, $exclude);
     foreach ($finder as $file) {
         $context = $this->createContextFromFilename($file->getPathname());
         if (!empty($context)) {
             $contexts[] = $context;
         }
     }
     return $contexts;
 }
Exemplo n.º 2
0
 /**
  * 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;
 }