function BackendCompilerPass::process

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass::process()
  2. 8.9.x core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass::process()
  3. 10 core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass::process()

File

core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php, line 39

Class

BackendCompilerPass
Defines a compiler pass to allow automatic override per backend.

Namespace

Drupal\Core\DependencyInjection\Compiler

Code

public function process(ContainerBuilder $container) : void {
    $driver_backend = NULL;
    if ($container->hasParameter('default_backend')) {
        $default_backend = $container->getParameter('default_backend');
        // Opt out from the default backend.
        if (!$default_backend) {
            return;
        }
    }
    else {
        try {
            $driver_backend = $container->get('database')
                ->driver();
            $default_backend = $container->get('database')
                ->databaseType();
            $container->set('database', NULL);
        } catch (\Exception $e) {
            // If Drupal is not installed or a test doesn't define database there
            // is nothing to override.
            return;
        }
    }
    foreach ($container->findTaggedServiceIds('backend_overridable') as $id => $attributes) {
        // If the service is already an alias it is not the original backend, so
        // we don't want to fallback to other storages any longer.
        if ($container->hasAlias($id)) {
            continue;
        }
        if ($container->hasDefinition("{$driver_backend}.{$id}") || $container->hasAlias("{$driver_backend}.{$id}")) {
            $container->setAlias($id, new Alias("{$driver_backend}.{$id}"));
        }
        elseif ($container->hasDefinition("{$default_backend}.{$id}") || $container->hasAlias("{$default_backend}.{$id}")) {
            $container->setAlias($id, new Alias("{$default_backend}.{$id}"));
        }
    }
}

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