|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AsyncAws\Symfony\Bundle\DependencyInjection; |
| 6 | + |
| 7 | +use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
| 8 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 9 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 10 | +use Symfony\Component\DependencyInjection\Definition; |
| 11 | +use Symfony\Component\DependencyInjection\Reference; |
| 12 | +use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
| 13 | + |
| 14 | +class AsyncAwsExtension extends Extension |
| 15 | +{ |
| 16 | + public function load(array $configs, ContainerBuilder $container) |
| 17 | + { |
| 18 | + $configuration = new Configuration(); |
| 19 | + $config = $this->processConfiguration($configuration, $configs); |
| 20 | + |
| 21 | + $usedServices = $this->registerConfiguredServices($container, $config); |
| 22 | + $usedServices = $this->registerInstalledServices($container, $config, $usedServices); |
| 23 | + $this->autowireServices($container, $usedServices); |
| 24 | + } |
| 25 | + |
| 26 | + private function registerConfiguredServices(ContainerBuilder $container, array $config): array |
| 27 | + { |
| 28 | + $availableServices = AwsPackagesProvider::getAllServices(); |
| 29 | + $usedServices = []; |
| 30 | + $defaultConfig = $config; |
| 31 | + unset($defaultConfig['services']); |
| 32 | + |
| 33 | + foreach ($config['services'] as $name => $data) { |
| 34 | + $client = $availableServices[$data['type']]['client']; |
| 35 | + if (!class_exists($client)) { |
| 36 | + throw new InvalidConfigurationException(sprintf('You have configured "async_aws.%s" but the "%s" package is not installed. Try running "composer require %s"', $name, $name, $availableServices[$name]['package'])); |
| 37 | + } |
| 38 | + |
| 39 | + $config = array_merge($defaultConfig, $data); |
| 40 | + if ($config['register_service']) { |
| 41 | + $usedServices[$name] = $client; |
| 42 | + $this->addServiceDefinition($container, $name, $config, $client); |
| 43 | + } else { |
| 44 | + $usedServices[$name] = null; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return $usedServices; |
| 49 | + } |
| 50 | + |
| 51 | + private function registerInstalledServices(ContainerBuilder $container, array $config, array $usedServices): array |
| 52 | + { |
| 53 | + if (!$config['register_service']) { |
| 54 | + return $usedServices; |
| 55 | + } |
| 56 | + |
| 57 | + unset($config['services']); |
| 58 | + $availableServices = AwsPackagesProvider::getAllServices(); |
| 59 | + foreach ($availableServices as $name => $data) { |
| 60 | + if (\array_key_exists($name, $usedServices)) { |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + $client = $data['client']; |
| 65 | + if (!class_exists($client)) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + $usedServices[$name] = $client; |
| 70 | + $this->addServiceDefinition($container, $name, $config, $client); |
| 71 | + } |
| 72 | + |
| 73 | + return $usedServices; |
| 74 | + } |
| 75 | + |
| 76 | + private function addServiceDefinition(ContainerBuilder $container, string $name, array $config, string $clientClass): void |
| 77 | + { |
| 78 | + if (\array_key_exists('logger', $config)) { |
| 79 | + $logger = $config['logger'] ? new Reference($config['logger']) : null; |
| 80 | + } else { |
| 81 | + // Use default Symfony logger unless explicitly set to null. |
| 82 | + $logger = new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE); |
| 83 | + } |
| 84 | + |
| 85 | + $definition = new Definition($clientClass); |
| 86 | + $definition->addArgument($config['config']); |
| 87 | + $definition->addArgument(isset($config['credential_provider']) ? new Reference($config['credential_provider']) : null); |
| 88 | + $definition->addArgument(isset($config['http_client']) ? new Reference($config['http_client']) : null); |
| 89 | + $definition->addArgument($logger); |
| 90 | + $container->setDefinition(sprintf('async_aws.service.%s', $name), $definition); |
| 91 | + } |
| 92 | + |
| 93 | + private function autowireServices(ContainerBuilder $container, array $usedServices): void |
| 94 | + { |
| 95 | + $awsServices = AwsPackagesProvider::getAllServices(); |
| 96 | + foreach ($usedServices as $name => $client) { |
| 97 | + if (null === $client) { |
| 98 | + // This client is disabled. |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + $serviceId = sprintf('async_aws.service.%s', $name); |
| 103 | + if (isset($awsServices[$name])) { |
| 104 | + $container->setAlias($client, $serviceId); |
| 105 | + |
| 106 | + continue; |
| 107 | + } |
| 108 | + |
| 109 | + // Assert: Custom name |
| 110 | + $container->registerAliasForArgument($serviceId, $client, $name); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments