1

I'm developing my first bundle in Symfony2, are services that make use of some parameters in its construction and was working properly if I keep this structure, in AcmeBundle/Resource/config/parameters.yml:

parameters:
    id: IDVAL
    secret: SECRETVAL

services:
    my_service:
        class: MyClass
        arguments: [%id%, %secret%]

But I wonder if there is any way to make use of Interactive Management of the parameters.yml File, for the installation of the bundle through the use of composer AcmeBundle/Resource/config/parameters.yml.dist is made instead of app/config/parameters.yml.dist

Greetings and thanks in advance.

Adexe Rivera
  • 414
  • 7
  • 12

1 Answers1

1

I don't know a way to do that through composer, when you're creating a reusable bundle.
Instead you can configure your bundle based on the config.yml file, using symfony's Configuration Component, and make those parameters required, this way when you're bundle is installed the user, will have to provide those parameters. For you're use case it will be something like:

// in YourBundle\DependencyInjection\Configuration
public function getConfigTreeBuilder(){
  $treeBuilder = new TreeBuilder();
  $rootNode = $treeBuilder->root('your_bundle_alias');
  $rootNode
    ->children()
      ->scalarNode('param1')->isRequired()->end()
      ->scalarNode('param1')->isRequired()->end()
    ->end();
 }

// in YourBundle\DependencyInjection\YourExtension
public function load(array $configs, ContainerBuilder $container){

  $configuration = new Configuration();
  $config = $this->processConfiguration($configuration, $configs);
  // ...
  // get the parameters entered in the config file, and configured above
  $param1 = $config['param1'];
  $param2 = $config['param2'];
  // inject them into my_service
  $container->getDefinition('my_service')
    ->setArguments(array($param1, $param2));
// ...
}

Writing your Bundle Configuration this way will force the user to provide an entry for your bundle in the config.yml file like the following:

your_bundle_alias:
  param1: the_users_value
  param2: another_value
user2268997
  • 1,263
  • 2
  • 14
  • 35