class DatabaseDriverList

Same name in other branches
  1. 10 core/lib/Drupal/Core/Extension/DatabaseDriverList.php \Drupal\Core\Extension\DatabaseDriverList

Provides a list of available database drivers.

@internal This class is not yet stable and therefore there are no guarantees that the internal implementations including constructor signature and protected properties / methods will not change over time. This will be reviewed after https://www.drupal.org/project/drupal/issues/2940481

Hierarchy

Expanded class hierarchy of DatabaseDriverList

3 files declare their use of DatabaseDriverList
Database.php in core/lib/Drupal/Core/Database/Database.php
DriverModuleMissingDependenciesTest.php in core/tests/Drupal/Tests/Core/Database/DriverModuleMissingDependenciesTest.php
SiteSettingsForm.php in core/lib/Drupal/Core/Installer/Form/SiteSettingsForm.php

File

core/lib/Drupal/Core/Extension/DatabaseDriverList.php, line 16

Namespace

Drupal\Core\Extension
View source
class DatabaseDriverList extends ExtensionList {
    
    /**
     * The namespace of core's MySql database driver.
     */
    protected const CORE_MYSQL_DRIVER_NAMESPACE = 'Drupal\\mysql\\Driver\\Database\\mysql';
    
    /**
     * Determines whether test drivers shall be included in the discovery.
     *
     * If FALSE, all 'tests' directories are excluded from the search. If NULL,
     * it will be determined by the 'extension_discovery_scan_tests' setting.
     */
    private ?bool $includeTestDrivers = NULL;
    
    /**
     * Constructs a new instance.
     *
     * @param string $root
     *   The app root.
     * @param string $type
     *   The extension type.
     * @param \Drupal\Core\Cache\CacheBackendInterface $cache
     *   The cache.
     */
    public function __construct($root, $type, CacheBackendInterface $cache) {
        $this->root = $root;
        $this->type = $type;
        $this->cache = $cache;
    }
    
    /**
     * Determines whether test drivers shall be included in the discovery.
     *
     * @param bool|null $includeTestDrivers
     *   Whether to include test extensions. If FALSE, all 'tests' directories
     *   are excluded in the search. If NULL, it will be determined by the
     *   'extension_discovery_scan_tests' setting.
     *
     * @return $this
     */
    public function includeTestDrivers(?bool $includeTestDrivers) : static {
        $this->includeTestDrivers = $includeTestDrivers;
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getExtensionDiscovery() {
        return new ExtensionDiscovery($this->root, FALSE);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function doScanExtensions() {
        return $this->getExtensionDiscovery()
            ->scan('module', $this->includeTestDrivers);
    }
    
    /**
     * {@inheritdoc}
     */
    protected function doList() : array {
        // Determine the modules that contain at least one installable database
        // driver.
        $discoveredModules = $this->doScanExtensions();
        $drivers = [];
        foreach ($discoveredModules as $module) {
            $moduleDriverDirectory = $this->root . DIRECTORY_SEPARATOR . $module->getPath() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'Driver' . DIRECTORY_SEPARATOR . 'Database';
            if (is_dir($moduleDriverDirectory)) {
                // Use directory iterator to avoid services.
                $directoryIterator = new \DirectoryIterator($moduleDriverDirectory);
                foreach ($directoryIterator as $fileInfo) {
                    if ($fileInfo->isDir() && !$fileInfo->isDot() && file_exists($moduleDriverDirectory . DIRECTORY_SEPARATOR . $fileInfo->getFilename() . DIRECTORY_SEPARATOR . 'Install' . DIRECTORY_SEPARATOR . 'Tasks.php')) {
                        $databaseDriver = new DatabaseDriver($this->root, $module, $fileInfo->getFilename(), $discoveredModules);
                        $drivers[$databaseDriver->getName()] = $databaseDriver;
                    }
                }
            }
        }
        return $drivers;
    }
    
    /**
     * Returns the list of installable database drivers.
     *
     * @return \Drupal\Core\Extension\DatabaseDriver[]
     *   An array of installable database driver extension objects.
     */
    public function getInstallableList() : array {
        $installableDrivers = [];
        foreach ($this->getList() as $name => $driver) {
            if ($driver->getInstallTasks()
                ->installable()) {
                $installableDrivers[$name] = $driver;
            }
        }
        // Usability: unconditionally put core MySQL driver on top.
        if (isset($installableDrivers[static::CORE_MYSQL_DRIVER_NAMESPACE])) {
            $mysqlDriver = $installableDrivers[static::CORE_MYSQL_DRIVER_NAMESPACE];
            unset($installableDrivers[static::CORE_MYSQL_DRIVER_NAMESPACE]);
            $installableDrivers = [
                static::CORE_MYSQL_DRIVER_NAMESPACE => $mysqlDriver,
            ] + $installableDrivers;
        }
        return $installableDrivers;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getName($extension_name) {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    public function get($extension_name) {
        if (!str_contains($extension_name, "\\")) {
            throw new \RuntimeException("Passing a database driver name '{$extension_name}' to " . __METHOD__ . '() is not supported. Pass a database driver namespace instead. See https://www.drupal.org/node/3258175');
        }
        return parent::get($extension_name);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getExtensionInfo($extension_name) {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getAllAvailableInfo() {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    protected function getInstalledExtensionNames() {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getAllInstalledInfo() {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    protected function recalculateInfo() {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPathNames() {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    protected function recalculatePathNames() {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    public function setPathname($extension_name, $pathname) {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPathname($extension_name) {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    public function getPath($extension_name) {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    protected function createExtensionInfo(Extension $extension) {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    public function checkIncompatibility($name) {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }
    
    /**
     * {@inheritdoc}
     */
    public static function sortByName(Extension $a, Extension $b) : int {
        throw new \LogicException(__METHOD__ . '() is not implemented');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
DatabaseDriverList::$includeTestDrivers private property Determines whether test drivers shall be included in the discovery.
DatabaseDriverList::checkIncompatibility public function Tests the compatibility of an extension. Overrides ExtensionList::checkIncompatibility
DatabaseDriverList::CORE_MYSQL_DRIVER_NAMESPACE protected constant The namespace of core's MySql database driver.
DatabaseDriverList::createExtensionInfo protected function Creates the info value for an extension object. Overrides ExtensionList::createExtensionInfo
DatabaseDriverList::doList protected function Builds the list of extensions. Overrides ExtensionList::doList
DatabaseDriverList::doScanExtensions protected function Scans the available extensions. Overrides ExtensionList::doScanExtensions
DatabaseDriverList::get public function Returns a single extension. Overrides ExtensionList::get
DatabaseDriverList::getAllAvailableInfo public function Returns an array of info files information of available extensions. Overrides ExtensionList::getAllAvailableInfo
DatabaseDriverList::getAllInstalledInfo public function Returns an array of info files information of installed extensions. Overrides ExtensionList::getAllInstalledInfo
DatabaseDriverList::getExtensionDiscovery protected function Returns the extension discovery. Overrides ExtensionList::getExtensionDiscovery
DatabaseDriverList::getExtensionInfo public function Returns information about a specified extension. Overrides ExtensionList::getExtensionInfo
DatabaseDriverList::getInstallableList public function Returns the list of installable database drivers.
DatabaseDriverList::getInstalledExtensionNames protected function Returns a list of machine names of installed extensions. Overrides ExtensionList::getInstalledExtensionNames
DatabaseDriverList::getName public function Returns the human-readable name of the extension. Overrides ExtensionList::getName
DatabaseDriverList::getPath public function Gets the path to an extension of a specific type (module, theme, etc.). Overrides ExtensionList::getPath
DatabaseDriverList::getPathname public function Gets the info file path for an extension. Overrides ExtensionList::getPathname
DatabaseDriverList::getPathNames public function Returns a list of extension file paths keyed by machine name. Overrides ExtensionList::getPathNames
DatabaseDriverList::includeTestDrivers public function Determines whether test drivers shall be included in the discovery.
DatabaseDriverList::recalculateInfo protected function Generates the information from .info.yml files for extensions of this type. Overrides ExtensionList::recalculateInfo
DatabaseDriverList::recalculatePathNames protected function Generates a sorted list of .info.yml file locations for all extensions. Overrides ExtensionList::recalculatePathNames
DatabaseDriverList::setPathname public function Sets the pathname for an extension. Overrides ExtensionList::setPathname
DatabaseDriverList::sortByName public static function Array sorting callback; sorts extensions by their name. Overrides ExtensionList::sortByName
DatabaseDriverList::__construct public function Constructs a new instance. Overrides ExtensionList::__construct
ExtensionList::$addedPathNames protected property A list of extension folder names directly added in code (not discovered).
ExtensionList::$cache protected property The cache.
ExtensionList::$defaults protected property Default values to be merged into *.info.yml file arrays. 4
ExtensionList::$extensionInfo protected property Static caching for extension info.
ExtensionList::$extensions protected property The cached extensions.
ExtensionList::$infoParser protected property The info parser.
ExtensionList::$installProfile protected property The install profile used by the site.
ExtensionList::$moduleHandler protected property The module handler.
ExtensionList::$pathNames protected property A list of extension folder names keyed by extension name.
ExtensionList::$root protected property The app root.
ExtensionList::$state protected property The state store.
ExtensionList::$type protected property The type of the extension.
ExtensionList::exists public function Determines if an extension exists in the filesystem.
ExtensionList::getInfoCacheId protected function Returns the extension info cache ID.
ExtensionList::getList public function Returns all available extensions.
ExtensionList::getListCacheId protected function Returns the extension list cache ID.
ExtensionList::getPathNamesCacheId protected function Returns the extension filenames cache ID.
ExtensionList::reset public function Resets the stored extension list. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.