phpstan-symfony icon indicating copy to clipboard operation
phpstan-symfony copied to clipboard

Support APP_ENV for determining container filename

Open d42ohpaz opened this issue 5 years ago • 4 comments

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.

d42ohpaz avatar Oct 20 '20 19:10 d42ohpaz

<?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

pscheit-lillydoo avatar Oct 28 '20 14:10 pscheit-lillydoo

$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.

d42ohpaz avatar Oct 28 '20 15:10 d42ohpaz

sure but the xml file changes appDevDebugProjectContainer vs appTestDebugProjectContainer

pscheit-lillydoo avatar Oct 28 '20 15:10 pscheit-lillydoo

You're right. Nothing a ucfirst(strtolower($env)) couldn't fix. :)

d42ohpaz avatar Oct 28 '20 15:10 d42ohpaz

I can see there are solutions to this problem without modifying this extension, so I'm closing the issue. Thanks for understanding.

ondrejmirtes avatar Jan 16 '23 21:01 ondrejmirtes

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.

github-actions[bot] avatar Feb 17 '23 00:02 github-actions[bot]