Skip to content

Commit 5c7c7d5

Browse files
committed
Рефакторинг
1 parent 2880a4d commit 5c7c7d5

File tree

1 file changed

+38
-106
lines changed

1 file changed

+38
-106
lines changed

lib/Integration/DI/Services.php

Lines changed: 38 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
namespace Proklung\RabbitMq\Integration\DI;
44

55
use Bitrix\Main\Config\Configuration;
6-
use Closure;
76
use Exception;
8-
use ProklUng\ContainerBoilerplate\CompilerContainer;
9-
use ProklUng\ContainerBoilerplate\Utils\BitrixSettingsDiAdapter;
10-
use Symfony\Component\DependencyInjection\Container;
7+
use ProklUng\ContainerBoilerplate\DI\AbstractServiceContainer;
8+
use ReflectionClass;
9+
use ReflectionException;
1110
use Symfony\Component\DependencyInjection\ContainerBuilder;
1211
use Proklung\RabbitMq\RabbitMq\Binding;
1312
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -18,112 +17,50 @@
1817
* Class Services
1918
* @package Proklung\RabbitMq\Integration\DI
2019
*/
21-
class Services
20+
class Services extends AbstractServiceContainer
2221
{
2322
/**
24-
* @var array $config
23+
* @var ContainerBuilder|null $container Контейнер.
2524
*/
26-
private $config;
25+
protected static $container;
2726

2827
/**
29-
* @var array $parameters
28+
* @var array $config Битриксовая конфигурация.
3029
*/
31-
private $parameters;
30+
protected $config = [];
3231

3332
/**
34-
* @var array $services
33+
* @var array $parameters Параметры битриксового сервис-локатора.
3534
*/
36-
private $services;
35+
protected $parameters = [];
3736

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

4342
/**
44-
* @var string $environment
43+
* @var string $moduleId ID модуля (переопределяется наследником).
4544
*/
46-
private $environment;
47-
48-
/**
49-
* @var boolean $debug Режим отладки.
50-
*/
51-
private $debug;
45+
protected $moduleId = 'proklung.rabbitmq';
5246

5347
/**
5448
* Services constructor.
5549
*/
5650
public function __construct()
5751
{
58-
$this->debug = (bool)$_ENV['DEBUG'] ?? true;
59-
$this->environment = $this->debug ? 'dev' : 'prod';
52+
parent::__construct();
6053

6154
$this->config = Configuration::getInstance()->get('rabbitmq') ?? [];
62-
$this->parameters = Configuration::getInstance('proklung.rabbitmq')->get('parameters') ?? [];
63-
$this->services = Configuration::getInstance('proklung.rabbitmq')->get('services') ?? [];
55+
$this->parameters = Configuration::getInstance($this->moduleId)->get('parameters') ?? [];
56+
$this->services = Configuration::getInstance($this->moduleId)->get('services') ?? [];
6457

6558
// Инициализация параметров контейнера.
6659
$this->parameters['cache_path'] = $this->parameters['cache_path'] ?? '/bitrix/cache/proklung.rabbitmq';
6760
$this->parameters['container.dumper.inline_factories'] = $this->parameters['container.dumper.inline_factories'] ?? false;
6861
$this->parameters['compile_container_envs'] = (array)$this->parameters['compile_container_envs'];
6962
}
7063

71-
/**
72-
* Загрузка и инициализация контейнера.
73-
*
74-
* @return Container
75-
* @throws Exception
76-
*/
77-
public static function boot() : Container
78-
{
79-
$self = new static();
80-
81-
$self->load();
82-
83-
return $self->getContainer();
84-
}
85-
86-
/**
87-
* Alias boot для читаемости.
88-
*
89-
* @return Container
90-
* @throws Exception
91-
*/
92-
public static function getInstance() : Container
93-
{
94-
return static::boot();
95-
}
96-
97-
/**
98-
* @return void
99-
* @throws Exception
100-
*/
101-
public function load() : void
102-
{
103-
if (static::$container !== null) {
104-
return;
105-
}
106-
107-
$this->createContainer();
108-
$compilerContainer = new CompilerContainer($_SERVER['DOCUMENT_ROOT']);
109-
$compilerContainer->setModuleId('proklung.rabbitmq');
110-
111-
// Кэшировать контейнер?
112-
if (!in_array($this->environment, $this->parameters['compile_container_envs'], true)) {
113-
$this->initContainer();
114-
return;
115-
}
116-
117-
static::$container = $compilerContainer->cacheContainer(
118-
static::$container,
119-
$_SERVER['DOCUMENT_ROOT'] . $this->parameters['cache_path'],
120-
'container.php',
121-
$this->environment,
122-
$this->debug,
123-
Closure::fromCallable([$this, 'initContainer'])
124-
);
125-
}
126-
12764
/**
12865
* @return void
12966
* @throws Exception
@@ -144,31 +81,6 @@ public function initContainer() : void
14481
static::$container->compile(false);
14582
}
14683

147-
/**
148-
* Экземпляр контейнера.
149-
*
150-
* @return Container
151-
*/
152-
public function getContainer(): Container
153-
{
154-
return static::$container;
155-
}
156-
157-
/**
158-
* Создать пустой экземпляр контейнера.
159-
*
160-
* @return void
161-
*/
162-
private function createContainer() : void
163-
{
164-
static::$container = new ContainerBuilder();
165-
$adapter = new BitrixSettingsDiAdapter();
166-
167-
$adapter->importParameters(static::$container, $this->config);
168-
$adapter->importParameters(static::$container, $this->parameters);
169-
$adapter->importServices(static::$container, $this->services);
170-
}
171-
17284
/**
17385
* @return void
17486
* @throws Exception
@@ -635,6 +547,7 @@ private function argumentsStringAsArray($arguments)
635547
* @param string $name
636548
*
637549
* @return void
550+
* @throws ReflectionException
638551
*/
639552
private function addDequeuerAwareCall($callback, $name) : void
640553
{
@@ -648,18 +561,34 @@ private function addDequeuerAwareCall($callback, $name) : void
648561
}
649562
}
650563

564+
/**
565+
* @param Definition $definition
566+
* @param mixed $connectionName
567+
*
568+
* @return void
569+
*/
651570
private function injectConnection(Definition $definition, $connectionName)
652571
{
653572
$definition->addArgument(new Reference(sprintf('rabbitmq.connection.%s', $connectionName)));
654573
}
655574

575+
/**
576+
* @param string $class
577+
*
578+
* @return boolean
579+
*
580+
* @throws ReflectionException
581+
*/
656582
private function isDequeverAwareInterface(string $class): bool
657583
{
658-
$refClass = new \ReflectionClass($class);
584+
$refClass = new ReflectionClass($class);
659585

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

589+
/**
590+
* @return array
591+
*/
663592
private function getDefaultExchangeOptions(): array
664593
{
665594
return [
@@ -670,6 +599,9 @@ private function getDefaultExchangeOptions(): array
670599
];
671600
}
672601

602+
/**
603+
* @return array
604+
*/
673605
private function getDefaultQueueOptions(): array
674606
{
675607
return [

0 commit comments

Comments
 (0)