function Database::findDriverAutoloadDirectory

Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::findDriverAutoloadDirectory()
  2. 8.9.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::findDriverAutoloadDirectory()

Finds the directory to add to the autoloader for the driver's namespace.

For Drupal sites that manage their codebase with Composer, the package that provides the database driver should add the driver's namespace to Composer's autoloader. However, to support sites that add Drupal modules without Composer, and because the database connection must be established before Drupal adds the module's entire namespace to the autoloader, the database connection info array can include an "autoload" key containing the autoload directory for the driver's namespace. For requests that connect to the database via a connection info array, the value of the "autoload" key is automatically added to the autoloader.

This method can be called to find the default value of that key when the database connection info array isn't available. This includes:

  • Console commands and test runners that connect to a database specified by a database URL rather than a connection info array.
  • During installation, prior to the connection info array being written to settings.php.

This method returns the directory that must be added to the autoloader for the given namespace.

  • If the namespace is a sub-namespace of a Drupal module, then this method returns the autoload directory for that namespace, allowing Drupal modules containing database drivers to be added to a Drupal website without Composer.
  • If the namespace is a sub-namespace of Drupal\Core or Drupal\Driver, then this method returns FALSE, because Drupal core's autoloader already includes these namespaces, so no additional autoload directory is required for any code within them.
  • If the namespace is anything else, then this method returns FALSE, because neither drupal_get_database_types() nor static::convertDbUrlToConnectionInfo() support that anyway. One can manually edit the connection info array in settings.php to reference any arbitrary namespace, but requests using that would use the corresponding 'autoload' key in that connection info rather than calling this method.

Parameters

string $namespace: The database driver's namespace.

string $root: The root directory of the Drupal installation.

bool|null $include_test_drivers: (optional) Whether to include test extensions. If FALSE, all 'tests' directories are excluded in the search. When NULL will be determined by the extension_discovery_scan_tests setting.

Return value

string|false The PSR-4 directory to add to the autoloader for the namespace if the namespace is a sub-namespace of a Drupal module. FALSE otherwise, as explained above.

Throws

\RuntimeException Exception thrown when a module provided database driver does not exist.

Deprecated

in drupal:10.2.0 and is removed from drupal:11.0.0. Use DatabaseDriverList::getList() instead.

See also

https://www.drupal.org/node/3258175

2 calls to Database::findDriverAutoloadDirectory()
DatabaseTest::testFindDriverAutoloadDirectory in core/tests/Drupal/Tests/Core/Database/DatabaseTest.php
@covers ::findDriverAutoloadDirectory @dataProvider providerFindDriverAutoloadDirectory @group legacy
DatabaseTest::testFindDriverAutoloadDirectoryException in core/tests/Drupal/Tests/Core/Database/DatabaseTest.php
@covers ::findDriverAutoloadDirectory @dataProvider providerFindDriverAutoloadDirectoryException @group legacy

File

core/lib/Drupal/Core/Database/Database.php, line 685

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static function findDriverAutoloadDirectory($namespace, $root, ?bool $include_test_drivers = NULL) {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use DatabaseDriverList::getList() instead. See https://www.drupal.org/node/3258175', E_USER_DEPRECATED);
    $autoload_info = static::getDriverList()->includeTestDrivers($include_test_drivers)
        ->get($namespace)
        ->getAutoloadInfo();
    return $autoload_info['autoload'] ?? FALSE;
}

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