From 947dad46c4e6533ca625fde8d60ddd436391fe6a Mon Sep 17 00:00:00 2001 From: ProklUng Date: Fri, 6 Aug 2021 17:06:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BA=D0=B0=20=D0=BF=D0=BE=20=D0=BF=D1=80=D0=B8=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D1=82=D0=B5=D1=82=D1=83=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=B0=D0=B2=D1=82=D0=BE=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.MD | 12 ++++ src/PostLoadingPass/BootstrapServices.php | 28 ++++++++-- .../BootstrapServicesTest.php | 56 ++++++++++++++++++- 3 files changed, 89 insertions(+), 7 deletions(-) diff --git a/readme.MD b/readme.MD index b4992f4..16961e1 100644 --- a/readme.MD +++ b/readme.MD @@ -186,6 +186,18 @@ var_dump(container($micro)->getParameter('example')); tags: ['service.bootstrap'] ``` +Поддерживается приоритет запуска. Тогда надо так: + +```yaml + app.options: + class: Local\Services\AppOptions + arguments: ['%kernel.environment%', '@parameter_bag'] + tags: + - { name: 'service.bootstrap', priority: 100 } +``` + +Сервис с приоритетом 100 запустится раньше сервиса с приоритетом 200. + ## Автоматическая подвязка на события Битрикс Тэг: `bitrix.events.init`. diff --git a/src/PostLoadingPass/BootstrapServices.php b/src/PostLoadingPass/BootstrapServices.php index eedbe73..2b475a9 100644 --- a/src/PostLoadingPass/BootstrapServices.php +++ b/src/PostLoadingPass/BootstrapServices.php @@ -14,7 +14,10 @@ * * @package Prokl\ServiceProvider\PostLoadingPass * - * @since 28.09.2020 + * @since 26.09.2020 + * @since 27.09.2020 Доработки. + * @since 04.05.2021 Исключения сервисов автозагрузки больше не глушатся. + * @since 06.08.2021 Сортировка по приоритету. */ final class BootstrapServices implements PostLoadingPassInterface { @@ -25,7 +28,7 @@ final class BootstrapServices implements PostLoadingPassInterface /** * @inheritDoc - * @throws Exception + * @throws Exception Когда проблемы с получением сервиса из контейнера. */ public function action(Container $containerBuilder) : bool { @@ -39,10 +42,27 @@ public function action(Container $containerBuilder) : bool return false; } + $result = []; foreach ($bootstrapServices as $service => $value) { - $containerBuilder->get($service); + $priority = 0; + if (array_key_exists(0, $value) && is_array($value[0])) { + if (array_key_exists('priority', $value[0])) { + $priority = (int)$value[0]['priority']; + } + } + + $result[] = ['service' => $service, 'priority' => $priority]; + } + + usort($result, static function ($a, $b) : bool { + // @phpstan-ignore-line + return $a['priority'] > $b['priority']; + }); + + foreach ($result as $service) { + $containerBuilder->get($service['service']); } return true; } -} +} \ No newline at end of file diff --git a/tests/Cases/PostLoadingPasses/BootstrapServicesTest.php b/tests/Cases/PostLoadingPasses/BootstrapServicesTest.php index daaf773..50eb6fa 100644 --- a/tests/Cases/PostLoadingPasses/BootstrapServicesTest.php +++ b/tests/Cases/PostLoadingPasses/BootstrapServicesTest.php @@ -28,6 +28,7 @@ class BootstrapServicesTest extends BaseTestCase */ private $stubService; + /** * @inheritDoc */ @@ -44,7 +45,6 @@ protected function setUp(): void * * @return void * @throws Exception - * */ public function testAction(): void { @@ -78,6 +78,56 @@ public function testActionNoListener(): void ); } + /** + * Сортировка по приоритету. + * + * @return void + * @throws Exception + */ + public function testPriority() : void + { + ob_start(); + $fooService = new class { + public function __construct() + { + echo 'First'; + } + }; + + $booService = new class { + public function __construct() + { + echo 'Second'; + } + }; + ob_get_clean(); + + $container = new ContainerBuilder(); + $container + ->register('fooService', get_class($fooService)) + ->setPublic(true) + ->setTags(['bootstrap.service' => ['priority' => 200]]) + ; + + $container + ->register('barService', get_class($booService)) + ->setPublic(true) + ->setTags(['bootstrap.service' => ['priority' => 100]]) + ; + + $container->setParameter('_bootstrap', [ + 'fooService' => [['priority' => 200]], 'barService' => [['priority' => 100]], + ]); + + ob_start(); + $result = $this->obTestObject->action($container); + $content = ob_get_clean(); + + $this->assertTrue($result); + // Второй по порядку, но первый по сортировке сервис отработал первым. + $this->assertSame('SecondFirst', $content); + } + /** * Мок обработчика. * @@ -106,7 +156,7 @@ public function addEvent(): void * Тестовый контейнер. * * @param string $serviceId ID сервиса. - * @param array $params Параметры. + * @param array $params * * @return ContainerBuilder */ @@ -147,4 +197,4 @@ private function process(ContainerBuilder $container): void { (new RemoveUnusedDefinitionsPass())->process($container); } -} +} \ No newline at end of file