You are here

public function ComponentDiscovery::scan in Decoupled Blocks 8

Extends to provide user defined paths to look for components.

Overrides ExtensionDiscovery::scan

1 call to ComponentDiscovery::scan()
ComponentDiscovery::getComponents in src/ComponentDiscovery.php
Find all available front-end components.

File

src/ComponentDiscovery.php, line 102

Class

ComponentDiscovery
Discovery service for front-end components provided by modules and themes.

Namespace

Drupal\pdb

Code

public function scan($type, $include_tests = NULL) {

  // Try to get search dirs from settings.php.
  $search_dirs = Settings::get('pdb_search_dirs', []);
  if (is_string($search_dirs)) {
    $search_dirs = [
      $search_dirs,
    ];
  }

  // Try to get search dirs from subscribers.
  $event = new PdbDiscoveryEvent($search_dirs);
  $this->eventDispatcher
    ->dispatch(PdbDiscoveryEvent::SEARCH_DIRS, $event);

  // Get the updated dicovery path from subscribers.
  $search_dirs = $event
    ->getDirs();

  // If user is not defining any custom path, then do a global discovery
  // by following parent's approach.
  if (empty($search_dirs)) {
    $this->globalDiscovery = TRUE;
    return parent::scan($type, $include_tests);
  }

  // Based on parent::scan().
  $files = [];
  foreach ($search_dirs as $dir) {

    // Discover all extensions in the directory, unless we did already.
    if (!isset(static::$files[$this->root][$dir][$include_tests])) {
      static::$files[$this->root][$dir][$include_tests] = $this
        ->scanDirectory($dir, $include_tests);
    }

    // Only return extensions of the requested type.
    if (isset(static::$files[$this->root][$dir][$include_tests][$type])) {
      $files += static::$files[$this->root][$dir][$include_tests][$type];
    }
  }

  // If applicable, filter out extensions that do not belong to the current
  // installation profiles.
  $files = $this
    ->filterByProfileDirectories($files);

  // Sort the discovered extensions by their originating directories.
  $origin_weights = array_flip($search_dirs);
  $files = $this
    ->sort($files, $origin_weights);

  // Process and return the list of extensions keyed by extension name.
  return $this
    ->process($files);
}