TYPO3  7.6
TextDescriptor.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  if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
35  $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($argument->getDefault()));
36  } else {
37  $default = '';
38  }
39 
40  $totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument->getName());
41  $spacingWidth = $totalWidth - strlen($argument->getName()) + 2;
42 
43  $this->writeText(sprintf(' <info>%s</info>%s%s%s',
44  $argument->getName(),
45  str_repeat(' ', $spacingWidth),
46  // + 17 = 2 spaces + <info> + </info> + 2 spaces
47  preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 17), $argument->getDescription()),
48  $default
49  ), $options);
50  }
51 
55  protected function describeInputOption(InputOption $option, array $options = array())
56  {
57  if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
58  $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault()));
59  } else {
60  $default = '';
61  }
62 
63  $value = '';
64  if ($option->acceptValue()) {
65  $value = '='.strtoupper($option->getName());
66 
67  if ($option->isValueOptional()) {
68  $value = '['.$value.']';
69  }
70  }
71 
72  $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions(array($option));
73  $synopsis = sprintf('%s%s',
74  $option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : ' ',
75  sprintf('--%s%s', $option->getName(), $value)
76  );
77 
78  $spacingWidth = $totalWidth - strlen($synopsis) + 2;
79 
80  $this->writeText(sprintf(' <info>%s</info>%s%s%s%s',
81  $synopsis,
82  str_repeat(' ', $spacingWidth),
83  // + 17 = 2 spaces + <info> + </info> + 2 spaces
84  preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 17), $option->getDescription()),
85  $default,
86  $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''
87  ), $options);
88  }
89 
93  protected function describeInputDefinition(InputDefinition $definition, array $options = array())
94  {
95  $totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
96  foreach ($definition->getArguments() as $argument) {
97  $totalWidth = max($totalWidth, strlen($argument->getName()));
98  }
99 
100  if ($definition->getArguments()) {
101  $this->writeText('<comment>Arguments:</comment>', $options);
102  $this->writeText("\n");
103  foreach ($definition->getArguments() as $argument) {
104  $this->describeInputArgument($argument, array_merge($options, array('total_width' => $totalWidth)));
105  $this->writeText("\n");
106  }
107  }
108 
109  if ($definition->getArguments() && $definition->getOptions()) {
110  $this->writeText("\n");
111  }
112 
113  if ($definition->getOptions()) {
114  $laterOptions = array();
115 
116  $this->writeText('<comment>Options:</comment>', $options);
117  foreach ($definition->getOptions() as $option) {
118  if (strlen($option->getShortcut()) > 1) {
119  $laterOptions[] = $option;
120  continue;
121  }
122  $this->writeText("\n");
123  $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
124  }
125  foreach ($laterOptions as $option) {
126  $this->writeText("\n");
127  $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
128  }
129  }
130  }
131 
135  protected function describeCommand(Command $command, array $options = array())
136  {
137  $command->getSynopsis(true);
138  $command->getSynopsis(false);
139  $command->mergeApplicationDefinition(false);
140 
141  $this->writeText('<comment>Usage:</comment>', $options);
142  foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) {
143  $this->writeText("\n");
144  $this->writeText(' '.$usage, $options);
145  }
146  $this->writeText("\n");
147 
148  $definition = $command->getNativeDefinition();
149  if ($definition->getOptions() || $definition->getArguments()) {
150  $this->writeText("\n");
151  $this->describeInputDefinition($definition, $options);
152  $this->writeText("\n");
153  }
154 
155  if ($help = $command->getProcessedHelp()) {
156  $this->writeText("\n");
157  $this->writeText('<comment>Help:</comment>', $options);
158  $this->writeText("\n");
159  $this->writeText(' '.str_replace("\n", "\n ", $help), $options);
160  $this->writeText("\n");
161  }
162  }
163 
167  protected function describeApplication(Application $application, array $options = array())
168  {
169  $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
170  $description = new ApplicationDescription($application, $describedNamespace);
171 
172  if (isset($options['raw_text']) && $options['raw_text']) {
173  $width = $this->getColumnWidth($description->getCommands());
174 
175  foreach ($description->getCommands() as $command) {
176  $this->writeText(sprintf("%-${width}s %s", $command->getName(), $command->getDescription()), $options);
177  $this->writeText("\n");
178  }
179  } else {
180  if ('' != $help = $application->getHelp()) {
181  $this->writeText("$help\n\n", $options);
182  }
183 
184  $this->writeText("<comment>Usage:</comment>\n", $options);
185  $this->writeText(" command [options] [arguments]\n\n", $options);
186 
187  $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options);
188 
189  $this->writeText("\n");
190  $this->writeText("\n");
191 
192  $width = $this->getColumnWidth($description->getCommands());
193 
194  if ($describedNamespace) {
195  $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
196  } else {
197  $this->writeText('<comment>Available commands:</comment>', $options);
198  }
199 
200  // add commands by namespace
201  foreach ($description->getNamespaces() as $namespace) {
202  if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
203  $this->writeText("\n");
204  $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options);
205  }
206 
207  foreach ($namespace['commands'] as $name) {
208  $this->writeText("\n");
209  $spacingWidth = $width - strlen($name);
210  $this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $description->getCommand($name)->getDescription()), $options);
211  }
212  }
213 
214  $this->writeText("\n");
215  }
216  }
217 
221  private function writeText($content, array $options = array())
222  {
223  $this->write(
224  isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
225  isset($options['raw_output']) ? !$options['raw_output'] : true
226  );
227  }
228 
236  private function formatDefaultValue($default)
237  {
238  if (PHP_VERSION_ID < 50400) {
239  return str_replace('\/', '/', json_encode($default));
240  }
241 
242  return json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
243  }
244 
250  private function getColumnWidth(array $commands)
251  {
252  $widths = array();
253 
254  foreach ($commands as $command) {
255  $widths[] = strlen($command->getName());
256  foreach ($command->getAliases() as $alias) {
257  $widths[] = strlen($alias);
258  }
259  }
260 
261  return max($widths) + 2;
262  }
263 
269  private function calculateTotalWidthForOptions($options)
270  {
271  $totalWidth = 0;
272  foreach ($options as $option) {
273  // "-" + shortcut + ", --" + name
274  $nameLength = 1 + max(strlen($option->getShortcut()), 1) + 4 + strlen($option->getName());
275 
276  if ($option->acceptValue()) {
277  $valueLength = 1 + strlen($option->getName()); // = + value
278  $valueLength += $option->isValueOptional() ? 2 : 0; // [ + ]
279 
280  $nameLength += $valueLength;
281  }
282  $totalWidth = max($totalWidth, $nameLength);
283  }
284 
285  return $totalWidth;
286  }
287 }