function OptimizedPhpArrayDumper::dumpValue

Same name in other branches
  1. 9 core/lib/Drupal/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumper.php \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper::dumpValue()
  2. 8.9.x 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::getIterator in core/lib/Drupal/Component/DependencyInjection/Dumper/OptimizedPhpArrayDumper.php
Gets a service iterator in a suitable PHP array format.
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 393

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) && str_contains($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 ($value instanceof ServiceClosureArgument) {
        $reference = $value->getValues();
        
        /** @var \Symfony\Component\DependencyInjection\Reference $reference */
        $reference = reset($reference);
        return $this->getServiceClosureCall((string) $reference, $reference->getInvalidBehavior());
    }
    elseif ($value instanceof IteratorArgument) {
        return $this->getIterator($value);
    }
    elseif (is_object($value)) {
        // Drupal specific: Instantiated objects have a _serviceId parameter.
        if (isset($value->_serviceId)) {
            @trigger_error('_serviceId is deprecated in drupal:9.5.0 and is removed from drupal:11.0.0. Use \\Drupal\\Core\\DrupalKernelInterface::getServiceIdMapping() instead. See https://www.drupal.org/node/3292540', E_USER_DEPRECATED);
            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.