PhpArrayDumper.php in Service Container 7.2
File
lib/Drupal/Core/DependencyInjection/Dumper/PhpArrayDumper.php
View source
<?php
namespace Drupal\Core\DependencyInjection\Dumper;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Dumper\Dumper;
class PhpArrayDumper extends Dumper {
public function dump(array $options = array()) {
return serialize($this
->getArray());
}
public function getArray() {
$definition = array();
$definition['parameters'] = $this
->getParameters();
$definition['services'] = $this
->getServiceDefinitions();
return $definition;
}
protected function getParameters() {
if (!$this->container
->getParameterBag()
->all()) {
return array();
}
$parameters = $this->container
->getParameterBag()
->all();
$is_frozen = $this->container
->isFrozen();
return $this
->prepareParameters($parameters, $is_frozen);
}
protected function getServiceDefinitions() {
if (!$this->container
->getDefinitions()) {
return array();
}
$services = array();
foreach ($this->container
->getDefinitions() as $id => $definition) {
$services[$id] = $this
->getServiceDefinition($definition);
}
$aliases = $this->container
->getAliases();
foreach ($aliases as $alias => $id) {
while (isset($aliases[(string) $id])) {
$id = $aliases[(string) $id];
}
$services[$alias] = $this
->getServiceAliasDefinition($id);
}
return $services;
}
protected function prepareParameters($parameters, $escape = true) {
$filtered = array();
foreach ($parameters as $key => $value) {
if (is_array($value)) {
$value = $this
->prepareParameters($value, $escape);
}
elseif ($value instanceof Reference) {
$value = '@' . $value;
}
$filtered[$key] = $value;
}
return $escape ? $this
->escape($filtered) : $filtered;
}
protected function escape($arguments) {
$args = array();
foreach ($arguments as $k => $v) {
if (is_array($v)) {
$args[$k] = $this
->escape($v);
}
elseif (is_string($v)) {
$args[$k] = str_replace('%', '%%', $v);
}
else {
$args[$k] = $v;
}
}
return $args;
}
protected function getServiceDefinition($definition) {
$service = array();
if ($definition
->getClass()) {
$service['class'] = $definition
->getClass();
}
if (!$definition
->isPublic()) {
$service['public'] = FALSE;
}
$tags = $definition
->getTags();
if (!empty($tags)) {
foreach ($tags as $tag => $attributes) {
$tag = [
'name' => $tag,
];
foreach ($attributes as $attribute) {
$tag += $attribute;
}
$service['tags'][] = $tag;
}
}
if ($definition
->getFile()) {
$service['file'] = $definition
->getFile();
}
if ($definition
->isSynthetic()) {
$service['synthetic'] = TRUE;
}
if ($definition
->isLazy()) {
$service['lazy'] = TRUE;
}
if ($definition
->getArguments()) {
$service['arguments'] = $this
->dumpValue($definition
->getArguments());
}
if ($definition
->getProperties()) {
$service['properties'] = $this
->dumpValue($definition
->getProperties());
}
if ($definition
->getMethodCalls()) {
$service['calls'] = $this
->dumpValue($definition
->getMethodCalls());
}
if (($scope = $definition
->getScope()) !== ContainerInterface::SCOPE_CONTAINER) {
$service['scope'] = $scope;
}
if (($decorated = $definition
->getDecoratedService()) !== NULL) {
$service['decorates'] = $decorated;
}
if ($callable = $definition
->getFactory()) {
$service['factory'] = $this
->dumpCallable($callable);
}
if ($callable = $definition
->getConfigurator()) {
$service['configurator'] = $this
->dumpCallable($callable);
}
return $service;
}
protected function getServiceAliasDefinition($id) {
if ($id
->isPublic()) {
return array(
'alias' => (string) $id,
);
}
else {
return array(
'alias' => (string) $id,
'public' => FALSE,
);
}
}
protected function dumpCallable($callable) {
if (is_array($callable)) {
if ($callable[0] instanceof Reference) {
$callable = array(
$this
->getServiceCall((string) $callable[0], $callable[0]),
$callable[1],
);
}
elseif ($callable[0] instanceof Definition) {
$callable[0] = $this
->getPrivateService($callable[0]);
$callable = array(
$callable[0],
$callable[1],
);
}
else {
$callable = array(
$callable[0],
$callable[1],
);
}
}
return $callable;
}
protected function getPrivateService(Definition $definition) {
$service_definition = $this
->getServiceDefinition($definition);
$hash = sha1(serialize($service_definition));
return (object) array(
'type' => 'service',
'id' => 'private__' . $hash,
'value' => $service_definition,
);
}
protected function dumpValue($value) {
if (is_array($value)) {
$code = array();
foreach ($value as $k => $v) {
$code[$k] = $this
->dumpValue($v);
}
return $code;
}
elseif ($value instanceof Reference) {
return $this
->getServiceCall((string) $value, $value);
}
elseif ($value instanceof Definition) {
return $this
->getPrivateService($value);
}
elseif ($value instanceof Parameter) {
return $this
->getParameterCall((string) $value);
}
elseif ($value instanceof Expression) {
return $this
->getExpressionCall((string) $value);
}
elseif (is_object($value)) {
if (isset($value->_serviceId)) {
return '@' . $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;
}
protected function getServiceCall($id, Reference $reference = null) {
if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference
->getInvalidBehavior()) {
return sprintf('@?%s', $id);
}
return sprintf('@%s', $id);
}
protected function getParameterCall($id) {
return sprintf('%%%s%%', $id);
}
protected function getExpressionCall($expression) {
return sprintf('@=%s', $expression);
}
}
Classes
Name |
Description |
PhpArrayDumper |
PhpArrayDumper dumps a service container as a serialized PHP array. |