Skip to content

Commit 2ec37b3

Browse files
committed
Автоконфигурация тэгов вынесена в отдельный метод. Карта соответствий - в отдельном классе.
1 parent 13d627e commit 2ec37b3

File tree

2 files changed

+94
-22
lines changed

2 files changed

+94
-22
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Prokl\ServiceProvider\Framework;
4+
5+
use RuntimeException;
6+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7+
use Symfony\Component\DependencyInjection\ServiceLocator;
8+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
10+
use Symfony\Component\Validator\ConstraintValidatorInterface;
11+
use Symfony\Component\Validator\ObjectInitializerInterface;
12+
13+
/**
14+
* Class AutoconfigureConfig
15+
* @package Prokl\ServiceProvider\Framework
16+
*
17+
* @since 26.06.2021
18+
*/
19+
class AutoconfigureConfig
20+
{
21+
/**
22+
* @var string[] $autoConfigure Автоконфигурация тэгов.
23+
*/
24+
private $autoConfigure = [
25+
'controller.service_arguments' => AbstractController::class,
26+
'controller.argument_value_resolver' => ArgumentValueResolverInterface::class,
27+
'container.service_locator' => ServiceLocator::class,
28+
'kernel.event_subscriber' => EventSubscriberInterface::class,
29+
'validator.constraint_validator' => ConstraintValidatorInterface::class,
30+
'validator.initializer' => ObjectInitializerInterface::class,
31+
];
32+
33+
/**
34+
* AutoconfigureConfig constructor.
35+
*
36+
* @param string[] $autoConfigure Дополнительные конфигураторы для тэгов.
37+
*
38+
* @throws RuntimeException Когда необходимая зависимость не существует.
39+
*/
40+
public function __construct(array $autoConfigure = [])
41+
{
42+
$this->autoConfigure[] = $autoConfigure;
43+
44+
$this->checkDependency();
45+
}
46+
47+
/**
48+
* Карта автоконфигурируемых тэгов.
49+
*
50+
* @return string[]
51+
*/
52+
public function getAutoConfigure(): array
53+
{
54+
return $this->autoConfigure;
55+
}
56+
57+
/**
58+
* Проверка на существование зависимостей.
59+
*
60+
* @return void
61+
* @throws RuntimeException Когда необходимая зависимость не существует.
62+
*/
63+
private function checkDependency() : void
64+
{
65+
foreach ($this->autoConfigure as $class) {
66+
if (!class_exists($class)) {
67+
throw new RuntimeException(
68+
'Need class ' . $class . ' not exist.'
69+
);
70+
}
71+
}
72+
}
73+
}

src/ServiceProvider.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
use Exception;
88
use InvalidArgumentException;
99
use Prokl\ServiceProvider\Bundles\BundlesLoader;
10+
use Prokl\ServiceProvider\Framework\AutoconfigureConfig;
1011
use Prokl\ServiceProvider\Framework\SymfonyCompilerPassBag;
1112
use Prokl\ServiceProvider\Services\AppKernel;
1213
use Prokl\ServiceProvider\Utils\ErrorScreen;
1314
use Psr\Container\ContainerInterface;
1415
use Psr\Container\ContainerInterface as PsrContainerInterface;
1516
use RuntimeException;
1617
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
17-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1818
use Symfony\Component\Config\ConfigCache;
1919
use Symfony\Component\Config\Loader\DelegatingLoader;
2020
use Symfony\Component\Config\Loader\LoaderInterface;
@@ -31,16 +31,11 @@
3131
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
3232
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
3333
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
34-
use Symfony\Component\DependencyInjection\ServiceLocator;
3534
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
3635
use Symfony\Component\EventDispatcher\EventDispatcher;
37-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
3836
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
3937
use Symfony\Component\Filesystem\Filesystem;
40-
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
4138
use Symfony\Component\HttpKernel\KernelEvents;
42-
use Symfony\Component\Validator\ConstraintValidatorInterface;
43-
use Symfony\Component\Validator\ObjectInitializerInterface;
4439

4540
/**
4641
* Class ServiceProvider
@@ -63,6 +58,7 @@
6358
* @since 04.04.2021 Вынес стандартные compile pass Symfony в отдельный класс.
6459
* @since 14.04.2021 Метод boot бандлов вызывается теперь после компиляции контейнера.
6560
* @since 27.04.2021 Баг-фикс: при скомпилированном контейнере не запускался метод boot бандлов.
61+
* @since 26.06.2021 Автоконфигурация тэгов вынесена в отдельный метод.
6662
*
6763
* @psalm-consistent-constructor
6864
*/
@@ -336,6 +332,7 @@ private function initContainer(string $fileName)
336332
}
337333

338334
// Подключение скомпилированного контейнера.
335+
/** @noinspection PhpIncludeInspection */
339336
require_once $compiledContainerFile;
340337

341338
$classCompiledContainerName = '\\'.$classCompiledContainerName;
@@ -474,6 +471,7 @@ private function loadContainer(string $fileName)
474471
static::$containerBuilder->addCompilerPass($pass);
475472
}
476473

474+
$this->registerAutoconfig();
477475
$this->standartSymfonyPasses();
478476

479477
// Локальные compile pass.
@@ -628,7 +626,7 @@ private function getPathCacheDirectory(string $filename) : string
628626
}
629627

630628
/**
631-
* Стандартные Symfony манипуляции над контейнером.
629+
* Compiler passes.
632630
*
633631
* @return void
634632
*
@@ -638,21 +636,6 @@ private function getPathCacheDirectory(string $filename) : string
638636
*/
639637
private function standartSymfonyPasses(): void
640638
{
641-
/** @var array $autoConfigure Автоконфигурация тэгов. */
642-
$autoConfigure = [
643-
'controller.service_arguments' => AbstractController::class,
644-
'controller.argument_value_resolver' => ArgumentValueResolverInterface::class,
645-
'container.service_locator' => ServiceLocator::class,
646-
'kernel.event_subscriber' => EventSubscriberInterface::class,
647-
'validator.constraint_validator' => ConstraintValidatorInterface::class,
648-
'validator.initializer' => ObjectInitializerInterface::class,
649-
];
650-
651-
foreach ($autoConfigure as $tag => $class) {
652-
static::$containerBuilder->registerForAutoconfiguration($class)
653-
->addTag($tag);
654-
}
655-
656639
// Применяем compiler passes.
657640
foreach ($this->standartCompilerPasses as $pass) {
658641
if (!array_key_exists('pass', $pass) || !class_exists($pass['pass'])) {
@@ -665,6 +648,22 @@ private function standartSymfonyPasses(): void
665648
}
666649
}
667650

651+
/**
652+
* Регистрация автоконфигурируемых тэгов.
653+
*
654+
* @return void
655+
* @throws RuntimeException Когда необходимая зависимость не существует.
656+
*/
657+
private function registerAutoconfig() : void
658+
{
659+
$autoConfigure = new AutoconfigureConfig();
660+
661+
foreach ($autoConfigure->getAutoConfigure() as $tag => $class) {
662+
static::$containerBuilder->registerForAutoconfiguration($class)
663+
->addTag($tag);
664+
}
665+
}
666+
668667
/**
669668
* Загрузка "автономных" бандлов Symfony.
670669
*

0 commit comments

Comments
 (0)