Symfony2: How to inject ALL parameters in a service?

后端 未结 7 850
既然无缘
既然无缘 2020-12-28 14:14

How can I inject ALL parameters in a service?

I know I can do: arguments: [%some.key%] which will pass the parameters: some.key: \"value\"

相关标签:
7条回答
  • 2020-12-28 15:13

    AppKernel would work but it's even worse (from a scope perspective) than injecting the container since the kernel has even more stuff in it.

    You can look at xxxProjectContainer in your cache directory. Turns out that the assorted parameters are compiled directly into it as a big array. So you could inject the container and then just pull out the parameters. Violates the letter of the law but not the spirit of the law.

    class MyService {
        public function __construct($container) {
            $this->parameters = $container->parameters; // Then discard container to preclude temptation
    

    And just sort of messing around I found I could do this:

        $container = new \arbiterDevDebugProjectContainer();
        echo 'Parameter Count ' . count($container->parameters) . "\n";
    

    So you could actually create a service that had basically a empty copy of the master container and inject it just to get the parameters. Have to take into account the dev/debug flags which might be a pain.

    I suspect you could also do it with a compiler pass but have never tried.

    0 讨论(0)
提交回复
热议问题