|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yokai\Batch\Bridge\Symfony\Framework\DependencyInjection; |
| 6 | + |
| 7 | +use Symfony\Component\Config\FileLocator; |
| 8 | +use Symfony\Component\Config\Loader as ConfigLoader; |
| 9 | +use Symfony\Component\Console\Application; |
| 10 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 11 | +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
| 12 | +use Symfony\Component\DependencyInjection\Loader as DependencyInjectionLoader; |
| 13 | +use Symfony\Component\DependencyInjection\Reference; |
| 14 | +use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
| 15 | +use Symfony\Component\Messenger\MessageBusInterface; |
| 16 | +use Symfony\Component\Serializer\SerializerInterface; |
| 17 | +use Symfony\Component\Validator\Validator\ValidatorInterface; |
| 18 | +use Yokai\Batch\Bridge\Doctrine\DBAL\DoctrineDBALJobExecutionStorage; |
| 19 | +use Yokai\Batch\Bridge\Symfony\Serializer\SerializerJobExecutionSerializer; |
| 20 | +use Yokai\Batch\Launcher\JobLauncherInterface; |
| 21 | +use Yokai\Batch\Storage\FilesystemJobExecutionStorage; |
| 22 | +use Yokai\Batch\Storage\JobExecutionStorageInterface; |
| 23 | +use Yokai\Batch\Storage\ListableJobExecutionStorageInterface; |
| 24 | +use Yokai\Batch\Storage\QueryableJobExecutionStorageInterface; |
| 25 | + |
| 26 | +final class YokaiBatchExtension extends Extension |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @inheritDoc |
| 30 | + */ |
| 31 | + public function load(array $configs, ContainerBuilder $container): void |
| 32 | + { |
| 33 | + $configuration = new Configuration(); |
| 34 | + $config = $this->processConfiguration($configuration, $configs); |
| 35 | + |
| 36 | + $loader = $this->getLoader($container); |
| 37 | + $loader->load('global/'); |
| 38 | + $bundles = $container->getParameter('kernel.bundles'); |
| 39 | + |
| 40 | + $bridges = [ |
| 41 | + 'doctrine/orm/' => isset($bundles['DoctrineBundle']), |
| 42 | + 'doctrine/mongodb/' => isset($bundles['DoctrineMongoDBBundle']), |
| 43 | + 'symfony/console/' => class_exists(Application::class), |
| 44 | + 'symfony/messenger/' => class_exists(MessageBusInterface::class), |
| 45 | + 'symfony/serializer/' => interface_exists(SerializerInterface::class), |
| 46 | + 'symfony/validator/' => interface_exists(ValidatorInterface::class), |
| 47 | + ]; |
| 48 | + |
| 49 | + foreach (array_keys(array_filter($bridges)) as $resource) { |
| 50 | + $loader->load($resource); |
| 51 | + } |
| 52 | + |
| 53 | + $this->configureStorage($container, $config['storage']); |
| 54 | + |
| 55 | + $launcher = 'yokai_batch.job_launcher.simple'; |
| 56 | + if (class_exists(MessageBusInterface::class)) { |
| 57 | + $launcher = 'yokai_batch.job_launcher.dispatch_message'; |
| 58 | + } elseif (class_exists(Application::class)) { |
| 59 | + $launcher = 'yokai_batch.job_launcher.run_command'; |
| 60 | + } |
| 61 | + $container->setAlias(JobLauncherInterface::class, $launcher); |
| 62 | + } |
| 63 | + |
| 64 | + private function getLoader(ContainerBuilder $container): ConfigLoader\LoaderInterface |
| 65 | + { |
| 66 | + $locator = new FileLocator(__DIR__ . '/../Resources/services'); |
| 67 | + $resolver = new ConfigLoader\LoaderResolver( |
| 68 | + [ |
| 69 | + new DependencyInjectionLoader\XmlFileLoader($container, $locator), |
| 70 | + new DependencyInjectionLoader\DirectoryLoader($container, $locator), |
| 71 | + ] |
| 72 | + ); |
| 73 | + |
| 74 | + return new ConfigLoader\DelegatingLoader($resolver); |
| 75 | + } |
| 76 | + |
| 77 | + private function configureStorage(ContainerBuilder $container, array $config): void |
| 78 | + { |
| 79 | + if (isset($config['service'])) { |
| 80 | + $defaultStorage = $config['service']; |
| 81 | + } elseif (isset($config['dbal'])) { |
| 82 | + $container |
| 83 | + ->register('yokai_batch.storage.dbal', DoctrineDBALJobExecutionStorage::class) |
| 84 | + ->setArguments( |
| 85 | + [ |
| 86 | + new Reference("doctrine.dbal.{$config['dbal']['connection']}_connection"), |
| 87 | + $config['dbal']['options'], |
| 88 | + ] |
| 89 | + ) |
| 90 | + ; |
| 91 | + |
| 92 | + $defaultStorage = 'yokai_batch.storage.dbal'; |
| 93 | + } elseif (isset($config['filesystem'])) { |
| 94 | + $serializer = $config['filesystem']['serializer']; |
| 95 | + $format = $serializer['format']; |
| 96 | + |
| 97 | + if (isset($serializer['service'])) { |
| 98 | + $serializerId = $serializer['service']; |
| 99 | + } elseif (isset($serializer['symfony'])) { |
| 100 | + if (!interface_exists(SerializerInterface::class)) { |
| 101 | + throw new \LogicException(); //todo |
| 102 | + } |
| 103 | + |
| 104 | + $serializerId = 'yokai_batch.job_execution_serializer.filesystem_storage'; |
| 105 | + $container |
| 106 | + ->register($serializerId, SerializerJobExecutionSerializer::class) |
| 107 | + ->setArguments( |
| 108 | + [ |
| 109 | + new Reference(SerializerInterface::class), |
| 110 | + $format, |
| 111 | + $serializer['symfony']['context']['common'], |
| 112 | + $serializer['symfony']['context']['serialize'], |
| 113 | + $serializer['symfony']['context']['deserialize'], |
| 114 | + ] |
| 115 | + ) |
| 116 | + ; |
| 117 | + } else { |
| 118 | + throw new \LogicException(); //todo |
| 119 | + } |
| 120 | + |
| 121 | + $container |
| 122 | + ->register('yokai_batch.storage.filesystem', FilesystemJobExecutionStorage::class) |
| 123 | + ->setArguments( |
| 124 | + [ |
| 125 | + new Reference($serializerId), |
| 126 | + $config['filesystem']['dir'], |
| 127 | + $format, |
| 128 | + ] |
| 129 | + ) |
| 130 | + ; |
| 131 | + |
| 132 | + $defaultStorage = 'yokai_batch.storage.filesystem'; |
| 133 | + } else { |
| 134 | + throw new \LogicException();//todo |
| 135 | + } |
| 136 | + |
| 137 | + try { |
| 138 | + $defaultStorageDefinition = $container->getDefinition($defaultStorage); |
| 139 | + } catch (ServiceNotFoundException $exception) { |
| 140 | + throw new \LogicException( |
| 141 | + sprintf('Configured default job execution storage service "%s" does not exists.', $defaultStorage), |
| 142 | + 0, |
| 143 | + $exception |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + $interfaces = [ |
| 148 | + JobExecutionStorageInterface::class => true, |
| 149 | + ListableJobExecutionStorageInterface::class => false, |
| 150 | + QueryableJobExecutionStorageInterface::class => false, |
| 151 | + ]; |
| 152 | + $defaultStorageClass = $defaultStorageDefinition->getClass(); |
| 153 | + foreach ($interfaces as $interface => $required) { |
| 154 | + if (!is_a($defaultStorageClass, $interface, true)) { |
| 155 | + if ($required) { |
| 156 | + throw new \LogicException();//todo |
| 157 | + } |
| 158 | + continue; |
| 159 | + } |
| 160 | + $container |
| 161 | + ->setAlias($interface, $defaultStorage) |
| 162 | + ->setPublic(true) |
| 163 | + ; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments