|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ecotone\SymfonyBundle\DepedencyInjection\Compiler; |
| 4 | + |
| 5 | +use Ecotone\Messaging\Config\MessagingSystemConfiguration; |
| 6 | +use Ecotone\Messaging\Config\ServiceConfiguration; |
| 7 | +use Ecotone\Messaging\ConfigurationVariableService; |
| 8 | +use Ecotone\Messaging\Gateway\ConsoleCommandRunner; |
| 9 | +use Ecotone\Messaging\Handler\Recoverability\RetryTemplateBuilder; |
| 10 | +use Ecotone\SymfonyBundle\DepedencyInjection\MessagingEntrypointCommand; |
| 11 | +use Ecotone\SymfonyBundle\EcotoneSymfonyBundle; |
| 12 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 13 | +use Symfony\Component\DependencyInjection\Container; |
| 14 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 15 | +use Symfony\Component\DependencyInjection\Definition; |
| 16 | +use Symfony\Component\DependencyInjection\Reference; |
| 17 | + |
| 18 | +class EcotoneCompilerPass implements CompilerPassInterface |
| 19 | +{ |
| 20 | + public const FRAMEWORK_NAMESPACE = 'Ecotone'; |
| 21 | + public const SERVICE_NAME = 'ecotone.service_name'; |
| 22 | + public const WORKING_NAMESPACES_CONFIG = 'ecotone.namespaces'; |
| 23 | + public const FAIL_FAST_CONFIG = 'ecotone.fail_fast'; |
| 24 | + public const LOAD_SRC = 'ecotone.load_src'; |
| 25 | + public const DEFAULT_SERIALIZATION_MEDIA_TYPE = 'ecotone.serializationMediaType'; |
| 26 | + public const ERROR_CHANNEL = 'ecotone.errorChannel'; |
| 27 | + public const DEFAULT_MEMORY_LIMIT = 'ecotone.defaultMemoryLimit'; |
| 28 | + public const DEFAULT_CONNECTION_EXCEPTION_RETRY = 'ecotone.defaultChannelPollRetry'; |
| 29 | + public const SRC_CATALOG = 'src'; |
| 30 | + public const CACHE_DIRECTORY_SUFFIX = DIRECTORY_SEPARATOR . 'ecotone'; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param Container $container |
| 34 | + * |
| 35 | + * @return bool|string |
| 36 | + */ |
| 37 | + public static function getRootProjectPath(Container $container) |
| 38 | + { |
| 39 | + return realpath(($container->hasParameter('kernel.project_dir') ? $container->getParameter('kernel.project_dir') : $container->getParameter('kernel.root_dir') . '/..')); |
| 40 | + } |
| 41 | + |
| 42 | + public function process(ContainerBuilder $container) |
| 43 | + { |
| 44 | + $ecotoneCacheDirectory = $container->getParameter('kernel.cache_dir') . self::CACHE_DIRECTORY_SUFFIX; |
| 45 | + $applicationConfiguration = ServiceConfiguration::createWithDefaults() |
| 46 | + ->withEnvironment($container->getParameter('kernel.environment')) |
| 47 | + ->withFailFast($container->getParameter('kernel.environment') === 'prod' ? false : $container->getParameter(self::FAIL_FAST_CONFIG)) |
| 48 | + ->withLoadCatalog($container->getParameter(self::LOAD_SRC) ? 'src' : '') |
| 49 | + ->withNamespaces( |
| 50 | + array_merge( |
| 51 | + $container->getParameter(self::WORKING_NAMESPACES_CONFIG), |
| 52 | + [self::FRAMEWORK_NAMESPACE] |
| 53 | + ) |
| 54 | + ) |
| 55 | + ->withCacheDirectoryPath($ecotoneCacheDirectory); |
| 56 | + |
| 57 | + if ($container->getParameter(self::SERVICE_NAME)) { |
| 58 | + $applicationConfiguration = $applicationConfiguration |
| 59 | + ->withServiceName($container->getParameter(self::SERVICE_NAME)); |
| 60 | + } |
| 61 | + |
| 62 | + if ($container->getParameter(self::DEFAULT_SERIALIZATION_MEDIA_TYPE)) { |
| 63 | + $applicationConfiguration = $applicationConfiguration |
| 64 | + ->withDefaultSerializationMediaType($container->getParameter(self::DEFAULT_SERIALIZATION_MEDIA_TYPE)); |
| 65 | + } |
| 66 | + if ($container->getParameter(self::DEFAULT_MEMORY_LIMIT)) { |
| 67 | + $applicationConfiguration = $applicationConfiguration |
| 68 | + ->withConsumerMemoryLimit($container->getParameter(self::DEFAULT_MEMORY_LIMIT)); |
| 69 | + } |
| 70 | + if ($container->getParameter(self::DEFAULT_CONNECTION_EXCEPTION_RETRY)) { |
| 71 | + $retryTemplate = $container->getParameter(self::DEFAULT_CONNECTION_EXCEPTION_RETRY); |
| 72 | + $applicationConfiguration = $applicationConfiguration |
| 73 | + ->withConnectionRetryTemplate( |
| 74 | + RetryTemplateBuilder::exponentialBackoffWithMaxDelay( |
| 75 | + $retryTemplate['initialDelay'], |
| 76 | + $retryTemplate['maxAttempts'], |
| 77 | + $retryTemplate['multiplier'] |
| 78 | + ) |
| 79 | + ); |
| 80 | + } |
| 81 | + if ($container->getParameter(self::ERROR_CHANNEL)) { |
| 82 | + $applicationConfiguration = $applicationConfiguration |
| 83 | + ->withDefaultErrorChannel($container->getParameter(self::ERROR_CHANNEL)); |
| 84 | + } |
| 85 | + |
| 86 | + $configurationVariableService = new SymfonyConfigurationVariableService($container); |
| 87 | + $messagingConfiguration = MessagingSystemConfiguration::prepare( |
| 88 | + self::getRootProjectPath($container), |
| 89 | + new SymfonyReferenceTypeResolver($container), |
| 90 | + $configurationVariableService, |
| 91 | + $applicationConfiguration, |
| 92 | + false |
| 93 | + ); |
| 94 | + |
| 95 | + $definition = new Definition(); |
| 96 | + $definition->setClass(SymfonyConfigurationVariableService::class); |
| 97 | + $definition->setPublic(true); |
| 98 | + $definition->addArgument(new Reference('service_container')); |
| 99 | + $container->setDefinition(ConfigurationVariableService::REFERENCE_NAME, $definition); |
| 100 | + |
| 101 | + $definition = new $definition(); |
| 102 | + $definition->setClass(CacheCleaner::class); |
| 103 | + $definition->setPublic(true); |
| 104 | + $definition->addTag('kernel.cache_clearer'); |
| 105 | + $container->setDefinition(CacheCleaner::class, $definition); |
| 106 | + |
| 107 | + $definition = new Definition(); |
| 108 | + $definition->setClass(SymfonyReferenceSearchService::class); |
| 109 | + $definition->setPublic(true); |
| 110 | + $definition->addArgument(new Reference('service_container')); |
| 111 | + $container->setDefinition('symfonyReferenceSearchService', $definition); |
| 112 | + |
| 113 | + foreach ($messagingConfiguration->getRegisteredGateways() as $gatewayProxyBuilder) { |
| 114 | + $definition = new Definition(); |
| 115 | + $definition->setFactory([ProxyGenerator::class, 'createFor']); |
| 116 | + $definition->setClass($gatewayProxyBuilder->getInterfaceName()); |
| 117 | + $definition->addArgument($gatewayProxyBuilder->getReferenceName()); |
| 118 | + $definition->addArgument(new Reference('service_container')); |
| 119 | + $definition->addArgument($gatewayProxyBuilder->getInterfaceName()); |
| 120 | + $definition->addArgument($ecotoneCacheDirectory); |
| 121 | + $definition->addArgument($container->getParameter(self::FAIL_FAST_CONFIG)); |
| 122 | + $definition->setPublic(true); |
| 123 | + |
| 124 | + $container->setDefinition($gatewayProxyBuilder->getReferenceName(), $definition); |
| 125 | + } |
| 126 | + |
| 127 | + foreach ($messagingConfiguration->getRequiredReferences() as $requiredReference) { |
| 128 | + $alias = $container->setAlias($requiredReference . '-proxy', $requiredReference); |
| 129 | + |
| 130 | + if ($alias) { |
| 131 | + $alias->setPublic(true); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + foreach ($messagingConfiguration->getOptionalReferences() as $requiredReference) { |
| 136 | + if ($container->has($requiredReference)) { |
| 137 | + $alias = $container->setAlias($requiredReference . '-proxy', $requiredReference); |
| 138 | + |
| 139 | + if ($alias) { |
| 140 | + $alias->setPublic(true); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + foreach ($messagingConfiguration->getRegisteredConsoleCommands() as $oneTimeCommandConfiguration) { |
| 146 | + $definition = new Definition(); |
| 147 | + $definition->setClass(MessagingEntrypointCommand::class); |
| 148 | + $definition->addArgument($oneTimeCommandConfiguration->getName()); |
| 149 | + $definition->addArgument(serialize($oneTimeCommandConfiguration->getParameters())); |
| 150 | + $definition->addArgument(new Reference(ConsoleCommandRunner::class)); |
| 151 | + $definition->addTag('console.command', ['command' => $oneTimeCommandConfiguration->getName()]); |
| 152 | + |
| 153 | + $container->setDefinition($oneTimeCommandConfiguration->getChannelName(), $definition); |
| 154 | + } |
| 155 | + |
| 156 | + $container->setParameter(EcotoneSymfonyBundle::APPLICATION_CONFIGURATION_CONTEXT, serialize($applicationConfiguration)); |
| 157 | + } |
| 158 | +} |
0 commit comments