开发者

Symfony 2 configuration for Command

开发者 https://www.devze.com 2023-04-11 22:52 出处:网络
Does anyone know the best way I can create a configuration for a Console/Command-line Command (or indeed in any part of the application?)

Does anyone know the best way I can create a configuration for a Console/Command-line Command (or indeed in any part of the application?)

What I would like to achieve is this (for example)

$ app/console myapp:sync --server=server_2
connecting to "server2.servers.com"...success!
sync completed.

Where myapp is a command line class under /src/myBundle/Com开发者_Python百科mand/SyncCommand.php and in the configuration (app/config/config.yml?) this:

myapp:
    server_1:
        hostname: "server1.servers.com"
        port: 22
    server_2:
        hostname: "server2.servers.com"
        port: 22

For this example it isn't important what the sync does (the code has already been written as SyncCommand.php), I am just interested in knowing in which configuration file to use (I presume either config.yml or parameters.ini) and how to read that config out (treeBuilder? or something like $this->getParameters('myapp')? I suspect I am missing something obvious somewhere.

Thanks!


In the command you have access to the container with getContainer() method if you extend the ContainerAwareCommand class.

You can define your configurations in the parameters section of the services file:

parameters:
    server_1:
        hostname: "server1.servers.com"
        port: 22
    server_2:
        hostname: "server2.servers.com"
        port: 22

Then you should be able to access them with the container (just like described in another question):

$this->getContainer()->getParameter('server_1');
0

精彩评论

暂无评论...
验证码 换一张
取 消