1

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/Command/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!

mogoman
  • 2,286
  • 24
  • 28

1 Answers1

7

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');
Community
  • 1
  • 1
Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125