Reputation: 1385
I have a (private) bundle that expects boolean parameter:
$rootNode
->addDefaultsIfNotSet()
->children()
->booleanNode('property_cache_enabled')->defaultTrue()->end()
->end()
When I want it to resolve to kernel.debug
it throws the exception:
Invalid type for path "rvlt_digital_symfony_api.property_cache_enabled". Expected boolean, but got string.
This is the relevant part of configuration:
rvlt_digital_symfony_api:
property_cache_enabled: '%kernel.debug%'
How can this be solved? When I searched for this issue I only found stuff related to environment variables casting; which didn't help as this is not an environment variable.
Upvotes: 2
Views: 1235
Reputation: 47647
You can use the value of %kernel.debug%
to configure your module directly, without having to set explicit configuration for that value.
Change your configuration class so it goes:
class ApplicationConfiguration implements ConfigurationInterface
{
private $debug;
public function __construct($debug)
{
$this->debug = (bool) $debug;
}
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('rvlt_digital_symfony_api');
$treeBuilder->getRootNode()
->addDefaultsIfNotSet()
->children()
->booleanNode('property_cache_enabled')->defaultValue($this->debug)->end()
->end();
return $treeBuilder;
}
}
And your extension class so that when configuration is instantiated, the value for %kernel.debug%
is read from the container and injected into the configuration instance:
class ApplicationExtension extends Extension
{
public function getConfiguration(array $config, ContainerBuilder $container)
{
return new Configuration($container->getParameter('kernel.debug'));
}
}
This way, if you do not set up the configuration for rvlt_digital_symfony_api.property_cache_enabled
it simply matches the app's %kernel.debug%
setting, which is what you wanted in the first place. (You can still configure this value and override the default, though).
This use case is explicitly talked about in the docs here: Using Parameters within a Dependency Injection Class
Upvotes: 1