Definition::addMethodCall Is not calling the set method

521 Views Asked by At

I am trying to upgrade Symfony from 2.3 to 3.4. At the moment I am try to fix an issue with the dependancy injection. It seems like the old version of the DependencyInjection\Definition class would call the Factory method when the factory was set, but this has changed?

I have tried to using this guides to upgrade the code: https://gist.github.com/mickaelandrieu/5211d0047e7a6fbff925#dependencyinjection. But, I believe that the behaviour of the class has changed.

So this is the original code:

public function getSymfonyValue()
{
    $def = new \Symfony\Component\DependencyInjection\Definition("Zend_Config");
    $def->setFactoryClass(__CLASS__);
    $def->setFactoryMethod('valueFactory');
    $def->addArgument($this->_configPath);
    return $def;
}

which invokes this method:

public function valueFactory($configPath){
    ...
    return $value;
}

I have updated the body of the method to this:

$def = new \Symfony\Component\DependencyInjection\Definition("Zend_Config");
$def->setFactory(__CLASS__);
$def->addMethodCall('valueFactory', [$this->_configPath]);
return $def;

I expected the code to call the function valueFactory, but this function is never called. This could be because the old code does not have a container? I am not sure

1

There are 1 best solutions below

3
Vincent On

The setFactory method expect a callable, not just a class. The addMethodCall will call a function on the service itself, not his factory.

What you want to do is :

$def->setFactory(array(__CLASS__, 'valueFactory'))
$def->setArguments(array($this->_configPath))

It means, you will create the service by calling the factory method, with the given arguments.