You are here

class Scanner in Default Content for D8 8

A scanner to find YAML files in a given folder.

Hierarchy

Expanded class hierarchy of Scanner

1 string reference to 'Scanner'
default_content.services.yml in ./default_content.services.yml
default_content.services.yml
1 service uses Scanner
default_content.scanner in ./default_content.services.yml
Drupal\default_content\Scanner

File

src/Scanner.php, line 8

Namespace

Drupal\default_content
View source
class Scanner implements ScannerInterface {

  /**
   * {@inheritdoc}
   */
  public function scan($directory) {

    // Use Unix paths regardless of platform, skip dot directories, follow
    // symlinks (to allow extensions to be linked from elsewhere), and return
    // the RecursiveDirectoryIterator instance to have access to getSubPath(),
    // since SplFileInfo does not support relative paths.
    $flags = \FilesystemIterator::UNIX_PATHS;
    $flags |= \FilesystemIterator::SKIP_DOTS;
    $flags |= \FilesystemIterator::CURRENT_AS_SELF;
    $directory_iterator = new \RecursiveDirectoryIterator($directory, $flags);
    $iterator = new \RecursiveIteratorIterator($directory_iterator);
    $files = [];
    foreach ($iterator as $fileinfo) {

      /* @var \SplFileInfo $fileinfo */

      // Skip directories and non-json files.
      if ($fileinfo
        ->isDir() || $fileinfo
        ->getExtension() != 'json') {
        continue;
      }

      // @todo Use a typed class?
      $file = new \stdClass();
      $file->name = $fileinfo
        ->getFilename();
      $file->uri = $fileinfo
        ->getPathname();
      $files[$file->uri] = $file;
    }
    return $files;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Scanner::scan public function Returns a list of file objects. Overrides ScannerInterface::scan