/** * @param BaseNode $node * @param int $depth * @param bool $asComment */ private function writeNode(BaseNode $node, $depth = 0, $asComment = false) { // Reinitialize fields $this->comments = []; $this->default = ''; $this->defaultArray = null; $children = null; $this->example = $node->getExample(); $this->isCoreNode = $node->getParent() && $node->getParent()->getName() === 'rocketeer'; if ($node instanceof ArrayNode) { $children = $node->getChildren(); if ($node instanceof PrototypedArrayNode) { $prototype = $node->getPrototype(); if ($prototype instanceof ArrayNode) { $children = $prototype->getChildren(); } // Check for attribute as key if ($key = $node->getKeyAttribute()) { /* @var ArrayNode|ScalarNode $keyNode */ $keyNodeClass = 'Symfony\\Component\\Config\\Definition\\' . ($prototype instanceof ArrayNode ? 'ArrayNode' : 'ScalarNode'); $keyNode = new $keyNodeClass($key, $node); $keyNode->setInfo('Prototype'); // Add children foreach ($children as $childNode) { $keyNode->addChild($childNode); } $children = [$key => $keyNode]; } if (!$children) { if ($node->hasDefaultValue() && count($this->defaultArray = $node->getDefaultValue())) { $this->default = ''; } elseif (!is_array($this->example)) { $this->default = []; } } } } elseif ($node instanceof EnumNode) { $this->comments[] = 'One of ' . implode(', ', array_map('json_encode', $node->getValues())); $this->default = $node->getDefaultValue(); } elseif ($node instanceof ClosureNode) { $this->default = $node->getDefaultValue(); $analyzer = new TokenAnalyzer(); $this->default = $analyzer->analyze($this->default)['code']; $this->default = preg_replace_callback('/(\\n +)(.+)/', function ($line) use($depth) { return substr($line[1], 0, $depth * -4 - 4) . $line[2]; }, $this->default); } else { $this->default = null; if ($node->hasDefaultValue()) { $this->default = $node->getDefaultValue(); if (is_array($this->default)) { if (count($this->defaultArray = $node->getDefaultValue())) { $this->default = ''; } elseif (!is_array($this->example)) { $this->default = []; } } } } // required? if ($node->isRequired()) { $this->comments[] = 'Required'; } // example if ($this->example && !is_array($this->example)) { $this->comments[] = 'Example: ' . $this->example; } // Format comments and values $this->comments = count($this->comments) ? '// ' . implode(', ', $this->comments) : ''; $name = $this->serializeValue($node->getName()) . ' => '; $format = $asComment ? '// %s%s %s' : '%s%s %s'; if ($node instanceof ArrayNode) { $name .= '['; $this->default = !$this->example && !$children && !$this->defaultArray ? '],' : null; } elseif ($node instanceof ClosureNode) { $this->default .= ','; } else { $this->default = $this->default === "\n" ? '"\\n"' : $this->serializeValue($this->default); $this->default .= ','; } // Output informations $this->outputInformations($node, $depth); $text = rtrim(sprintf($format, $name, $this->default, $this->comments), ' '); // Output main value $this->writeLine($text, $depth * 4); $this->outputDefaults($depth); $this->outputExamples($depth); if ($node instanceof ArrayNode) { $this->outputChildren($children, $depth); } }