Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ExtensionDiscovery.php

function ExtensionDiscovery::scan

Same name in this branch
  1. 11.1.x core/lib/Drupal/Core/Extension/ExtensionDiscovery.php \Drupal\Core\Extension\ExtensionDiscovery::scan()

Discovers available extensions of a given type.

Finds all extensions (modules, themes, etc) that exist on the site. It searches in several locations. For instance, to discover all available modules:

$listing = new ExtensionDiscovery(\Drupal::root());
$modules = $listing->scan('module');

The following directories will be searched (in the order stated):

  • the core directory; i.e., /core
  • the installation profile directory; e.g., /core/profiles/standard
  • the legacy site-wide directory; i.e., /sites/all
  • the site-wide directory; i.e., /
  • the site-specific directory; e.g., /sites/example.com

To also find test modules, add

$settings['extension_discovery_scan_tests'] = TRUE;

to your settings.php.

The information is returned in an associative array, keyed by the extension name (without .info.yml extension). Extensions found later in the search will take precedence over extensions found earlier - unless they are not compatible with the current version of Drupal core.

Parameters

string $type: The extension type to search for. One of 'profile', 'module', 'theme', or 'theme_engine'.

Return value

\mglaman\PHPStanDrupal\Drupal\Extension[] An associative array of Extension objects, keyed by extension name.

File

vendor/mglaman/phpstan-drupal/src/Drupal/ExtensionDiscovery.php, line 132

Class

ExtensionDiscovery

Namespace

mglaman\PHPStanDrupal\Drupal

Code

public function scan($type) {
    static $scanresult;
    if (!$scanresult) {
        $scanresult = [];
    }
    if (isset($scanresult[$type])) {
        return $scanresult[$type];
    }
    $searchdirs = [];
    // Search the core directory.
    $searchdirs[self::ORIGIN_CORE] = 'core';
    // Search the legacy sites/all directory.
    $searchdirs[self::ORIGIN_SITES_ALL] = 'sites/all';
    // Search for contributed and custom extensions in top-level directories.
    // The scan uses a whitelist to limit recursion to the expected extension
    // type specific directory names only.
    $searchdirs[self::ORIGIN_ROOT] = '';
    $searchdirs[self::ORIGIN_SITE] = $this->sitePath;
    $files = [];
    foreach ($searchdirs as $dir) {
        // Discover all extensions in the directory, unless we did already.
        if (!isset(static::$files[$this->root][$dir])) {
            static::$files[$this->root][$dir] = $this->scanDirectory($dir);
        }
        // Only return extensions of the requested type.
        if (isset(static::$files[$this->root][$dir][$type])) {
            $files += static::$files[$this->root][$dir][$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($searchdirs);
    $files = $this->sort($files, $origin_weights);
    // Process and return the list of extensions keyed by extension name.
    $scanresult[$type] = $this->process($files);
    return $scanresult[$type];
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal