|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProklUng\ContainerBoilerplate\DI; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use RuntimeException; |
| 7 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 8 | +use Symfony\Component\HttpKernel\Bundle\Bundle; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class LoaderBundles |
| 12 | + * |
| 13 | + * @since 19.08.2021 |
| 14 | + */ |
| 15 | +class LoaderBundles |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var array $bundles |
| 19 | + */ |
| 20 | + private $bundles = []; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var ContainerBuilder $containerBuilder Целевой контейнер. |
| 24 | + */ |
| 25 | + private $containerBuilder; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var string $environment Окружение. |
| 29 | + */ |
| 30 | + private $environment; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param ContainerBuilder $containerBuilder Целевой контейнер. |
| 34 | + * @param string $environment Окружение. |
| 35 | + */ |
| 36 | + public function __construct( |
| 37 | + ContainerBuilder $containerBuilder, |
| 38 | + string $environment |
| 39 | + ) { |
| 40 | + $this->containerBuilder = $containerBuilder; |
| 41 | + $this->environment = $environment; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Загрузка из файла. |
| 46 | + * |
| 47 | + * @param string $configPath Путь к файлу с конфигурацией бандлов. |
| 48 | + * |
| 49 | + * @return array |
| 50 | + * @throws RuntimeException |
| 51 | + */ |
| 52 | + public function fromFile(string $configPath) : array |
| 53 | + { |
| 54 | + $bundles = $this->loadConfig($configPath); |
| 55 | + |
| 56 | + return $this->loadBundles($bundles); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Загрузка из файла. |
| 61 | + * |
| 62 | + * @param array $config Конфиг в виде массива. |
| 63 | + * |
| 64 | + * @return array |
| 65 | + * @throws InvalidArgumentException |
| 66 | + */ |
| 67 | + public function fromArray(array $config) : array |
| 68 | + { |
| 69 | + return $this->loadBundles($config); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Загрузка конфига бандлов из файла. |
| 74 | + * |
| 75 | + * @param string $configPath Путь к файлу с конфигурацией бандлов. |
| 76 | + * |
| 77 | + * @return array |
| 78 | + * @throws RuntimeException |
| 79 | + */ |
| 80 | + private function loadConfig(string $configPath) : array |
| 81 | + { |
| 82 | + if (!file_exists($configPath)) { |
| 83 | + throw new RuntimeException('Config bundles file' . $configPath . ' not exists.'); |
| 84 | + } |
| 85 | + |
| 86 | + $this->bundles = (array)require $configPath; |
| 87 | + |
| 88 | + return $this->bundles; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Загрузка и инициализация бандлов. |
| 93 | + * |
| 94 | + * @param array $config Массив с конфигом бандлов. |
| 95 | + * |
| 96 | + * @return array |
| 97 | + * @throws InvalidArgumentException |
| 98 | + */ |
| 99 | + private function loadBundles(array $config) : array |
| 100 | + { |
| 101 | + $resultBundles = []; |
| 102 | + |
| 103 | + foreach ($config as $bundleClass => $envs) { |
| 104 | + if (!class_exists($bundleClass)) { |
| 105 | + throw new InvalidArgumentException( |
| 106 | + sprintf('Bundle class %s not exist.', $bundleClass) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + if (!array_key_exists($this->environment, (array)$envs) |
| 111 | + && |
| 112 | + !array_key_exists('all', (array)$envs) |
| 113 | + ) { |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + if (!method_exists($bundleClass, 'getContainerExtension')) { |
| 118 | + throw new InvalidArgumentException( |
| 119 | + sprintf('Bundle %s dont have implemented getContainerExtension method.', $bundleClass) |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @var Bundle $bundle Бандл. |
| 125 | + */ |
| 126 | + $bundle = new $bundleClass; |
| 127 | + |
| 128 | + if ((bool)$_ENV['DEBUG'] === true) { |
| 129 | + $this->containerBuilder->addObjectResource($bundle); |
| 130 | + } |
| 131 | + |
| 132 | + $extension = $bundle->getContainerExtension(); |
| 133 | + if ($extension !== null) { |
| 134 | + $this->containerBuilder->registerExtension($extension); |
| 135 | + $bundle->build($this->containerBuilder); |
| 136 | + } |
| 137 | + |
| 138 | + $resultBundles[static::class][$bundle->getName()] = $bundle; |
| 139 | + } |
| 140 | + |
| 141 | + return $resultBundles; |
| 142 | + } |
| 143 | +} |
0 commit comments