class BackendCompilerPass

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass
  2. 10 core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass
  3. 11.x core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass

Defines a compiler pass to allow automatic override per backend.

A module developer has to tag a backend service with "backend_overridable":


custom_service:
  class: ...
  tags:
    - { name: backend_overridable }

As a site admin you set the 'default_backend' in your services.yml file:

parameters:
default_backend:
sqlite;

As a developer for alternative storage engines you register a service with $yourbackend.$original_service:


sqlite.custom_service:
  class: ...

Hierarchy

  • class \Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass implements \Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface

Expanded class hierarchy of BackendCompilerPass

2 files declare their use of BackendCompilerPass
BackendCompilerPassTest.php in core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php
Contains \Drupal\Tests\Core\DependencyInjection\Compiler\BackendCompilerPassTest.
CoreServiceProvider.php in core/lib/Drupal/Core/CoreServiceProvider.php

File

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

Namespace

Drupal\Core\DependencyInjection\Compiler
View source
class BackendCompilerPass implements CompilerPassInterface {
    
    /**
     * {@inheritdoc}
     */
    public function process(ContainerBuilder $container) {
        $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}"));
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
BackendCompilerPass::process public function

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