You are here

class LibraryDiscovery in System stream wrapper 8

Discovers available libraries in the filesystem.

Hierarchy

Expanded class hierarchy of LibraryDiscovery

1 file declares its use of LibraryDiscovery
LibraryStream.php in src/StreamWrapper/LibraryStream.php

File

src/Extension/LibraryDiscovery.php, line 11

Namespace

Drupal\system_stream_wrapper\Extension
View source
class LibraryDiscovery extends ExtensionDiscovery {

  /**
   * The directory libraries are registered in.
   */
  const LIBRARIES_DIRECTORY = 'libraries';

  /**
   * The type of this extension.
   */
  const EXTENSION_TYPE = 'library';

  /**
   * We don't want to affect the static cache of the core ExtensionDiscovery
   * class, so we keep one separately.
   *
   * @var array
   */
  protected static $files = array();

  /**
   * {@inheritdoc}
   */
  protected function scanDirectory($dir, $include_tests) {
    $files = array();

    // In order to scan top-level directories, absolute directory paths have to
    // be used (which also improves performance, since any configured PHP
    // include_paths will not be consulted). Retain the relative originating
    // directory being scanned, so relative paths can be reconstructed below
    // (all paths are expected to be relative to $this->root).
    $dir_prefix = ($dir == '' ? '' : "{$dir}/") . self::LIBRARIES_DIRECTORY . '/';
    $absolute_dir = $dir == '' ? $this->root : $this->root . "/{$dir}";
    $absolute_dir .= '/' . self::LIBRARIES_DIRECTORY;
    if (!is_dir($absolute_dir)) {
      return $files;
    }

    // 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::FOLLOW_SYMLINKS;
    $flags |= \FilesystemIterator::CURRENT_AS_SELF;
    $directory_iterator = new \RecursiveDirectoryIterator($absolute_dir, $flags);

    /**
     * @var string $key
     * @var \RecursiveDirectoryIterator $fileinfo
     */
    foreach ($directory_iterator as $key => $fileinfo) {
      if ($this->fileCache && ($cached_extension = $this->fileCache
        ->get($fileinfo
        ->getPathname()))) {
        $files[$cached_extension
          ->getType()][$key] = $cached_extension;
        continue;
      }
      if (!$fileinfo
        ->isDir()) {
        continue;
      }
      $type = self::EXTENSION_TYPE;
      $name = $fileinfo
        ->getBasename();
      $pathname = $dir_prefix . $fileinfo
        ->getSubPathname();
      $extension = new Extension($this->root, $type, $pathname);

      // Track the originating directory for sorting purposes.
      $extension->subpath = self::LIBRARIES_DIRECTORY . '/' . $fileinfo
        ->getFilename();
      $extension->origin = $dir;
      $files[$type][$key] = $extension;
      if ($this->fileCache) {
        $this->fileCache
          ->set($fileinfo
          ->getPathname(), $extension);
      }
    }
    return $files;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExtensionDiscovery::$fileCache protected property The file cache object.
ExtensionDiscovery::$profileDirectories protected property List of installation profile directories to additionally scan.
ExtensionDiscovery::$root protected property The app root for the current operation.
ExtensionDiscovery::$sitePath protected property The site path.
ExtensionDiscovery::filterByProfileDirectories protected function Filters out extensions not belonging to the scanned installation profiles.
ExtensionDiscovery::getProfileDirectories public function Gets the installation profile directories to be scanned.
ExtensionDiscovery::ORIGIN_CORE constant Origin directory weight: Core.
ExtensionDiscovery::ORIGIN_PARENT_SITE constant Origin directory weight: Parent site directory of a test site environment.
ExtensionDiscovery::ORIGIN_PROFILE constant Origin directory weight: Installation profile.
ExtensionDiscovery::ORIGIN_ROOT constant Origin directory weight: Site-wide directory.
ExtensionDiscovery::ORIGIN_SITE constant Origin directory weight: Site-specific directory.
ExtensionDiscovery::ORIGIN_SITES_ALL constant Origin directory weight: sites/all.
ExtensionDiscovery::PHP_FUNCTION_PATTERN constant Regular expression to match PHP function names.
ExtensionDiscovery::process protected function Processes the filtered and sorted list of extensions.
ExtensionDiscovery::scan public function Discovers available extensions of a given type.
ExtensionDiscovery::setProfileDirectories public function Sets explicit profile directories to scan.
ExtensionDiscovery::setProfileDirectoriesFromSettings public function Sets installation profile directories based on current site settings.
ExtensionDiscovery::sort protected function Sorts the discovered extensions.
ExtensionDiscovery::__construct public function Constructs a new ExtensionDiscovery object.
LibraryDiscovery::$files protected static property We don't want to affect the static cache of the core ExtensionDiscovery class, so we keep one separately. Overrides ExtensionDiscovery::$files
LibraryDiscovery::EXTENSION_TYPE constant The type of this extension.
LibraryDiscovery::LIBRARIES_DIRECTORY constant The directory libraries are registered in.
LibraryDiscovery::scanDirectory protected function Recursively scans a base directory for the extensions it contains. Overrides ExtensionDiscovery::scanDirectory