在我的Bundle扩展中,我将方法调用(动态地,基于配置)添加到我的服务定义my.service
:
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
// ...
// Get the defintion
$definition = $container->getDefinition('my.service');
// Dynamically add method calls to the definition
foreach($config['options'] as $name => $value) {
$definition->addMethodCall('set'.ucfirst($name), array($value));
}
// ...
}
如果定义中不存在该方法,我想不要调用addMethodCall
。有没有办法检查这个?
答案 0 :(得分:3)
如果您的服务类具有这些方法的定义,我假设您只想在服务类上添加方法调用
$serviceMethods = get_class_methods($definition->getClass());
//loop on your added methods
$method = 'set'.ucfirst($name);
if(in_array($method, $serviceMethods))
{
$definition->addMethodCall($method, array($value));
}
答案 1 :(得分:2)
你能不能使用..
$class = $definition->getclass();
然后在添加之前检查方法是否存在..
foreach($config['options'] as $name => $value) {
$method = 'set'.ucfirst($name);
if (method_exists($class, $method)
{
$definition->addMethodCall('set'.ucfirst($name), array($value));
}
}