|
6 | 6 | use Nette\DI\Config\Loader; |
7 | 7 | use Nette\DI\Container as OriginalNetteContainer; |
8 | 8 | use Nette\DI\ContainerLoader; |
| 9 | +use Nette\DI\Definitions\Statement; |
| 10 | +use Nette\Neon\Entity; |
9 | 11 | use Override; |
10 | 12 | use PHPStan\File\CouldNotReadFileException; |
11 | 13 | use PHPStan\File\CouldNotWriteFileException; |
12 | 14 | use PHPStan\File\FileReader; |
13 | 15 | use PHPStan\File\FileWriter; |
14 | 16 | use PHPStan\Turbo\TurboExtensionEnabler; |
| 17 | +use Throwable; |
| 18 | +use function array_fill_keys; |
| 19 | +use function array_intersect_key; |
15 | 20 | use function array_keys; |
16 | 21 | use function count; |
17 | 22 | use function error_reporting; |
18 | 23 | use function explode; |
| 24 | +use function file_get_contents; |
19 | 25 | use function hash_file; |
20 | 26 | use function implode; |
21 | 27 | use function in_array; |
| 28 | +use function is_array; |
22 | 29 | use function is_dir; |
23 | 30 | use function is_file; |
| 31 | +use function is_string; |
24 | 32 | use function ksort; |
| 33 | +use function preg_match; |
| 34 | +use function preg_match_all; |
25 | 35 | use function restore_error_handler; |
26 | 36 | use function set_error_handler; |
27 | 37 | use function sprintf; |
| 38 | +use function str_contains; |
28 | 39 | use function str_ends_with; |
29 | 40 | use function substr; |
30 | 41 | use function time; |
|
38 | 49 | final class Configurator extends \Nette\Bootstrap\Configurator |
39 | 50 | { |
40 | 51 |
|
| 52 | + /** |
| 53 | + * Env vars PHPStan reads at build time in a CompilerExtension (FnsrExtension reads PHPSTAN_FNSR), |
| 54 | + * so the container depends on them and they must stay in the cache key. ContainerCacheKeyEnvGuardTest |
| 55 | + * fails if a CompilerExtension reads an env var not listed here or referenced via %env.*%. |
| 56 | + */ |
| 57 | + public const BUILD_TIME_ENV_VARIABLES = ['PHPSTAN_FNSR']; |
| 58 | + |
41 | 59 | /** @var string[] */ |
42 | 60 | private array $allConfigFiles = []; |
43 | 61 |
|
@@ -97,6 +115,19 @@ public function loadContainer(): string |
97 | 115 | // make sure invocations via blackfire use the same container |
98 | 116 | unset($staticParameters['env']['BLACKFIRE_AGENT_SOCKET']); |
99 | 117 |
|
| 118 | + // Keep only the env vars the container actually depends on in the cache key, so unrelated env |
| 119 | + // changes (CI/shell) don't force a full recompile - phpstan/phpstan#14072. The full env stays |
| 120 | + // in the container parameters, so %env.*% resolution is unaffected; this only narrows the key. |
| 121 | + if (isset($staticParameters['env'])) { |
| 122 | + $relevantEnvVariableNames = $this->relevantEnvVariableNamesForCacheKey(); |
| 123 | + if ($relevantEnvVariableNames !== null) { |
| 124 | + $staticParameters['env'] = array_intersect_key( |
| 125 | + $staticParameters['env'], |
| 126 | + array_fill_keys($relevantEnvVariableNames, true), |
| 127 | + ); |
| 128 | + } |
| 129 | + } |
| 130 | + |
100 | 131 | $containerKey = [ |
101 | 132 | $staticParameters, |
102 | 133 | array_keys($this->dynamicParameters), |
@@ -231,6 +262,96 @@ public function createContainer(bool $initialize = true): OriginalNetteContainer |
231 | 262 | return $container; |
232 | 263 | } |
233 | 264 |
|
| 265 | + /** |
| 266 | + * Env vars that can change the generated container, so they must stay in the cache key: every |
| 267 | + * %env.NAME% referenced across the loaded configs, plus BUILD_TIME_ENV_VARIABLES. Returns null |
| 268 | + * when a config references the whole %env% array (or its references can't be safely enumerated), |
| 269 | + * in which case all of it must be kept. |
| 270 | + * |
| 271 | + * References are enumerated from the config as parsed by PHPStan's own NeonAdapter - the same |
| 272 | + * parser the container compiler uses - rather than from raw config text: comments are ignored and |
| 273 | + * service entities become Statements, so a %env.NAME% in a parameter value, a service argument or |
| 274 | + * a factory is found the same way it is at compile time. The %env.NAME% grammar mirrors Nette's |
| 275 | + * parameter-name grammar (%([\w.-]*)%), so dashed names like %env.MY-VAR% are handled too. |
| 276 | + * |
| 277 | + * @return list<string>|null |
| 278 | + */ |
| 279 | + private function relevantEnvVariableNamesForCacheKey(): ?array |
| 280 | + { |
| 281 | + $names = self::BUILD_TIME_ENV_VARIABLES; |
| 282 | + $adapter = new NeonAdapter([]); |
| 283 | + |
| 284 | + foreach ($this->allConfigFiles as $file) { |
| 285 | + $contents = @file_get_contents($file); |
| 286 | + if ($contents === false || !str_contains($contents, '%env')) { |
| 287 | + continue; |
| 288 | + } |
| 289 | + |
| 290 | + try { |
| 291 | + $data = $adapter->load($file); |
| 292 | + } catch (Throwable) { |
| 293 | + // A config we can't parse as NEON here (e.g. a .php config) might reference any env |
| 294 | + // var, so we can't prove which ones the container needs: keep the whole environment. |
| 295 | + return null; |
| 296 | + } |
| 297 | + |
| 298 | + $referencesWholeEnv = false; |
| 299 | + $referencedNames = $this->collectReferencedEnvVariableNames($data, $referencesWholeEnv); |
| 300 | + if ($referencesWholeEnv) { |
| 301 | + return null; |
| 302 | + } |
| 303 | + |
| 304 | + foreach ($referencedNames as $name) { |
| 305 | + $names[] = $name; |
| 306 | + } |
| 307 | + } |
| 308 | + |
| 309 | + return $names; |
| 310 | + } |
| 311 | + |
| 312 | + /** |
| 313 | + * Recursively collect the env-variable names referenced by %env.NAME% in a parsed config node. |
| 314 | + * Recurses into arrays and into the service definitions the NeonAdapter produces (Statements, and |
| 315 | + * any remaining Neon entities) so references in service arguments and factories are not missed. |
| 316 | + * Sets $referencesWholeEnv on a bare %env% (the whole environment), which forces keeping all of it. |
| 317 | + * |
| 318 | + * @param mixed $node |
| 319 | + * @return list<string> |
| 320 | + */ |
| 321 | + private function collectReferencedEnvVariableNames($node, bool &$referencesWholeEnv): array |
| 322 | + { |
| 323 | + if (is_string($node)) { |
| 324 | + if (preg_match('~%env%~', $node) === 1) { |
| 325 | + $referencesWholeEnv = true; |
| 326 | + } |
| 327 | + |
| 328 | + if (preg_match_all('~%env\.([\w.-]+)%~', $node, $matches) > 0) { |
| 329 | + return $matches[1]; |
| 330 | + } |
| 331 | + |
| 332 | + return []; |
| 333 | + } |
| 334 | + |
| 335 | + if ($node instanceof Statement) { |
| 336 | + $node = [$node->getEntity(), $node->arguments]; |
| 337 | + } elseif ($node instanceof Entity) { |
| 338 | + $node = [$node->value, $node->attributes]; |
| 339 | + } |
| 340 | + |
| 341 | + if (!is_array($node)) { |
| 342 | + return []; |
| 343 | + } |
| 344 | + |
| 345 | + $names = []; |
| 346 | + foreach ($node as $value) { |
| 347 | + foreach ($this->collectReferencedEnvVariableNames($value, $referencesWholeEnv) as $name) { |
| 348 | + $names[] = $name; |
| 349 | + } |
| 350 | + } |
| 351 | + |
| 352 | + return $names; |
| 353 | + } |
| 354 | + |
234 | 355 | /** |
235 | 356 | * @return string[] |
236 | 357 | */ |
|
0 commit comments