|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Input\InputArgument; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | +use Symfony\Component\Config\Definition\NodeInterface; |
| 18 | +use Symfony\Component\Config\Definition\ArrayNode; |
| 19 | +use Symfony\Component\Config\Definition\PrototypedArrayNode; |
| 20 | +use Symfony\Component\Config\Definition\BooleanNode; |
| 21 | + |
| 22 | +/** |
| 23 | + * A console command for dumping available configuration reference |
| 24 | + * |
| 25 | + * @author Kevin Bond <kevinbond@gmail.com> |
| 26 | + */ |
| 27 | +class ConfigDumpReferenceCommand extends ContainerDebugCommand |
| 28 | +{ |
| 29 | + protected $output; |
| 30 | + |
| 31 | + /** |
| 32 | + * @see Command |
| 33 | + */ |
| 34 | + protected function configure() |
| 35 | + { |
| 36 | + $this |
| 37 | + ->setDefinition(array( |
| 38 | + new InputArgument('name'
1E79
, InputArgument::REQUIRED, 'The Bundle or extension alias') |
| 39 | + )) |
| 40 | + ->setName('config:dump-reference') |
| 41 | + ->setDescription('Dumps default configuration for an extension.') |
| 42 | + ->setHelp(<<<EOF |
| 43 | +The <info>config:dump</info> command dumps the default configuration for an extension/bundle. |
| 44 | +
|
| 45 | +The extension alias or bundle name can be used: |
| 46 | +
|
| 47 | +Example: |
| 48 | +
|
| 49 | + <info>%command.name% framework</info> |
| 50 | +
|
| 51 | +or |
| 52 | +
|
| 53 | + <info>%command.name% FrameworkBundle</info> |
| 54 | +
|
| 55 | +EOF |
| 56 | + ) |
| 57 | + ; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @see Command |
| 62 | + */ |
| 63 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 64 | + { |
| 65 | + $this->output = $output; |
| 66 | + $kernel = $this->getContainer()->get('kernel'); |
| 67 | + $containerBuilder = $this->getContainerBuilder(); |
| 68 | + |
| 69 | + $name = $input->getArgument('name'); |
| 70 | + |
| 71 | + $extension = null; |
| 72 | + |
| 73 | + if (preg_match('/Bundle$/', $name)) { |
| 74 | + // input is bundle name |
| 75 | + $extension = $kernel->getBundle($name)->getContainerExtension(); |
| 76 | + |
| 77 | + if (!$extension) { |
| 78 | + throw new \LogicException('No extensions with configuration available for "'.$name.'"'); |
| 79 | + } |
| 80 | + |
| 81 | + $message = 'Default configuration for "'.$name.'"'; |
| 82 | + } else { |
| 83 | + foreach ($kernel->getBundles() as $bundle) { |
| 84 | + $extension = $bundle->getContainerExtension(); |
| 85 | + |
| 86 | + if ($extension && $extension->getAlias() === $name) { |
| 87 | + break; |
| 88 | + } |
| 89 | + |
| 90 | + $extension = null; |
| 91 | + } |
| 92 | + |
| 93 | + if (!$extension) { |
| 94 | + throw new \LogicException('No extension with alias "'.$name.'" is enabled'); |
| 95 | + } |
| 96 | + |
| 97 | + $message = 'Default configuration for extension with alias: "'.$name.'"'; |
| 98 | + } |
| 99 | + |
| 100 | + $configuration = $extension->getConfiguration(array(), $containerBuilder); |
| 101 | + |
| 102 | + if (!$configuration) { |
| 103 | + throw new \LogicException('The extension with alias "'.$extension->getAlias(). |
| 104 | + '" does not have it\'s getConfiguration() method setup'); |
| 105 | + } |
| 106 | + |
| 107 | + $rootNode = $configuration->getConfigTreeBuilder()->buildTree(); |
| 108 | + |
| 109 | + $output->writeln($message); |
| 110 | + |
| 111 | + // root node |
| 112 | + $this->outputNode($rootNode); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Outputs a single config reference line |
| 117 | + * |
| 118 | + * @param string $text |
| 119 | + * @param int $indent |
| 120 | + */ |
| 121 | + private function outputLine($text, $indent = 0) |
| 122 | + { |
| 123 | + $indent = strlen($text) + $indent; |
| 124 | + |
| 125 | + $format = '%'.$indent.'s'; |
| 126 | + |
| 127 | + $this->output->writeln(sprintf($format, $text)); |
| 128 | + } |
| 129 | + |
| 130 | + private function outputArray(array $array, $depth) |
| 131 | + { |
| 132 | + $is_indexed = array_values($array) === $array; |
| 133 | + |
| 134 | + foreach ($array as $key => $value) { |
| 135 | + if (is_array($value)) { |
| 136 | + $val = ''; |
| 137 | + } else { |
| 138 | + $val = $value; |
| 139 | + } |
| 140 | + |
| 141 | + if ($is_indexed) { |
| 142 | + $this->outputLine('- '.$val, $depth * 4); |
| 143 | + } else { |
| 144 | + $this->outputLine(sprintf('%-20s %s', $key.':', $val), $depth * 4); |
| 145 | + } |
| 146 | + |
| 147 | + if (is_array($value)) { |
| 148 | + $this->outputArray($value, $depth + 1); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @param NodeInterface $node |
| 155 | + * @param int $depth |
| 156 | + */ |
| 157 | + private function outputNode(NodeInterface $node, $depth = 0) |
| 158 | + { |
| 159 | + $comments = array(); |
| 160 | + $default = ''; |
| 161 | + $defaultArray = null; |
| 162 | + $children = null; |
| 163 | + $example = $node->getExample(); |
| 164 | + |
| 165 | + // defaults |
| 166 | + if ($node instanceof ArrayNode) { |
| 167 | + $children = $node->getChildren(); |
| 168 | + |
| 169 | + if ($node instanceof PrototypedArrayNode) { |
| 170 | + $prototype = $node->getPrototype(); |
| 171 | + |
| 172 | + if ($prototype instanceof ArrayNode) { |
| 173 | + $children = $prototype->getChildren(); |
| 174 | + } |
| 175 | + |
| 176 | + // check for attribute as key |
| 177 | + if ($key = $node->getKeyAttribute()) { |
| 178 | + $keyNode = new ArrayNode($key, $node); |
| 179 | + $keyNode->setInfo('Prototype'); |
| 180 | + |
| 181 | + // add children |
| 182 | + foreach ($children as $childNode) { |
| 183 | + $keyNode->addChild($childNode); |
| 184 | + } |
| 185 | + $children = array($key => $keyNode); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + if (!$children) { |
| 190 | + if ($node->hasDefaultValue() && count($defaultArray = $node->getDefaultValue())) { |
| 191 | + $default = ''; |
| 192 | + } elseif (!is_array($example)) { |
| 193 | + $default = '[]'; |
| 194 | + } |
| 195 | + } |
| 196 | + } else { |
| 197 | + $default = '~'; |
| 198 | + |
| 199 | + if ($node->hasDefaultValue()) { |
| 200 | + $default = $node->getDefaultValue(); |
| 201 | + |
| 202 | + if (true === $default) { |
| 203 | + $default = 'true'; |
| 204 | + } elseif (false === $default) { |
| 205 | + $default = 'false'; |
| 206 | + } elseif (null === $default) { |
| 207 | + $default = '~'; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + // required? |
| 213 | + if ($node->isRequired()) { |
| 214 | + $comments[] = 'Required'; |
| 215 | + } |
| 216 | + |
| 217 | + // example |
| 218 | + if ($example && !is_array($example)) { |
| 219 | + $comments[] = 'Example: '.$example; |
| 220 | + } |
| 221 | + |
| 222 | + $default = (string) $default != '' ? ' '.$default : ''; |
| 223 | + $comments = count($comments) ? '# '.implode(', ', $comments) : ''; |
| 224 | + |
| 225 | + $text = sprintf('%-20s %s %s', $node->getName().':', $default, $comments); |
| 226 | + |
| 227 | + if ($info = $node->getInfo()) { |
| 228 | + $this->outputLine(''); |
| 229 | + $this->outputLine('# '.$info, $depth * 4); |
| 230 | + } |
| 231 | + |
| 232 | + $this->outputLine($text, $depth * 4); |
| 233 | + |
| 234 | + // output defaults |
| 235 | + if ($defaultArray) { |
| 236 | + $this->outputLine(''); |
| 237 | + |
| 238 | + $message = count($defaultArray) > 1 ? 'Defaults' : 'Default'; |
| 239 | + |
| 240 | + $this->outputLine('# '.$message.':', $depth * 4 + 4); |
| 241 | + |
| 242 | + $this->outputArray($defaultArray, $depth + 1); |
| 243 | + } |
| 244 | + |
| 245 | + if (is_array($example)) { |
| 246 | + $this->outputLine(''); |
| 247 | + |
| 248 | + $message = count($example) > 1 ? 'Examples' : 'Example'; |
| 249 | + |
| 250 | + $this->outputLine('# '.$message.':', $depth * 4 + 4); |
| 251 | + |
| 252 | + $this->outputArray($example, $depth + 1); |
| 253 | + } |
| 254 | + |
| 255 | + if ($children) { |
| 256 | + foreach ($children as $childNode) { |
| 257 | + $this->outputNode($childNode, $depth + 1); |
| 258 | + } |
| 259 | + } |
| 260 | + } |
| 261 | +} |
0 commit comments