# cookiebot/3.11.2/addons/inc/Dependencies/DI/Definition/Resolver/StringResolver.php

Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA &amp; Google Consent Mode, version 3.11.2. 96 lines.

- Page: https://pluginprobe.com/plugins/cookiebot/3.11.2/code/addons/inc/Dependencies/DI/Definition/Resolver/StringResolver.php
- Raw: https://pluginprobe.com/plugins/cookiebot/3.11.2/raw/addons/inc/Dependencies/DI/Definition/Resolver/StringResolver.php
- Modified: 2021-03-23T11:49:48+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/cookiebot/3.11.2/code/addons/inc/Dependencies/DI/Definition/Resolver/StringResolver.php#L10-L20`.

```php
<?php
/**
 * PHP-DI
 *
 * @link      http://mnapoli.github.com/PHP-DI/
 * @copyright Matthieu Napoli (http://mnapoli.fr/)
 * @license   http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
 */

namespace Cybot\Dependencies\DI\Definition\Resolver;

use Cybot\Dependencies\DI\Definition\Definition;
use Cybot\Dependencies\DI\Definition\StringDefinition;
use Cybot\Dependencies\DI\DependencyException;
use Cybot\Dependencies\DI\NotFoundException;
use Cybot\Dependencies\Interop\Container\ContainerInterface;

/**
 * Resolves a string expression.
 *
 * @since 5.0
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
 */
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)
            ));
        }
    }
}

```
