|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use Symfony\Component\Config\Definition\ArrayShapeGenerator; |
| 15 | +use Symfony\Component\Config\Definition\ConfigurationInterface; |
| 16 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 17 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 18 | +use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface; |
| 19 | +use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
| 20 | +use Symfony\Component\DependencyInjection\Loader\Configurator\AppReference; |
| 21 | +use Symfony\Component\Routing\Loader\Configurator\RoutesReference; |
| 22 | + |
| 23 | +/** |
| 24 | + * @internal |
| 25 | + */ |
| 26 | +class PhpConfigReferenceDumpPass implements CompilerPassInterface |
| 27 | +{ |
| 28 | + private const REFERENCE_TEMPLATE = <<<'PHP' |
| 29 | + <?php |
| 30 | +
|
| 31 | + // This file is auto-generated and is for apps only. Bundles SHOULD NOT rely on its content. |
| 32 | +
|
| 33 | + namespace Symfony\Component\DependencyInjection\Loader\Configurator; |
| 34 | +
|
| 35 | + {APP_TYPES} |
| 36 | + final class App extends AppReference |
| 37 | + { |
| 38 | + {APP_PARAM} |
| 39 | + public static function config(array $config): array |
| 40 | + { |
| 41 | + return parent::config($config); |
| 42 | + } |
| 43 | + } |
| 44 | +
|
| 45 | + namespace Symfony\Component\Routing\Loader\Configurator; |
| 46 | +
|
| 47 | + {ROUTES_TYPES} |
| 48 | + final class Routes extends RoutesReference |
| 49 | + { |
| 50 | + {ROUTES_PARAM} |
| 51 | + public static function config(array $config): array |
| 52 | + { |
| 53 | + return parent::config($config); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + PHP; |
| 58 | + |
| 59 | + private const WHEN_ENV_APP_TEMPLATE = <<<'PHPDOC' |
| 60 | +
|
| 61 | + * "when@{ENV}"?: array{ |
| 62 | + * imports?: ImportsConfig, |
| 63 | + * parameters?: ParametersConfig, |
| 64 | + * services?: ServicesConfig,{SHAPE} |
| 65 | + * }, |
| 66 | + PHPDOC; |
| 67 | + |
| 68 | + private const ROUTES_TYPES_TEMPLATE = <<<'PHPDOC' |
| 69 | +
|
| 70 | + * @psalm-type RoutesConfig = array{{SHAPE} |
| 71 | + * ...<string, RouteConfig|ImportConfig|AliasConfig> |
| 72 | + * } |
| 73 | + */ |
| 74 | + PHPDOC; |
| 75 | + |
| 76 | + private const WHEN_ENV_ROUTES_TEMPLATE = <<<'PHPDOC' |
| 77 | +
|
| 78 | + * "when@{ENV}"?: array<string, RouteConfig|ImportConfig|AliasConfig>, |
| 79 | + PHPDOC; |
| 80 | + |
| 81 | + public function __construct( |
| 82 | + private string $referenceFile, |
| 83 | + private array $bundlesDefinition, |
| 84 | + ) { |
| 85 | + } |
| 86 | + |
| 87 | + public function process(ContainerBuilder $container): void |
| 88 | + { |
| 89 | + $knownEnvs = $container->hasParameter('.container.known_envs') ? $container->getParameter('.container.known_envs') : [$container->getParameter('kernel.environment')]; |
| 90 | + $knownEnvs = array_unique($knownEnvs); |
| 91 | + sort($knownEnvs); |
| 92 | + $extensionsPerEnv = []; |
| 93 | + $appTypes = ''; |
| 94 | + |
| 95 | + $anyEnvExtensions = []; |
| 96 | + foreach ($this->bundlesDefinition as $bundle => $envs) { |
| 97 | + if (!$extension = (new $bundle())->getContainerExtension()) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + if (!$configuration = $this->getConfiguration($extension, $container)) { |
| 101 | + continue; |
| 102 | + } |
| 103 | + $anyEnvExtensions[$bundle] = $extension; |
| 104 | + $type = $this->camelCase($extension->getAlias()).'Config'; |
| 105 | + $appTypes .= \sprintf("\n * @psalm-type %s = %s", $type, ArrayShapeGenerator::generate($configuration->getConfigTreeBuilder()->buildTree())); |
| 106 | + |
| 107 | + foreach ($knownEnvs as $env) { |
| 108 | + if ($envs[$env] ?? $envs['all'] ?? false) { |
| 109 | + $extensionsPerEnv[$env][] = $extension; |
| 110 | + } else { |
| 111 | + unset($anyEnvExtensions[$bundle]); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + krsort($extensionsPerEnv); |
| 116 | + |
| 117 | + $r = new \ReflectionClass(AppReference::class); |
| 118 | + |
| 119 | + if (false === $i = strpos($phpdoc = $r->getDocComment(), "\n */")) { |
| 120 | + throw new \LogicException(\sprintf('Cannot insert config shape in "%s".', AppReference::class)); |
| 121 | + } |
| 122 | + $appTypes = substr_replace($phpdoc, $appTypes, $i, 0); |
| 123 | + |
| 124 | + if (false === $i = strpos($phpdoc = $r->getMethod('config')->getDocComment(), "\n * ...<string, ExtensionType|array{")) { |
| 125 | + throw new \LogicException(\sprintf('Cannot insert config shape in "%s".', AppReference::class)); |
| 126 | + } |
| 127 | + $appParam = substr_replace($phpdoc, $this->getShapeForExtensions($anyEnvExtensions, $container), $i, 0); |
| 128 | + $i += \strlen($appParam) - \strlen($phpdoc); |
| 129 | + |
| 130 | + foreach ($extensionsPerEnv as $env => $extensions) { |
| 131 | + $appParam = substr_replace($appParam, strtr(self::WHEN_ENV_APP_TEMPLATE, [ |
| 132 | + '{ENV}' => $env, |
| 133 | + '{SHAPE}' => $this->getShapeForExtensions($extensions, $container, ' '), |
| 134 | + ]), $i, 0); |
| 135 | + } |
| 136 | + |
| 137 | + $r = new \ReflectionClass(RoutesReference::class); |
| 138 | + |
| 139 | + if (false === $i = strpos($phpdoc = $r->getDocComment(), "\n * @psalm-type RoutesConfig = ")) { |
| 140 | + throw new \LogicException(\sprintf('Cannot insert config shape in "%s".', RoutesReference::class)); |
| 141 | + } |
| 142 | + $routesTypes = ''; |
| 143 | + foreach ($knownEnvs as $env) { |
| 144 | + $routesTypes .= strtr(self::WHEN_ENV_ROUTES_TEMPLATE, ['{ENV}' => $env]); |
| 145 | + } |
| 146 | + if ('' !== $routesTypes) { |
| 147 | + $routesTypes = strtr(self::ROUTES_TYPES_TEMPLATE, ['{SHAPE}' => $routesTypes]); |
| 148 | + $routesTypes = substr_replace($phpdoc, $routesTypes, $i); |
| 149 | + } |
| 150 | + |
| 151 | + $configReference = strtr(self::REFERENCE_TEMPLATE, [ |
| 152 | + '{APP_TYPES}' => $appTypes, |
| 153 | + '{APP_PARAM}' => $appParam, |
| 154 | + '{ROUTES_TYPES}' => $routesTypes, |
| 155 | + '{ROUTES_PARAM}' => $r->getMethod('config')->getDocComment(), |
| 156 | + ]); |
| 157 | + |
| 158 | + $dir = \dirname($this->referenceFile); |
| 159 | + if (is_dir($dir) && is_writable($dir) && (!is_file($this->referenceFile) || file_get_contents($this->referenceFile) !== $configReference)) { |
| 160 | + file_put_contents($this->referenceFile, $configReference); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private function camelCase(string $input): string |
| 165 | + { |
| 166 | + $output = ucfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $input)))); |
| 167 | + |
| 168 | + return preg_replace('#\W#', '', $output); |
| 169 | + } |
| 170 | + |
| 171 | + private function getConfiguration(ExtensionInterface $extension, ContainerBuilder $container): ?ConfigurationInterface |
| 172 | + { |
| 173 | + return match (true) { |
| 174 | + $extension instanceof ConfigurationInterface => $extension, |
| 175 | + $extension instanceof ConfigurationExtensionInterface => $extension->getConfiguration([], $container), |
| 176 | + default => null, |
| 177 | + }; |
| 178 | + } |
| 179 | + |
| 180 | + private function getShapeForExtensions(array $extensions, ContainerBuilder $container, string $indent = ''): string |
| 181 | + { |
| 182 | + $shape = ''; |
| 183 | + foreach ($extensions as $extension) { |
| 184 | + if ($this->getConfiguration($extension, $container)) { |
| 185 | + $type = $this->camelCase($extension->getAlias()).'Config'; |
| 186 | + $shape .= \sprintf("\n * %s%s?: %s,", $indent, $extension->getAlias(), $type); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return $shape; |
| 191 | + } |
| 192 | +} |
0 commit comments