View source
<?php
namespace Symfony\Component\DependencyInjection\Dumper;
use Symfony\Component\Yaml\Dumper as YmlDumper;
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\ExpressionLanguage\Expression;
class YamlDumper extends Dumper {
private $dumper;
public function dump(array $options = array()) {
if (!class_exists('Symfony\\Component\\Yaml\\Dumper')) {
throw new RuntimeException('Unable to dump the container as the Symfony Yaml Component is not installed.');
}
if (null === $this->dumper) {
$this->dumper = new YmlDumper();
}
return $this
->addParameters() . "\n" . $this
->addServices();
}
private function addService($id, $definition) {
$code = " {$id}:\n";
if ($definition
->getClass()) {
$code .= sprintf(" class: %s\n", $definition
->getClass());
}
if (!$definition
->isPublic()) {
$code .= " public: false\n";
}
$tagsCode = '';
foreach ($definition
->getTags() as $name => $tags) {
foreach ($tags as $attributes) {
$att = array();
foreach ($attributes as $key => $value) {
$att[] = sprintf('%s: %s', $this->dumper
->dump($key), $this->dumper
->dump($value));
}
$att = $att ? ', ' . implode(', ', $att) : '';
$tagsCode .= sprintf(" - { name: %s%s }\n", $this->dumper
->dump($name), $att);
}
}
if ($tagsCode) {
$code .= " tags:\n" . $tagsCode;
}
if ($definition
->getFile()) {
$code .= sprintf(" file: %s\n", $definition
->getFile());
}
if ($definition
->isSynthetic()) {
$code .= sprintf(" synthetic: true\n");
}
if ($definition
->isSynchronized(false)) {
$code .= sprintf(" synchronized: true\n");
}
if ($definition
->getFactoryClass(false)) {
$code .= sprintf(" factory_class: %s\n", $definition
->getFactoryClass(false));
}
if ($definition
->isLazy()) {
$code .= sprintf(" lazy: true\n");
}
if ($definition
->getFactoryMethod(false)) {
$code .= sprintf(" factory_method: %s\n", $definition
->getFactoryMethod(false));
}
if ($definition
->getFactoryService(false)) {
$code .= sprintf(" factory_service: %s\n", $definition
->getFactoryService(false));
}
if ($definition
->getArguments()) {
$code .= sprintf(" arguments: %s\n", $this->dumper
->dump($this
->dumpValue($definition
->getArguments()), 0));
}
if ($definition
->getProperties()) {
$code .= sprintf(" properties: %s\n", $this->dumper
->dump($this
->dumpValue($definition
->getProperties()), 0));
}
if ($definition
->getMethodCalls()) {
$code .= sprintf(" calls:\n%s\n", $this->dumper
->dump($this
->dumpValue($definition
->getMethodCalls()), 1, 12));
}
if (ContainerInterface::SCOPE_CONTAINER !== ($scope = $definition
->getScope())) {
$code .= sprintf(" scope: %s\n", $scope);
}
if (null !== ($decorated = $definition
->getDecoratedService())) {
list($decorated, $renamedId) = $decorated;
$code .= sprintf(" decorates: %s\n", $decorated);
if (null !== $renamedId) {
$code .= sprintf(" decoration_inner_name: %s\n", $renamedId);
}
}
if ($callable = $definition
->getFactory()) {
$code .= sprintf(" factory: %s\n", $this->dumper
->dump($this
->dumpCallable($callable), 0));
}
if ($callable = $definition
->getConfigurator()) {
$code .= sprintf(" configurator: %s\n", $this->dumper
->dump($this
->dumpCallable($callable), 0));
}
return $code;
}
private function addServiceAlias($alias, $id) {
if ($id
->isPublic()) {
return sprintf(" %s: @%s\n", $alias, $id);
}
else {
return sprintf(" %s:\n alias: %s\n public: false", $alias, $id);
}
}
private function addServices() {
if (!$this->container
->getDefinitions()) {
return '';
}
$code = "services:\n";
foreach ($this->container
->getDefinitions() as $id => $definition) {
$code .= $this
->addService($id, $definition);
}
$aliases = $this->container
->getAliases();
foreach ($aliases as $alias => $id) {
while (isset($aliases[(string) $id])) {
$id = $aliases[(string) $id];
}
$code .= $this
->addServiceAlias($alias, $id);
}
return $code;
}
private function addParameters() {
if (!$this->container
->getParameterBag()
->all()) {
return '';
}
$parameters = $this
->prepareParameters($this->container
->getParameterBag()
->all(), $this->container
->isFrozen());
return $this->dumper
->dump(array(
'parameters' => $parameters,
), 2);
}
private function dumpCallable($callable) {
if (is_array($callable)) {
if ($callable[0] instanceof Reference) {
$callable = array(
$this
->getServiceCall((string) $callable[0], $callable[0]),
$callable[1],
);
}
else {
$callable = array(
$callable[0],
$callable[1],
);
}
}
return $callable;
}
private 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 Parameter) {
return $this
->getParameterCall((string) $value);
}
elseif ($value instanceof Expression) {
return $this
->getExpressionCall((string) $value);
}
elseif (is_object($value) || is_resource($value)) {
throw new RuntimeException('Unable to dump a service container if a parameter is an object or a resource.');
}
return $value;
}
private function getServiceCall($id, Reference $reference = null) {
if (null !== $reference && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $reference
->getInvalidBehavior()) {
return sprintf('@?%s', $id);
}
return sprintf('@%s', $id);
}
private function getParameterCall($id) {
return sprintf('%%%s%%', $id);
}
private function getExpressionCall($expression) {
return sprintf('@=%s', $expression);
}
private 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 || is_string($value) && 0 === strpos($value, '@')) {
$value = '@' . $value;
}
$filtered[$key] = $value;
}
return $escape ? $this
->escape($filtered) : $filtered;
}
private 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;
}
}