Myślę, że jeśli nie przekażesz/ustawisz parametru, Symfony będzie narzekać na zależność od usługi. Chcesz, aby parametr był opcjonalny, tak aby nie był wymagany zawsze w pliku config.yml. I chcesz użyć tego parametru, gdy jest ustawiony.
Jest moje rozwiązanie:
# src/Acme/HelloBundle/Resources/config/services.yml
parameters:
my_parameter:
services:
my_mailer:
class: "%my_mailer.class%"
arguments: ["%my_parameter%"]
A potem
# you-bundle-dir/DependencyInjection/Configuration.php
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('you_bundle_ns');
// This is for wkhtmltopdf configuration
$rootNode
->children()
->scalarNode('my_parameter')->defaultNull()->end()
->end();
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder;
}
A potem
# you-bundle-dir/DependencyInjection/YourBundleExtension.php
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$container->setParameter(
'you_bundle_ns.your_parameter',
isset($$config['you_bundle_ns']['your_parameter'])?$$config['you_bundle_ns']['your_parameter']:null
);
}
można sprawić, że Twój parametr opcjonalny, dając wartość domyślną na „% parametru% '
Daj mi znać, jeśli masz lepsze alternatywy.
Jakie są zalety opcjonalnego parametru? Parametry służą do zmiany konfiguracji między środowiskami. Możesz zmienić konfigurację w swoich środowiskach, ale to nie będzie tak czyste. – Ryan