Search code examples
phpsymfonyparameterscompiler-constructioncontainers

Symfony 2.8 : dynamic containers parameters at compiler pass and used in security.yml


I'm facing an issue I can't solve by myself in my Symfony 2.8 API project:

I have some paramter loaded at compiler pass in the DependencyInjection of one of my bundle:

class ParametersCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        //loads api version from database when container gets built.
        $api_version = $container->get('doctrine')->getRepository('MyAppEntityBundle:Parameter')
            ->findOneBy(['name' => 'api_version']);
        if (!$api_version) {
            throw new MyAppException('api_version couldnt be found in database during container build process.');
        }
        $container->setParameter('api_version', $api_version);
    }
}

I dont want this parameter in paramters_*.yml files, to ensure it's ALWAYS retrieved from db + I use it in security.yml file, such as:

security:
    firewalls:
        api:
            pattern:   ^/api/%api_version%/

How can I achieved that ?

Apparently, $container->setParameter('name', $value); doesnt work if there isnt a parameter in parameter_*.yml file with dummy value in parameters.yml + I need to set these values BEFORE security.yml gets parsed and loaded. I have look into symfony core for hours, spotted where security config is loaded from kernel->boot() process but I dont see how to achieve what I want properly.

Any help or hints on the proper method would be highly appreciated :)


Solution

  • It is clearly not a good practice, as I stated in response to previous comments, I admit it, it is overkill in complexity for a simple API version management issue. I end up setting the parameter in parameters.yml file so when security.yml file is processed, it is not a problem, and placed my API management logic in the build process of the CI. Makes more sense. @Mocrates Thanks a lot for your answer :)