*/ class FactoryResolver implements DefinitionResolver { /** * @var ContainerInterface */ private $container; /** * The resolver needs a container. This container will be passed to the factory as a parameter * so that the factory can access other entries of the container. * * @param ContainerInterface $container */ public function __construct(ContainerInterface $container) { $this->container = $container; } /** * Resolve a factory definition to a value. * * This will call the callable of the definition. * * @param FactoryDefinition $definition * * {@inheritdoc} */ public function resolve(Definition $definition, array $parameters = []) { $this->assertIsFactoryDefinition($definition); $callable = $definition->getCallable(); if (! is_callable($callable)) { throw new DefinitionException(sprintf( 'The factory definition "%s" is not callable', $definition->getName() )); } return call_user_func($callable, $this->container); } /** * {@inheritdoc} */ public function isResolvable(Definition $definition, array $parameters = []) { $this->assertIsFactoryDefinition($definition); return true; } private function assertIsFactoryDefinition(Definition $definition) { if (!$definition instanceof FactoryDefinition) { throw new \InvalidArgumentException(sprintf( 'This definition resolver is only compatible with FactoryDefinition objects, %s given', get_class($definition) )); } } }