function OptimizedPhpArrayDumper::dumpValue

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumper.php \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper::dumpValue()
  2. 10 core/lib/Drupal/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumper.php \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper::dumpValue()
  3. 11.x core/lib/Drupal/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumper.php \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper::dumpValue()

Dumps the value to PHP array format.

Parameters

mixed $value: The value to dump.

Return value

mixed The dumped value in a suitable format.

Throws

\Symfony\Component\DependencyInjection\Exception\RuntimeException When trying to dump object or resource.

4 calls to OptimizedPhpArrayDumper::dumpValue()
OptimizedPhpArrayDumper::dumpCallable in core/lib/Drupal/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumper.php
Dumps callable to a PHP array.
OptimizedPhpArrayDumper::dumpCollection in core/lib/Drupal/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumper.php
Dumps a collection to a PHP array.
OptimizedPhpArrayDumper::prepareParameters in core/lib/Drupal/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumper.php
Prepares parameters for the PHP array dumping.
PhpArrayDumper::dumpCollection in core/lib/Drupal/Component/DependencyInjection/Dumper/PhpArrayDumper.php
Dumps a collection to a PHP array.

File

core/lib/Drupal/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumper.php, line 395

Class

OptimizedPhpArrayDumper
OptimizedPhpArrayDumper dumps a service container as a serialized PHP array.

Namespace

Drupal\Component\DependencyInjection\Dumper

Code

protected function dumpValue($value) {
    if (is_array($value)) {
        $code = [];
        foreach ($value as $k => $v) {
            $code[$k] = $this->dumpValue($v);
        }
        return $code;
    }
    elseif ($value instanceof Reference) {
        return $this->getReferenceCall((string) $value, $value);
    }
    elseif ($value instanceof Definition) {
        return $this->getPrivateServiceCall(NULL, $value);
    }
    elseif ($value instanceof Parameter) {
        return $this->getParameterCall((string) $value);
    }
    elseif (is_string($value) && FALSE !== strpos($value, '%')) {
        if (preg_match('/^%([^%]+)%$/', $value, $matches)) {
            return $this->getParameterCall($matches[1]);
        }
        else {
            $replaceParameters = function ($matches) {
                return $this->getParameterCall($matches[2]);
            };
            // We cannot directly return the string value because it would
            // potentially not always be resolved in the dumpCollection() method.
            return (object) [
                'type' => 'raw',
                'value' => str_replace('%%', '%', preg_replace_callback('/(?<!%)(%)([^%]+)\\1/', $replaceParameters, $value)),
            ];
        }
    }
    elseif ($value instanceof Expression) {
        throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
    }
    elseif (is_object($value)) {
        // Drupal specific: Instantiated objects have a _serviceId parameter.
        if (isset($value->_serviceId)) {
            return $this->getReferenceCall($value->_serviceId);
        }
        throw new RuntimeException('Unable to dump a service container if a parameter is an object without _serviceId.');
    }
    elseif (is_resource($value)) {
        throw new RuntimeException('Unable to dump a service container if a parameter is a resource.');
    }
    return $value;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.