Skip to content

Commit

Permalink
Рефакторинг
Browse files Browse the repository at this point in the history
  • Loading branch information
ProklUng committed Jul 16, 2021
1 parent 2880a4d commit 5c7c7d5
Showing 1 changed file with 38 additions and 106 deletions.
144 changes: 38 additions & 106 deletions lib/Integration/DI/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace Proklung\RabbitMq\Integration\DI;

use Bitrix\Main\Config\Configuration;
use Closure;
use Exception;
use ProklUng\ContainerBoilerplate\CompilerContainer;
use ProklUng\ContainerBoilerplate\Utils\BitrixSettingsDiAdapter;
use Symfony\Component\DependencyInjection\Container;
use ProklUng\ContainerBoilerplate\DI\AbstractServiceContainer;
use ReflectionClass;
use ReflectionException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Proklung\RabbitMq\RabbitMq\Binding;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand All @@ -18,112 +17,50 @@
* Class Services
* @package Proklung\RabbitMq\Integration\DI
*/
class Services
class Services extends AbstractServiceContainer
{
/**
* @var array $config
* @var ContainerBuilder|null $container Контейнер.
*/
private $config;
protected static $container;

/**
* @var array $parameters
* @var array $config Битриксовая конфигурация.
*/
private $parameters;
protected $config = [];

/**
* @var array $services
* @var array $parameters Параметры битриксового сервис-локатора.
*/
private $services;
protected $parameters = [];

/**
* @var ContainerBuilder $container Контейнер.
* @var array $services Сервисы битриксового сервис-локатора.
*/
private static $container;
protected $services = [];

/**
* @var string $environment
* @var string $moduleId ID модуля (переопределяется наследником).
*/
private $environment;

/**
* @var boolean $debug Режим отладки.
*/
private $debug;
protected $moduleId = 'proklung.rabbitmq';

/**
* Services constructor.
*/
public function __construct()
{
$this->debug = (bool)$_ENV['DEBUG'] ?? true;
$this->environment = $this->debug ? 'dev' : 'prod';
parent::__construct();

$this->config = Configuration::getInstance()->get('rabbitmq') ?? [];
$this->parameters = Configuration::getInstance('proklung.rabbitmq')->get('parameters') ?? [];
$this->services = Configuration::getInstance('proklung.rabbitmq')->get('services') ?? [];
$this->parameters = Configuration::getInstance($this->moduleId)->get('parameters') ?? [];
$this->services = Configuration::getInstance($this->moduleId)->get('services') ?? [];

// Инициализация параметров контейнера.
$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'];
}

/**
* Загрузка и инициализация контейнера.
*
* @return Container
* @throws Exception
*/
public static function boot() : Container
{
$self = new static();

$self->load();

return $self->getContainer();
}

/**
* Alias boot для читаемости.
*
* @return Container
* @throws Exception
*/
public static function getInstance() : Container
{
return static::boot();
}

/**
* @return void
* @throws Exception
*/
public function load() : void
{
if (static::$container !== null) {
return;
}

$this->createContainer();
$compilerContainer = new CompilerContainer($_SERVER['DOCUMENT_ROOT']);
$compilerContainer->setModuleId('proklung.rabbitmq');

// Кэшировать контейнер?
if (!in_array($this->environment, $this->parameters['compile_container_envs'], true)) {
$this->initContainer();
return;
}

static::$container = $compilerContainer->cacheContainer(
static::$container,
$_SERVER['DOCUMENT_ROOT'] . $this->parameters['cache_path'],
'container.php',
$this->environment,
$this->debug,
Closure::fromCallable([$this, 'initContainer'])
);
}

/**
* @return void
* @throws Exception
Expand All @@ -144,31 +81,6 @@ public function initContainer() : void
static::$container->compile(false);
}

/**
* Экземпляр контейнера.
*
* @return Container
*/
public function getContainer(): 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);
}

/**
* @return void
* @throws Exception
Expand Down Expand Up @@ -635,6 +547,7 @@ private function argumentsStringAsArray($arguments)
* @param string $name
*
* @return void
* @throws ReflectionException
*/
private function addDequeuerAwareCall($callback, $name) : void
{
Expand All @@ -648,18 +561,34 @@ private function addDequeuerAwareCall($callback, $name) : void
}
}

/**
* @param Definition $definition
* @param mixed $connectionName
*
* @return void
*/
private function injectConnection(Definition $definition, $connectionName)
{
$definition->addArgument(new Reference(sprintf('rabbitmq.connection.%s', $connectionName)));
}

/**
* @param string $class
*
* @return boolean
*
* @throws ReflectionException
*/
private function isDequeverAwareInterface(string $class): bool
{
$refClass = new \ReflectionClass($class);
$refClass = new ReflectionClass($class);

return $refClass->implementsInterface('Proklung\RabbitMq\RabbitMq\DequeuerAwareInterface');
}

/**
* @return array
*/
private function getDefaultExchangeOptions(): array
{
return [
Expand All @@ -670,6 +599,9 @@ private function getDefaultExchangeOptions(): array
];
}

/**
* @return array
*/
private function getDefaultQueueOptions(): array
{
return [
Expand Down

0 comments on commit 5c7c7d5

Please sign in to comment.