|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Enqueue\Monitoring\Symfony\DependencyInjection; |
| 4 | + |
| 5 | +use Enqueue\Monitoring\ClientMonitoringExtension; |
| 6 | +use Enqueue\Monitoring\ConsumerMonitoringExtension; |
| 7 | +use Enqueue\Monitoring\GenericStatsStorageFactory; |
| 8 | +use Enqueue\Monitoring\Resources; |
| 9 | +use Enqueue\Monitoring\StatsStorage; |
| 10 | +use Enqueue\Monitoring\StatsStorageFactory; |
| 11 | +use Enqueue\Symfony\DiUtils; |
| 12 | +use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
| 13 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 14 | +use Symfony\Component\DependencyInjection\Reference; |
| 15 | + |
| 16 | +/** |
| 17 | + * @internal |
| 18 | + */ |
| 19 | +final class MonitoringFactory |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var DiUtils |
| 23 | + */ |
| 24 | + private $diUtils; |
| 25 | + |
| 26 | + public function __construct(string $name) |
| 27 | + { |
| 28 | + if (empty($name)) { |
| 29 | + throw new \InvalidArgumentException('The name could not be empty.'); |
| 30 | + } |
| 31 | + |
| 32 | + $this->diUtils = DiUtils::create('monitoring', $name); |
| 33 | + } |
| 34 | + |
| 35 | + public static function getConfiguration(string $name = 'monitoring'): ArrayNodeDefinition |
| 36 | + { |
| 37 | + $builder = new ArrayNodeDefinition($name); |
| 38 | + |
| 39 | + $builder |
| 40 | + ->info(sprintf('The "%s" option could accept a string DSN, an array with DSN key, or null. It accept extra options. To find out what option you can set, look at stats storage constructor doc block.', $name)) |
| 41 | + ->beforeNormalization() |
| 42 | + ->always(function ($v) { |
| 43 | + if (is_array($v)) { |
| 44 | + if (isset($v['storage_factory_class']) && isset($v['storage_factory_service'])) { |
| 45 | + throw new \LogicException('Both options storage_factory_class and storage_factory_service are set. Please choose one.'); |
| 46 | + } |
| 47 | + |
| 48 | + return $v; |
| 49 | + } |
| 50 | + |
| 51 | + if (is_string($v)) { |
| 52 | + return ['dsn' => $v]; |
| 53 | + } |
| 54 | + |
| 55 | + return $v; |
| 56 | + }) |
| 57 | + ->end() |
| 58 | + ->ignoreExtraKeys(false) |
| 59 | + ->children() |
| 60 | + ->scalarNode('dsn') |
| 61 | + ->cannotBeEmpty() |
| 62 | + ->isRequired() |
| 63 | + ->info(sprintf('The stats storage DSN. These schemes are supported: "%s".', implode('", "', array_keys(Resources::getKnownSchemes())))) |
| 64 | + ->end() |
| 65 | + ->scalarNode('storage_factory_service') |
| 66 | + ->info(sprintf('The factory class should implement "%s" interface', StatsStorageFactory::class)) |
| 67 | + ->end() |
| 68 | + ->scalarNode('storage_factory_class') |
| 69 | + ->info(sprintf('The factory service should be a class that implements "%s" interface', StatsStorageFactory::class)) |
| 70 | + ->end() |
| 71 | + ->end() |
| 72 | + ; |
| 73 | + |
| 74 | + return $builder; |
| 75 | + } |
| 76 | + |
| 77 | + public function buildStorage(ContainerBuilder $container, array $config): void |
| 78 | + { |
| 79 | + $storageId = $this->diUtils->format('storage'); |
| 80 | + $storageFactoryId = $this->diUtils->format('storage.factory'); |
| 81 | + |
| 82 | + if (isset($config['storage_factory_service'])) { |
| 83 | + $container->setAlias($storageFactoryId, $config['storage_factory_service']); |
| 84 | + } elseif (isset($config['storage_factory_class'])) { |
| 85 | + $container->register($storageFactoryId, $config['storage_factory_class']); |
| 86 | + } else { |
| 87 | + $container->register($storageFactoryId, GenericStatsStorageFactory::class); |
| 88 | + } |
| 89 | + |
| 90 | + unset($config['storage_factory_service'], $config['storage_factory_class']); |
| 91 | + |
| 92 | + $container->register($storageId, StatsStorage::class) |
| 93 | + ->setFactory([new Reference($storageFactoryId), 'create']) |
| 94 | + ->addArgument($config) |
| 95 | + ; |
| 96 | + } |
| 97 | + |
| 98 | + public function buildClientExtension(ContainerBuilder $container, array $config): void |
| 99 | + { |
| 100 | + $container->register($this->diUtils->format('client_extension'), ClientMonitoringExtension::class) |
| 101 | + ->addArgument($this->diUtils->reference('storage')) |
| 102 | + ->addArgument(new Reference('logger')) |
| 103 | + ->addTag('enqueue.client_extension', ['client' => $this->diUtils->getConfigName()]) |
| 104 | + ; |
| 105 | + } |
| 106 | + |
| 107 | + public function buildConsumerExtension(ContainerBuilder $container, array $config): void |
| 108 | + { |
| 109 | + $container->register($this->diUtils->format('consumer_extension'), ConsumerMonitoringExtension::class) |
| 110 | + ->addArgument($this->diUtils->reference('storage')) |
| 111 | + ->addTag('enqueue.consumption_extension', ['client' => $this->diUtils->getConfigName()]) |
| 112 | + ->addTag('enqueue.transport.consumption_extension', ['transport' => $this->diUtils->getConfigName()]) |
| 113 | + ; |
| 114 | + } |
| 115 | +} |
0 commit comments