TYPO3  7.6
JsonDescriptor.php
Go to the documentation of this file.
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\Component\Console\Descriptor;
13 
19 
28 {
32  protected function describeInputArgument(InputArgument $argument, array $options = array())
33  {
34  $this->writeData($this->getInputArgumentData($argument), $options);
35  }
36 
40  protected function describeInputOption(InputOption $option, array $options = array())
41  {
42  $this->writeData($this->getInputOptionData($option), $options);
43  }
44 
48  protected function describeInputDefinition(InputDefinition $definition, array $options = array())
49  {
50  $this->writeData($this->getInputDefinitionData($definition), $options);
51  }
52 
56  protected function describeCommand(Command $command, array $options = array())
57  {
58  $this->writeData($this->getCommandData($command), $options);
59  }
60 
64  protected function describeApplication(Application $application, array $options = array())
65  {
66  $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
67  $description = new ApplicationDescription($application, $describedNamespace);
68  $commands = array();
69 
70  foreach ($description->getCommands() as $command) {
71  $commands[] = $this->getCommandData($command);
72  }
73 
74  $data = $describedNamespace
75  ? array('commands' => $commands, 'namespace' => $describedNamespace)
76  : array('commands' => $commands, 'namespaces' => array_values($description->getNamespaces()));
77 
78  $this->writeData($data, $options);
79  }
80 
89  private function writeData(array $data, array $options)
90  {
91  $this->write(json_encode($data, isset($options['json_encoding']) ? $options['json_encoding'] : 0));
92  }
93 
99  private function getInputArgumentData(InputArgument $argument)
100  {
101  return array(
102  'name' => $argument->getName(),
103  'is_required' => $argument->isRequired(),
104  'is_array' => $argument->isArray(),
105  'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $argument->getDescription()),
106  'default' => $argument->getDefault(),
107  );
108  }
109 
115  private function getInputOptionData(InputOption $option)
116  {
117  return array(
118  'name' => '--'.$option->getName(),
119  'shortcut' => $option->getShortcut() ? '-'.implode('|-', explode('|', $option->getShortcut())) : '',
120  'accept_value' => $option->acceptValue(),
121  'is_value_required' => $option->isValueRequired(),
122  'is_multiple' => $option->isArray(),
123  'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $option->getDescription()),
124  'default' => $option->getDefault(),
125  );
126  }
127 
133  private function getInputDefinitionData(InputDefinition $definition)
134  {
135  $inputArguments = array();
136  foreach ($definition->getArguments() as $name => $argument) {
137  $inputArguments[$name] = $this->getInputArgumentData($argument);
138  }
139 
140  $inputOptions = array();
141  foreach ($definition->getOptions() as $name => $option) {
142  $inputOptions[$name] = $this->getInputOptionData($option);
143  }
144 
145  return array('arguments' => $inputArguments, 'options' => $inputOptions);
146  }
147 
153  private function getCommandData(Command $command)
154  {
155  $command->getSynopsis();
156  $command->mergeApplicationDefinition(false);
157 
158  return array(
159  'name' => $command->getName(),
160  'usage' => array_merge(array($command->getSynopsis()), $command->getUsages(), $command->getAliases()),
161  'description' => $command->getDescription(),
162  'help' => $command->getProcessedHelp(),
163  'definition' => $this->getInputDefinitionData($command->getNativeDefinition()),
164  );
165  }
166 }