3 namespace Drupal\Core\DependencyInjection\Compiler;
5 use Symfony\Component\DependencyInjection\Alias;
6 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7 use Symfony\Component\DependencyInjection\ContainerBuilder;
10 * Defines a compiler pass to allow automatic override per backend.
12 * A module developer has to tag his backend service with "backend_overridable":
17 * - { name: backend_overridable }
20 * As a site admin you set the 'default_backend' in your services.yml file:
23 * default_backend: sqlite
26 * As a developer for alternative storage engines you register a service with
27 * $yourbackend.$original_service:
30 * sqlite.custom_service:
34 class BackendCompilerPass implements CompilerPassInterface {
39 public function process(ContainerBuilder $container) {
40 if ($container->hasParameter('default_backend')) {
41 $default_backend = $container->getParameter('default_backend');
42 // Opt out from the default backend.
43 if (!$default_backend) {
49 $default_backend = $container->get('database')->driver();
50 $container->set('database', NULL);
52 catch (\Exception $e) {
53 // If Drupal is not installed or a test doesn't define database there
54 // is nothing to override.
59 foreach ($container->findTaggedServiceIds('backend_overridable') as $id => $attributes) {
60 // If the service is already an alias it is not the original backend, so
61 // we don't want to fallback to other storages any longer.
62 if ($container->hasAlias($id)) {
65 if ($container->hasDefinition("$default_backend.$id") || $container->hasAlias("$default_backend.$id")) {
66 $container->setAlias($id, new Alias("$default_backend.$id"));