-
Notifications
You must be signed in to change notification settings - Fork 0
Connection pool #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.0.x
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,8 +10,10 @@ | |
| namespace EventBand\Bundle\DependencyInjection; | ||
|
|
||
| use EventBand\Bundle\DependencyInjection\Compiler\SerializerPass; | ||
| use Symfony\Component\DependencyInjection\Alias; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\Config\FileLocator; | ||
| use Symfony\Component\DependencyInjection\Definition; | ||
| use Symfony\Component\DependencyInjection\DefinitionDecorator; | ||
| use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
|
|
@@ -58,7 +60,7 @@ private function loadAmqpTransport(array $config, ContainerBuilder $container) | |
| $this->loader->load('transport/amqp/tracer.xml'); | ||
| } | ||
|
|
||
| $camelizeKey = function (array $config) { | ||
| $camelizeKey = function (array $config) use (&$camelizeKey) { | ||
| $camelized = []; | ||
| foreach ($config as $key => $value) { | ||
| $camelized[lcfirst(ContainerBuilder::camelize($key))] = $value; | ||
|
|
@@ -67,15 +69,30 @@ private function loadAmqpTransport(array $config, ContainerBuilder $container) | |
| return $camelized; | ||
| }; | ||
|
|
||
| $camelizeKeyRecursive = function (array $config) use (&$camelizeKey) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just only have one recursive camelizeKey function? It seems a bit redundant to have them both.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is shouldn't apply for |
||
| $camelized = []; | ||
| foreach ($config as $key => $value) { | ||
| if (is_array($value)) { | ||
| $camelized[lcfirst(ContainerBuilder::camelize($key))] = $camelizeKey($value); | ||
| } else { | ||
| $camelized[lcfirst(ContainerBuilder::camelize($key))] = $value; | ||
| } | ||
| } | ||
|
|
||
| return $camelized; | ||
| }; | ||
|
|
||
| $definitions = []; | ||
| foreach ($config['connections'] as $name => $connectionConfig) { | ||
| $exchanges = $connectionConfig['exchanges']; | ||
| unset($connectionConfig['exchanges']); | ||
| $queues = $connectionConfig['queues']; | ||
| unset($connectionConfig['queues']); | ||
| $servers = $connectionConfig['connection']['servers']; | ||
| unset($connectionConfig['connection']['servers']); | ||
|
|
||
| $amqp = new DefinitionDecorator('event_band.transport.amqp.definition'); | ||
| $amqp->addMethodCall('connection', [$camelizeKey($connectionConfig)]); | ||
| $amqp->addMethodCall('connections', [$camelizeKeyRecursive($servers)]); | ||
| foreach ($exchanges as $exchange => $exchangeConfig) { | ||
| $exchangeType = $exchangeConfig['type']; | ||
| unset($exchangeConfig['type']); | ||
|
|
@@ -89,21 +106,42 @@ private function loadAmqpTransport(array $config, ContainerBuilder $container) | |
| $container->setDefinition($definitionId, $amqp); | ||
| $definitions[$name] = $definitionId; | ||
|
|
||
| $connection = new DefinitionDecorator('event_band.transport.amqp.connection_definition'); | ||
| $connection->setFactoryService($definitionId); | ||
| $connectionId = self::getAmqpConnectionDefinitionId($name); | ||
| $container->setDefinition($connectionId, $connection); | ||
| $driverPoolStrategy = new Definition('EventBand\Transport\AmqpLib\Pool\Strategy\StrategyInterface'); | ||
| $driverPoolStrategy->setPublic(false); | ||
| $driverPoolStrategy->addArgument($connectionConfig['connection']['strategy']); | ||
| $driverPoolStrategy->setFactory([new Reference(sprintf('event_band.transport.amqp.driver.driver_pool.strategy_factory.%s', $config['driver'])), 'create']); | ||
|
|
||
| $driverPool = new DefinitionDecorator(sprintf('event_band.transport.amqp.driver.driver_pool.%s', $config['driver'])); | ||
| $driverPool->replaceArgument(0, $driverPoolStrategy); | ||
|
|
||
| $container->setDefinition(self::getAmqpDriverPoolId($name), $driverPool); | ||
|
|
||
| foreach ($servers as $index => $server) { | ||
| $connection = new Definition('EventBand\Transport\Amqp\Definition\ConnectionDefinition'); | ||
| $connection->setPublic(false); | ||
| $connection->addArgument($index); | ||
| $connection->setFactory([new Reference($definitionId), 'getConnection']); | ||
|
|
||
| $factory = new DefinitionDecorator(sprintf('event_band.transport.amqp.connection_factory.%s', $config['driver'])); | ||
| $factory->addMethodCall('setDefinition', [new Reference($connectionId)]); | ||
| $container->setDefinition(self::getAmqpLibConnectionFactoryId($name), $factory); | ||
| $container->setDefinition(self::getAmqpConnectionDefinitionId($name, $index), $connection); | ||
|
|
||
| $driver = new DefinitionDecorator('event_band.transport.amqp.driver.'.$config['driver']); | ||
| $driver->replaceArgument(0, new Reference($this->getAmqpLibConnectionFactoryId($name))); | ||
| $container->setDefinition($this->getAmqpDriverId($name), $driver); | ||
| $factory = new DefinitionDecorator(sprintf('event_band.transport.amqp.connection_factory.%s', $config['driver'])); | ||
| $factory->addMethodCall('setDefinition', [new Reference(self::getAmqpConnectionDefinitionId($name, $index))]); | ||
| $container->setDefinition(self::getAmqpLibConnectionFactoryId($name, $index), $factory); | ||
|
|
||
| $parent = $container->getDefinition('event_band.transport.amqp.driver.'.$config['driver']); | ||
|
|
||
| $driver = new DefinitionDecorator('event_band.transport.amqp.driver.'.$config['driver']); | ||
| $driver->setClass($parent->getClass()); | ||
| $driver->replaceArgument(0, new Reference(self::getAmqpLibConnectionFactoryId($name, $index))); | ||
| $container->setDefinition(self::getAmqpDriverId($name, $index), $driver); | ||
|
|
||
| $driverPool->addMethodCall('addDriver', [$driver]); | ||
| } | ||
|
|
||
| $container->setAlias(self::getAmqpDriverId($name), new Alias(self::getAmqpDriverPoolId($name))); | ||
|
|
||
| $configurator = new DefinitionDecorator('event_band.transport.amqp.configurator'); | ||
| $configurator->replaceArgument(0, new Reference($this->getAmqpDriverId($name))); | ||
| $configurator->replaceArgument(0, new Reference(self::getAmqpDriverId($name))); | ||
| $container->setDefinition(self::getTypedTransportConfiguratorId('amqp', $name), $configurator); | ||
| $container->getDefinition(self::getTransportConfiguratorId()) | ||
| ->addMethodCall('registerConfigurator', ['amqp.'.$name, new Reference(self::getTypedTransportConfiguratorId('amqp', $name))]); | ||
|
|
@@ -287,13 +325,22 @@ public static function getTransportDefinitionId($type, $name) | |
| return sprintf('event_band.transport.%s.definition.%s', $type, $name); | ||
| } | ||
|
|
||
| private static function getAmqpConnectionDefinitionId($name) | ||
| private static function getAmqpConnectionDefinitionId($name, $index) | ||
| { | ||
| return sprintf('event_band.transport.amqp.connection_definition.%s', $name); | ||
| return sprintf('event_band.transport.amqp.connection_definition.%s.%d', $name, $index); | ||
| } | ||
|
|
||
| public static function getAmqpDriverId($connectionName) | ||
| public static function getAmqpDriverPoolId($connectionName) | ||
| { | ||
| return sprintf('event_band.transport.amqp.driver_pool.%s', $connectionName); | ||
| } | ||
|
|
||
| public static function getAmqpDriverId($connectionName, $index = null) | ||
| { | ||
| if ($index !== null) { | ||
| return sprintf('event_band.transport.amqp.connection_driver.%s.%d', $connectionName, $index); | ||
| } | ||
|
|
||
| return sprintf('event_band.transport.amqp.connection_driver.%s', $connectionName); | ||
| } | ||
|
|
||
|
|
@@ -302,9 +349,9 @@ public static function getAmqpConverterId($name) | |
| return sprintf('event_band.transport.amqp.converter.%s', $name); | ||
| } | ||
|
|
||
| private static function getAmqpLibConnectionFactoryId($name) | ||
| private static function getAmqpLibConnectionFactoryId($name, $index) | ||
| { | ||
| return sprintf('event_band.transport.amqplib.connection_factory.%s', $name); | ||
| return sprintf('event_band.transport.amqplib.connection_factory.%s.%d', $name, $index); | ||
| } | ||
|
|
||
| public static function getTransportConfiguratorId() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this used for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops, It should be deleted :)