Support APP_ENV for determining container filename
I saw https://github.com/phpstan/phpstan-symfony/issues/61 and that works as an okay workaround, but I would posit that developers shouldn't have to carry the burden of dynamically generating a configuration file or symlinking a to a specific file when an extension-level solution could just as easily be introduced in the code.
With that said, my suggestion would be to look for APP_ENV in the environment, and if it exists, use that to determine the container_xml_path automatically. Then we developers can omit that configuration option if we wanted to, or if we absolutely had to, we could still provide it and it would override automatically determining the value (i.e., use current logic).
In other words, if I - the developer - omit container_xml_path, and provide APP_ENV=test, then this extension would produce /path/to/my/var/cache/test/Project_KernelTestContainer.xml file. But, if I omit APP_ENV=test or I provide container_xml_path, then the extension would use the current logic.
The use case for this is to be able to run phpstan in both development and CI environments.
<?php
$env = getenv('APP_ENV');
$xmlContainerFile = $env === 'dev'
? dirname(dirname(__DIR__)) . '/app/cache/dev/appDevDebugProjectContainer.xml'
: dirname(dirname(__DIR__)) . '/app/cache/test/appTestDebugProjectContainer.xml';
if (!file_exists($xmlContainerFile)) {
throw new \Exception(
sprintf(
'phpstan depends on the meta information the symfony dependency injection that the compiler pass writes' . PHP_EOL
. 'The meta xml file could not be found: %s' . PHP_EOL
. 'To compile the container do a cache:clear in the current env (%s) with debug: true!',
$xmlContainerFile,
$env
)
);
}
return [
'parameters' => [
'symfony' => [
'container_xml_path' => $xmlContainerFile
]
]
];
then reference the file in includes in your phpstan.neon
$xmlContainerFile = $env === 'dev'
? dirname(dirname(__DIR__)) . '/app/cache/dev/appDevDebugProjectContainer.xml'
: dirname(dirname(__DIR__)) . '/app/cache/test/appTestDebugProjectContainer.xml';
It would be much more encompassing to just use $env directly (after ensuring it's safe to use):
$env = getenv('APP_ENV');
$xmlContainerFile = realpath(dirname(dirname(__DIR__)) . '/app/cache/' . $env . '/appDevDebugProjectContainer.xml');
if (strpos($xmlContainerFile, dirname(dirname(__DIR__))) !== 0) {
throw new \UnexpectedValueException(sprintf('APP_ENV must be an environment name, not path; got "%s"', $env));
}
// Continue with rest of code
Use case: we use multiple non-prod environments: dev, stage, and test. May not be standard all around, but it is what it is.
sure but the xml file changes appDevDebugProjectContainer vs appTestDebugProjectContainer
You're right. Nothing a ucfirst(strtolower($env)) couldn't fix. :)
I can see there are solutions to this problem without modifying this extension, so I'm closing the issue. Thanks for understanding.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.