-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Загрузка конфигурации битриксового сервис-локатора.
- Loading branch information
Showing
15 changed files
with
665 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Prokl\ServiceProvider\Utils; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
|
||
/** | ||
* Class BitrixSettingsDiAdapter | ||
* @package Prokl\ServiceProvider\Utils | ||
* | ||
* @since 12.07.2021 | ||
*/ | ||
class BitrixSettingsDiAdapter | ||
{ | ||
/** | ||
* Импортировать параметры из .settings.php | ||
* | ||
* @param ContainerInterface $container Контейнер. | ||
* @param array $settings Секция parameters .settings.php. | ||
* @param string|null $section Если задано, то параметры попадут в отдельную секцию контейнера. | ||
* | ||
* @return void | ||
*/ | ||
public function importParameters( | ||
ContainerInterface $container, | ||
array $settings, | ||
?string $section = null | ||
) : void { | ||
if ($section !== null) { | ||
$container->setParameter($section, $settings); | ||
return; | ||
} | ||
|
||
foreach ($settings as $id => $value) { | ||
$container->setParameter($id, $value); | ||
} | ||
} | ||
|
||
/** | ||
* Импортировать сервисы из .settings.php. | ||
* | ||
* @param ContainerInterface $container Контейнер. | ||
* @param array $services Секция services .settings.php. | ||
* | ||
* @return void | ||
*/ | ||
public function importServices(ContainerInterface $container, array $services) : void | ||
{ | ||
foreach ($services as $id => $service) { | ||
if (array_key_exists('constructor', $service) | ||
&& | ||
is_callable($service['constructor']) | ||
) { | ||
/** @var Definition $definition */ | ||
$definition = $container->register($id, FactoryClosure::class); | ||
$definition->setFactory([FactoryClosure::class, 'from']); | ||
$definition->addArgument($service['constructor']); | ||
$definition->setPublic(true); | ||
} | ||
|
||
if (array_key_exists('className', $service) && is_string($service['className'])) { | ||
$definition = $container->register($id, $service['className'])->setPublic(true); | ||
|
||
if (array_key_exists('constructorParams', $service) && is_callable($service['constructorParams'])) { | ||
$arguments = $service['constructorParams'](); | ||
if (is_array($arguments)) { | ||
foreach ($arguments as $argument) { | ||
$definition->addArgument($argument); | ||
} | ||
} else { | ||
$definition->addArgument($service['constructorParams']()); | ||
} | ||
} | ||
|
||
if (array_key_exists('constructorParams', $service) && is_array($service['constructorParams'])) { | ||
foreach ($service['constructorParams'] as $param) { | ||
$definition->addArgument($param); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Prokl\ServiceProvider\Utils; | ||
|
||
use Closure; | ||
|
||
/** | ||
* Class FactoryClosure | ||
* @package Prokl\FrameworkExtensionBundle\Services\Utils | ||
* | ||
* @since 13.07.2021 | ||
*/ | ||
class FactoryClosure | ||
{ | ||
/** | ||
* @param Closure $closure Closure. | ||
* | ||
* @return mixed | ||
*/ | ||
public function from(Closure $closure) | ||
{ | ||
return $closure(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace Prokl\ServiceProvider\Utils\Loaders; | ||
|
||
use Bitrix\Main\Config\Configuration; | ||
use Bitrix\Main\ModuleManager; | ||
use Prokl\ServiceProvider\Utils\BitrixSettingsDiAdapter; | ||
use Symfony\Component\DependencyInjection\Loader\FileLoader; | ||
use Symfony\Component\DependencyInjection\Loader\ProtectedPhpFileLoader; | ||
|
||
/** | ||
* Class PhpLoaderSettingsBitrix | ||
* Загрузчик битриксовых конфигурационных файлов | ||
* @package Prokl\ServiceProvider\Utils\Loaders | ||
* | ||
* @since 12.07.2021 | ||
*/ | ||
class PhpLoaderSettingsBitrix extends FileLoader | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function load($resource, string $type = null) | ||
{ | ||
// the container and loader variables are exposed to the included file below | ||
$container = $this->container; | ||
|
||
$path = $this->locator->locate($resource); | ||
|
||
$this->setCurrentDir(\dirname($path)); | ||
$this->container->fileExists($path); | ||
|
||
// the closure forbids access to the private scope in the included file | ||
$load = \Closure::bind(function ($path, $env) { | ||
return $this->loadBitrixConfig('services', true); | ||
}, $this, ProtectedPhpFileLoader::class); | ||
|
||
try { | ||
$callback = $load($path, $this->env); | ||
if (is_array($callback)) { | ||
$adapter = new BitrixSettingsDiAdapter(); | ||
$adapter->importServices($container, $callback); | ||
} | ||
} finally { | ||
$this->instanceof = []; | ||
$this->registerAliasesForSinglyImplementedInterfaces(); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($resource, string $type = null) | ||
{ | ||
if (!\is_string($resource) | ||
// ToDo более строгая проверка на нужный файл. | ||
|| stripos($resource, '.settings') === false | ||
) { | ||
return false; | ||
} | ||
|
||
if (null === $type && 'php' === pathinfo($resource, \PATHINFO_EXTENSION)) { | ||
return true; | ||
} | ||
|
||
return 'php' === $type; | ||
} | ||
|
||
/** | ||
* Загрузка битриксовых конфигов. | ||
* | ||
* @param string $key Ключ в параметрах битриксовых файлов. | ||
* @param boolean $loadModulesServices Загружать такую же секцию в установленных модулях. | ||
* | ||
* @return array | ||
*/ | ||
public function loadBitrixConfig(string $key, bool $loadModulesServices = true) : array | ||
{ | ||
$mainBitrixServices = Configuration::getInstance()->get($key) ?? []; | ||
|
||
// Собрать конфиги всех установленных модулей. | ||
$servicesModules = []; | ||
|
||
if ($loadModulesServices) { | ||
foreach (ModuleManager::getInstalledModules() as $module) { | ||
$services = Configuration::getInstance($module['ID'])->get($key) ?? []; | ||
if (count($services) > 0) { | ||
$servicesModules[] = $services; | ||
} | ||
} | ||
} | ||
|
||
return array_merge($mainBitrixServices, ...$servicesModules); | ||
} | ||
} |
Oops, something went wrong.