Reputation: 3972
I have defined in app/config.yml sample:
module_my_module
key: value1
key2: value2
And a bundle with DependecyInjection Configuration.php:
<?php
namespace Modules\Bundle\MyModuleBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('module_configuration');
$rootNode->children()
->scalarNode('key')->defaultValue('')->end()
->scalarNode('key2')->defaultValue('')->end()
->end()
;
return $treeBuilder;
}
}
A problem is how to call config.Bundle/Command class:
namespace Modules\Bundle\MyBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
//use \Modules\Bundle\NodeSocketBundle\DependencyInjection as DependencyInjection;
class MyCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getParams("key.val"); // GEt a key from config.yml
}
}
Config.yml:
module_configuration
key: val
Upvotes: 1
Views: 354
Reputation: 39380
You must inject your params in the Container in the Extension
Bundle Class definition.
In your case you must have something similar to:
<?php
namespace Modules\Bundle\MyModuleBundle\DependencyInjection;
class MyModuleExtension extends Extension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('...');
$container->setParameter('my_first_key', $config['key']);
$container->setParameter('my_second_key', $config['key2']);
}
}
Then you can access to the value, in your Command
class as:
class MyCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getContainer()->getParameter("my_first_key");
}
}
Hope this help
Upvotes: 1