Skip to content

Commit

Permalink
Рефакторинг
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 15, 2021
1 parent dd3a9e7 commit b25b47e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'rabbitmq.fallback.class' => 'Proklung\RabbitMq\RabbitMq\Fallback',
// Внутренние параметры модуля
'cache_path' => '/bitrix/cache/s1/proklung.rabbitmq', // Путь к закешированному контейнеру
'compile_container_envs' => ['dev', 'prod'], // Окружения при которых компилировать контейнер
'compile_container_envs' => ['prod'], // Окружения при которых компилировать контейнер
'container.dumper.inline_factories' => false, // Дампить контейнер как одиночные файлы
],
'readonly' => false,
Expand Down
71 changes: 42 additions & 29 deletions lib/Integration/DI/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Services
/**
* @var ContainerBuilder $container Контейнер.
*/
private $container;
private static $container;

/**
* @var string $environment
Expand Down Expand Up @@ -65,13 +65,6 @@ public function __construct()
$this->parameters['cache_path'] = $this->parameters['cache_path'] ?? '/bitrix/cache/proklung.rabbitmq';
$this->parameters['container.dumper.inline_factories'] = $this->parameters['container.dumper.inline_factories'] ?? false;
$this->parameters['compile_container_envs'] = (array)$this->parameters['compile_container_envs'];

$this->container = new ContainerBuilder();
$adapter = new BitrixSettingsDiAdapter();

$adapter->importParameters($this->container, $this->config);
$adapter->importParameters($this->container, $this->parameters);
$adapter->importServices($this->container, $this->services);
}

/**
Expand Down Expand Up @@ -106,6 +99,11 @@ public static function getInstance() : Container
*/
public function load() : void
{
if (static::$container !== null) {
return;
}

$this->createContainer();
$compilerContainer = new CompilerContainer();

// Кэшировать контейнер?
Expand All @@ -114,8 +112,8 @@ public function load() : void
return;
}

$this->container = $compilerContainer->cacheContainer(
$this->container,
static::$container = $compilerContainer->cacheContainer(
static::$container,
$_SERVER['DOCUMENT_ROOT'] . $this->parameters['cache_path'],
'container.php',
$this->environment,
Expand All @@ -141,7 +139,7 @@ public function initContainer() : void

$this->loadPartsHolder();

$this->container->compile(false);
static::$container->compile(false);
}

/**
Expand All @@ -151,7 +149,22 @@ public function initContainer() : void
*/
public function getContainer(): Container
{
return $this->container;
return static::$container;
}

/**
* Создать пустой экземпляр контейнера.
*
* @return void
*/
private function createContainer() : void
{
static::$container = new ContainerBuilder();
$adapter = new BitrixSettingsDiAdapter();

$adapter->importParameters(static::$container, $this->config);
$adapter->importParameters(static::$container, $this->parameters);
$adapter->importServices(static::$container, $this->services);
}

/**
Expand All @@ -176,7 +189,7 @@ private function loadPartsHolder() : void
$this->injectConnection($definition, $binding['connection']);
$key = md5(json_encode($binding));

$this->container->setDefinition(sprintf('rabbitmq.binding.%s', $key), $definition);
static::$container->setDefinition(sprintf('rabbitmq.binding.%s', $key), $definition);
}
}

Expand All @@ -202,7 +215,7 @@ private function loadConnections() : void
}
$definition->setPublic(true);
$factoryName = sprintf('rabbitmq.connection_factory.%s', $key);
$this->container->setDefinition($factoryName, $definition);
static::$container->setDefinition($factoryName, $definition);

$definition = new Definition($classParam);
if (method_exists($definition, 'setFactory')) {
Expand All @@ -216,7 +229,7 @@ private function loadConnections() : void
$definition->addTag('rabbitmq.connection');
$definition->setPublic(true);

$this->container->setDefinition(sprintf('rabbitmq.connection.%s', $key), $definition);
static::$container->setDefinition(sprintf('rabbitmq.connection.%s', $key), $definition);
}
}

Expand All @@ -243,7 +256,7 @@ private function loadBindings() : void
$connectionName = "rabbitmq.connection.{$binding['connection']}";

/** @var Binding $instance */
$instance = new $className($this->container->get($connectionName));
$instance = new $className(static::$container->get($connectionName));

$instance->setArguments($binding['arguments']);
$instance->setDestination($binding['destination']);
Expand All @@ -255,7 +268,7 @@ private function loadBindings() : void
return $instance;
};

$this->container->set(
static::$container->set(
"rabbitmq.binding.{$key}",
$binding()
);
Expand All @@ -270,7 +283,7 @@ private function loadProducers() : void
{
if ($this->config['sandbox'] == false) {
foreach ($this->config['producers'] as $key => $producer) {
$definition = new Definition($producer['class'] ?? $this->container->getParameter('rabbitmq.producer.class'));
$definition = new Definition($producer['class'] ?? static::$container->getParameter('rabbitmq.producer.class'));
$definition->setPublic(true);
$definition->addTag('rabbitmq.base_amqp');
$definition->addTag('rabbitmq.producer');
Expand All @@ -296,15 +309,15 @@ private function loadProducers() : void

$producerServiceName = sprintf('rabbitmq.%s_producer', $key);

$this->container->setDefinition($producerServiceName, $definition);
static::$container->setDefinition($producerServiceName, $definition);
if (null !== $producer['service_alias']) {
$this->container->setAlias($producer['service_alias'], $producerServiceName);
static::$container->setAlias($producer['service_alias'], $producerServiceName);
}
}
} else {
foreach ($this->config['producers'] as $key => $producer) {
$definition = new Definition('%rabbitmq.fallback.class%');
$this->container->setDefinition(sprintf('rabbitmq.%s_producer', $key), $definition);
static::$container->setDefinition(sprintf('rabbitmq.%s_producer', $key), $definition);
}
}
}
Expand Down Expand Up @@ -372,7 +385,7 @@ private function loadConsumers() : void
}

$name = sprintf('rabbitmq.%s_consumer', $key);
$this->container->setDefinition($name, $definition);
static::$container->setDefinition($name, $definition);
$this->addDequeuerAwareCall($consumer['callback'], $name);
}
}
Expand All @@ -399,7 +412,7 @@ private function loadRpcClients() : void
}
$definition->setPublic(true);

$this->container->setDefinition(sprintf('rabbitmq.%s_rpc', $key), $definition);
static::$container->setDefinition(sprintf('rabbitmq.%s_rpc', $key), $definition);
}
}

Expand Down Expand Up @@ -442,7 +455,7 @@ private function loadRpcServers() : void
$definition->addMethodCall('setSerializer', array($server['serializer']));
}

$this->container->setDefinition(sprintf('rabbitmq.%s_server', $key), $definition);
static::$container->setDefinition(sprintf('rabbitmq.%s_server', $key), $definition);
}
}

Expand All @@ -464,7 +477,7 @@ private function loadAnonConsumers() : void
$this->injectConnection($definition, $anon['connection']);

$name = sprintf('rabbitmq.%s_anon', $key);
$this->container->setDefinition($name, $definition);
static::$container->setDefinition($name, $definition);
$this->addDequeuerAwareCall($anon['callback'], $name);
}
}
Expand Down Expand Up @@ -528,7 +541,7 @@ private function loadBatchConsumers() : void
$this->injectLogger($definition);
}

$this->container->setDefinition(sprintf('rabbitmq.%s_batch', $key), $definition);
static::$container->setDefinition(sprintf('rabbitmq.%s_batch', $key), $definition);
}
}

Expand Down Expand Up @@ -557,7 +570,7 @@ private function registerCallbackAsService(string $class)
// Регистрация class как сервиса.
$defCallBack = new Definition($class);
$defCallBack->setPublic(true);
$this->container->setDefinition($class, $defCallBack);
static::$container->setDefinition($class, $defCallBack);
}

/**
Expand Down Expand Up @@ -623,11 +636,11 @@ private function argumentsStringAsArray($arguments)
*/
private function addDequeuerAwareCall($callback, $name) : void
{
if (!$this->container->has($callback)) {
if (!static::$container->has($callback)) {
return;
}

$callbackDefinition = $this->container->findDefinition($callback);
$callbackDefinition = static::$container->findDefinition($callback);
if ($this->isDequeverAwareInterface($callbackDefinition->getClass())) {
$callbackDefinition->addMethodCall('setDequeuer', array(new Reference($name)));
}
Expand Down

0 comments on commit b25b47e

Please sign in to comment.