*/ class StringResolver implements DefinitionResolver { /** * @var ContainerInterface */ private $container; /** * The resolver needs a container. * This container will be used to get the entry to which the alias points to. * * @param ContainerInterface $container */ public function __construct(ContainerInterface $container) { $this->container = $container; } /** * Resolve a value definition to a value. * * A value definition is simple, so this will just return the value of the ValueDefinition. * * @param StringDefinition $definition * * {@inheritdoc} */ public function resolve(Definition $definition, array $parameters = []) { $this->assertIsStringDefinition($definition); $expression = $definition->getExpression(); $result = preg_replace_callback('#\{([^\{\}]+)\}#', function (array $matches) use ($definition) { try { return $this->container->get($matches[1]); } catch (NotFoundException $e) { throw new DependencyException(sprintf( "Error while parsing string expression for entry '%s': %s", $definition->getName(), $e->getMessage() ), 0, $e); } }, $expression); if ($result === null) { throw new \RuntimeException(sprintf('An unknown error occurred while parsing the string definition: \'%s\'', $expression)); } return $result; } /** * {@inheritdoc} */ public function isResolvable(Definition $definition, array $parameters = []) { $this->assertIsStringDefinition($definition); return true; } private function assertIsStringDefinition(Definition $definition) { if (!$definition instanceof StringDefinition) { throw new \InvalidArgumentException(sprintf( 'This definition resolver is only compatible with StringDefinition objects, %s given', get_class($definition) )); } } }