How can i inject dependencies to Symfony Console commands?

后端 未结 7 1018
醉话见心
醉话见心 2020-12-09 08:00

I\'m writing an open source application uses some Symfony components, and using Symfony Console component for interacting with shell.

But, i need to inject dependen

7条回答
  •  抹茶落季
    2020-12-09 08:18

    It is best not to inject the container itself but to inject services from the container into your object. If you're using Symfony2's container, then you can do something like this:

    MyBundle/Resources/config/services (or wherever you decide to put this file):

    ...
        
            
            
                 
            
            
        
    ...
    

    Then your command class should look like this:

    someService = $someService;
       }
    ...
    

    I know you said you're not using the dependency injection container, but in order to implement the above answer from @ramon, you have to use it. At least this way your command can be properly unit tested.

提交回复
热议问题