Skip to content

Commit 8a743c8

Browse files
committed
Рефакторинг
1 parent c503818 commit 8a743c8

File tree

1 file changed

+15
-107
lines changed

1 file changed

+15
-107
lines changed

lib/DI/services.php

Lines changed: 15 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
namespace Proklung\Redis\DI;
44

55
use Bitrix\Main\Config\Configuration;
6-
use Closure;
76
use Enqueue\Consumption\Extension\ReplyExtension;
87
use Enqueue\Consumption\Extension\SignalExtension;
9-
use ProklUng\ContainerBoilerplate\CompilerContainer;
10-
use ProklUng\ContainerBoilerplate\Utils\BitrixSettingsDiAdapter;
8+
use ProklUng\ContainerBoilerplate\DI\AbstractServiceContainer;
119
use Proklung\Redis\DI\Extensions\ResetServicesExtension;
1210
use Proklung\Redis\Profiler\MessageQueueCollector;
1311
use Enqueue\Client\CommandSubscriberInterface;
@@ -30,7 +28,6 @@
3028
use Symfony\Component\Config\Definition\Processor;
3129
use Symfony\Component\Config\FileLocator;
3230
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
33-
use Symfony\Component\DependencyInjection\Container;
3431
use Symfony\Component\DependencyInjection\ContainerBuilder;
3532
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
3633
use Symfony\Component\DependencyInjection\Reference;
@@ -42,114 +39,50 @@
4239
* @since 13.07.2021
4340
* @internal Частично форкнуто из оригинального пакета https://github.com/php-enqueue/enqueue-bundle.
4441
*/
45-
class Services
42+
class Services extends AbstractServiceContainer
4643
{
4744
/**
4845
* @var ContainerBuilder|null $container Контейнер.
4946
*/
50-
private static $container;
47+
protected static $container;
5148

5249
/**
53-
* @var array $config
50+
* @var array $config Битриксовая конфигурация.
5451
*/
55-
private $config;
52+
protected $config = [];
5653

5754
/**
58-
* @var array $parameters
55+
* @var array $parameters Параметры битриксового сервис-локатора.
5956
*/
60-
private $parameters;
57+
protected $parameters = [];
6158

6259
/**
63-
* @var array $services
60+
* @var array $services Сервисы битриксового сервис-локатора.
6461
*/
65-
private $services;
62+
protected $services = [];
6663

6764
/**
68-
* @var string $environment
65+
* @var string $moduleId ID модуля (переопределяется наследником).
6966
*/
70-
private $environment;
71-
72-
/**
73-
* @var boolean $debug Режим отладки.
74-
*/
75-
private $debug;
67+
protected $moduleId = 'proklung.redis';
7668

7769
/**
7870
* Services constructor.
7971
*/
8072
public function __construct()
8173
{
82-
$this->debug = (bool)$_ENV['DEBUG'] ?? true;
83-
$this->environment = $this->debug ? 'dev' : 'prod';
74+
parent::__construct();
8475

85-
$this->config = Configuration::getInstance()->get('proklung.redis') ?? ['enqueue' => []];
86-
$this->parameters = Configuration::getInstance('proklung.redis')->get('parameters') ?? [];
87-
$this->services = Configuration::getInstance('proklung.redis')->get('services') ?? [];
76+
$this->config = Configuration::getInstance()->get($this->moduleId) ?? ['enqueue' => []];
77+
$this->parameters = Configuration::getInstance($this->moduleId)->get('parameters') ?? [];
78+
$this->services = Configuration::getInstance($this->moduleId)->get('services') ?? [];
8879

8980
// Инициализация параметров контейнера.
9081
$this->parameters['cache_path'] = $this->parameters['cache_path'] ?? '/bitrix/cache/proklung.redis';
9182
$this->parameters['container.dumper.inline_factories'] = $this->parameters['container.dumper.inline_factories'] ?? false;
9283
$this->parameters['compile_container_envs'] = (array)$this->parameters['compile_container_envs'];
9384
}
9485

95-
/**
96-
* Загрузка и инициализация контейнера.
97-
*
98-
* @return Container
99-
* @throws Exception
100-
*/
101-
public static function boot() : Container
102-
{
103-
$self = new static();
104-
105-
$self->load();
106-
107-
return $self->getContainer();
108-
}
109-
110-
/**
111-
* Alias boot для читаемости.
112-
*
113-
* @return Container
114-
* @throws Exception
115-
*/
116-
public static function getInstance() : Container
117-
{
118-
return static::boot();
119-
}
120-
121-
/**
122-
* Загрузка всего хозяйства.
123-
*
124-
* @return void
125-
* @throws Exception
126-
*/
127-
public function load() : void
128-
{
129-
if (static::$container !== null) {
130-
return;
131-
}
132-
133-
$this->createContainer();
134-
$compilerContainer = new CompilerContainer($_SERVER['DOCUMENT_ROOT']);
135-
$compilerContainer->setModuleId('proklung.redis');
136-
137-
// Кэшировать контейнер?
138-
if (!in_array($this->environment, $this->parameters['compile_container_envs'], true)) {
139-
$this->initContainer();
140-
return;
141-
}
142-
143-
static::$container = $compilerContainer->cacheContainer(
144-
static::$container,
145-
$_SERVER['DOCUMENT_ROOT'] . $this->parameters['cache_path'],
146-
'container.php',
147-
$this->environment,
148-
$this->debug,
149-
Closure::fromCallable([$this, 'initContainer'])
150-
);
151-
}
152-
15386
/**
15487
* Инициализация контейнера.
15588
*
@@ -252,31 +185,6 @@ public function initContainer() : void
252185
static::$container->compile(true);
253186
}
254187

255-
/**
256-
* Экземпляр контейнера.
257-
*
258-
* @return Container
259-
*/
260-
public function getContainer(): Container
261-
{
262-
return static::$container;
263-
}
264-
265-
/**
266-
* Создать пустой экземпляр контейнера.
267-
*
268-
* @return void
269-
*/
270-
private function createContainer() : void
271-
{
272-
static::$container = new ContainerBuilder();
273-
$adapter = new BitrixSettingsDiAdapter();
274-
275-
$adapter->importParameters(static::$container, $this->config);
276-
$adapter->importParameters(static::$container, $this->parameters);
277-
$adapter->importServices(static::$container, $this->services);
278-
}
279-
280188
/**
281189
* @param ContainerBuilder $container
282190
* @param string $defaultClient

0 commit comments

Comments
 (0)