8000 moved some code to the component · sam57/symfony@6bafe5a · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 6bafe5a

Browse files
committed
moved some code to the component
1 parent ad91b3b commit 6bafe5a

File tree

2 files changed

+178
-154
lines changed

2 files changed

+178
-154
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

Lines changed: 3 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14+
use Symfony\Component\Config\Definition\ReferenceDumper;
1415
use Symfony\Component\Console\Input\InputArgument;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Output\OutputInterface;
@@ -26,8 +27,6 @@
2627
*/
2728
class ConfigDumpReferenceCommand extends ContainerDebugCommand
2829
{
29-
protected $output;
30-
3130
/**
3231
* @see Command
3332
*/
@@ -61,7 +60,6 @@ protected function configure()
6160
*/
6261
protected function execute(InputInterface $input, OutputInterface $output)
6362
{
64-
$this->output = $output;
6563
$bundles = $this->getContainer()->get('kernel')->getBundles();
6664
$containerBuilder = $this->getContainerBuilder();
6765

@@ -112,158 +110,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
112110
'" should implement ConfigurationInterface in order to be dumpable');
113111
}
114112

115-
$rootNode = $configuration->getConfigTreeBuilder()->buildTree();
116-
117113
$output->writeln($message);
118114

119-
// root node
120-
$this->outputNode($rootNode);
121-
}
122-
123-
/**
124-
* Outputs a single config reference line
125-
*
126-
* @param string $text
127-
* @param int $indent
128-
*/
129-
private function outputLine($text, $indent = 0)
130-
{
131-
$indent = strlen($text) + $indent;
132-
133-
$format = '%'.$indent.'s';
134-
135-
$this->output->writeln(sprintf($format, $text));
136-
}
137-
138-
private function outputArray(array $array, $depth)
139-
{
140-
$isIndexed = array_values($array) === $array;
141-
142-
foreach ($array as $key => $value) {
143-
if (is_array($value)) {
144-
$val = '';
145-
} else {
146-
$val = $value;
147-
}
148-
149-
if ($isIndexed) {
150-
$this->outputLine('- '.$val, $depth * 4);
151-
} else {
152-
$this->outputLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
153-
}
154-
155-
if (is_array($value)) {
156-
$this->outputArray($value, $depth + 1);
157-
}
158-
}
159-
}
160-
161-
/**
162-
* @param NodeInterface $node
163-
* @param int $depth
164-
*/
165-
private function outputNode(NodeInterface $node, $depth = 0)
166-
{
167-
$comments = array();
168-
$default = '';
169-
$defaultArray = null;
170-
$children = null;
171-
$example = $node->getExample();
172-
173-
// defaults
174-
if ($node instanceof ArrayNode) {
175-
$children = $node->getChildren();
176-
177-
if ($node instanceof PrototypedArrayNode) {
178-
$prototype = $node->getPrototype();
179-
180-
if ($prototype instanceof ArrayNode) {
181-
$children = $prototype->getChildren();
182-
}
183-
184-
// check for attribute as key
185-
if ($key = $node->getKeyAttribute()) {
186-
$keyNode = new ArrayNode($key, $node);
187-
$keyNode->setInfo('Prototype');
188-
189-
// add children
190-
foreach ($children as $childNode) {
191-
$keyNode->addChild($childNode);
192-
}
193-
$children = array($key => $keyNode);
194-
}
195-
}
196-
197-
if (!$children) {
198-
if ($node->hasDefaultValue() && count($defaultArray = $node->getDefaultValue())) {
199-
$default = '';
200-
} elseif (!is_array($example)) {
201-
$default = '[]';
202-
}
203-
}
204-
} else {
205-
$default = '~';
206-
207-
if ($node->hasDefaultValue()) {
208-
$default = $node->getDefaultValue();
209-
210-
if (true === $default) {
211-
$default = 'true';
212-
} elseif (false === $default) {
213-
$default = 'false';
214-
} elseif (null === $default) {
215-
$default = '~';
216-
}
217-
}
218-
}
219-
220-
// required?
221-
if ($node->isRequired()) {
222-
$comments[] = 'Required';
223-
}
224-
225-
// example
226-
if ($example && !is_array($example)) {
227-
$comments[] = 'Example: '.$example;
228-
}
229-
230-
$default = (string) $default != '' ? ' '.$default : '';
231-
$comments = count($comments) ? '# '.implode(', ', $comments) : '';
232-
233-
$text = sprintf('%-20s %s %s', $node->getName().':', $default, $comments);
234-
235-
if ($info = $node->getInfo()) {
236-
$this->outputLine('');
237-
$this->outputLine('# '.$info, $depth * 4);
238-
}
239-
240-
$this->outputLine($text, $depth * 4);
241-
242-
// output defaults
243-
if ($defaultArray) {
244-
$this->outputLine('');
245-
246-
$message = count($defaultArray) > 1 ? 'Defaults' : 'Default';
247-
248-
$this->outputLine('# '.$message.':', $depth * 4 + 4);
249-
250-
$this->outputArray($defaultArray, $depth + 1);
251-
}
252-
253-
if (is_array($example)) {
254-
$this->outputLine('');
255-
256-
$message = count($example) > 1 ? 'Examples' : 'Example';
257-
258-
$this->outputLine('# '.$message.':', $depth * 4 + 4);
259-
260-
$this->outputArray($example, $depth + 1);
261-
}
262-
263-
if ($children) {
264-
foreach ($children as $childNode) {
265-
$this->outputNode($childNode, $depth + 1);
266-
}
267-
}
115+
$dumper = new ReferenceDumper();
116+
$output->writeln($dumper->dump($configuration));
268117
}
269118
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
3+
namespace Symfony\Component\Config\Definition;
4+
5+
/**
6+
* Dumps a reference configuration for the given configuration/node instance.
7+
*
8+
* Currently, only YML format is supported.
9+
*
10+
* @author Kevin Bond <kevinbond@gmail.com>
11+
*/
12+
class ReferenceDumper
13+
{
14+
private $reference;
15+
16+
public function dump(ConfigurationInterface $configuration)
17+
{
18+
return $this->dumpNode($configuration->getConfigTreeBuilder()->buildTree());
19+
}
20+
21+
public function dumpNode(NodeInterface $node)
22+
{
23+
$this->reference = '';
24+
$this->writeNode($node);
25+
$ref = $this->reference;
26+
$this->reference = null;
27+
28+
return $ref;
29+
}
30+
31+
/**
32+
* @param int $depth
33+
*/
34+
private function writeNode(NodeInterface $node, $depth = 0)
35+
{
36+
$comments = array();
37+
$default = '';
38+
$defaultArray = null;
39+
$children = null;
40+
$example = $node->getExample();
41+
42+
// defaults
43+
if ($node instanceof ArrayNode) {
44+
$children = $node->getChildren();
45+
46+
if ($node instanceof PrototypedArrayNode) {
47+
$prototype = $node->getPrototype();
48+
49+
if ($prototype instanceof ArrayNode) {
50+
$children = $prototype->getChildren();
51+
}
52+
53+
// check for attribute as key
54+
if ($key = $node->getKeyAttribute()) {
55+
$keyNode = new ArrayNode($key, $node);
56+
$keyNode->setInfo('Prototype');
57+
58+
// add children
59+
foreach ($children as $childNode) {
60+
$keyNode->addChild($childNode);
61+
}
62+
$children = array($key => $keyNode);
63+
}
64+
}
65+
66+
if (!$children) {
67+
if ($node->hasDefaultValue() && count($defaultArray = $node->getDefaultValue())) {
68+
$default = '';
69+
} elseif (!is_array($example)) {
70+
$default = '[]';
71+
}
72+
}
73+
} else {
74+
$default = '~';
75+
76+
if ($node->hasDefaultValue()) {
77+
$default = $node->getDefaultValue();
78+
79+
if (true === $default) {
80+
$default = 'true';
81+
} elseif (false === $default) {
82+
$default = 'false';
83+
} elseif (null === $default) {
84+
$default = '~';
85+
}
86+
}
87+
}
88+
89+
// required?
90+
if ($node->isRequired()) {
91+
$comments[] = 'Required';
92+
}
93+
94+
// example
95+
if ($example && !is_array($example)) {
96+
$comments[] = 'Example: '.$example;
97+
}
98+
99+
$default = (string) $default != '' ? ' '.$default : '';
100+
$comments = count($comments) ? '# '.implode(', ', $comments) : '';
101+
102+
$text = sprintf('%-20s %s %s', $node->getName().':', $default, $comments);
103+
104+
if ($info = $node->getInfo()) {
105+
$this->writeLine('');
106+
$this->writeLine('# '.$info, $depth * 4);
107+
}
108+
109+
$this->writeLine($text, $depth * 4);
110+
111+
// output defaults
112+
if ($defaultArray) {
113+
$this->writeLine('');
114+
115+
$message = count($defaultArray) > 1 ? 'Defaults' : 'Default';
116+
117+
$this->writeLine('# '.$message.':', $depth * 4 + 4);
118+
119+
$this->writeArray($defaultArray, $depth + 1);
120+
}
121+
122+
if (is_array($example)) {
123+
$this->writeLine('');
124+
125+
$message = count($example) > 1 ? 'Examples' : 'Example';
126+
127+
$this->writeLine('# '.$message.':', $depth * 4 + 4);
128+
129+
$this->writeArray($example, $depth + 1);
130+
}
131+
132+
if ($children) {
133+
foreach ($children as $childNode) {
134+
$this->writeNode($childNode, $depth + 1);
135+
}
136+
}
137+
}
138+
139+
/**
140+
* Outputs a single config reference line
141+
*
142+
* @param string $text
143+
* @param int $indent
144+
*/
145+
private function writeLine($text, $indent = 0)
146+
{
147+
$indent = strlen($text) + $indent;
148+
$format = '%'.$indent.'s';
149+
150+
$this->reference .= sprintf($format, $text)."\n";
151+
}
152+
153+
private function writeArray(array $array, $depth)
154+
{
155+
$isIndexed = array_values($array) === $array;
156+
157+
foreach ($array as $key => $value) {
158+
if (is_array($value)) {
159+
$val = '';
160+
} else {
161+
$val = $value;
162+
}
163+
164+
if ($isIndexed) {
165+
$this->writeLine('- '.$val, $depth * 4);
166+
} else {
167+
$this->writeLine(sprintf('%-20s %s', $key.':', $val), $depth * 4);
168+
}
169+
170+
if (is_array($value)) {
171+
$this->writeArray($value, $depth + 1);
172+
}
173+
}
174+
}
175+
}

0 commit comments

Comments
 (0)
0