TYPO3  7.6
MarkdownDescriptor.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->write(
35  '**'.$argument->getName().':**'."\n\n"
36  .'* Name: '.($argument->getName() ?: '<none>')."\n"
37  .'* Is required: '.($argument->isRequired() ? 'yes' : 'no')."\n"
38  .'* Is array: '.($argument->isArray() ? 'yes' : 'no')."\n"
39  .'* Description: '.preg_replace('/\s*[\r\n]\s*/', "\n ", $argument->getDescription() ?: '<none>')."\n"
40  .'* Default: `'.str_replace("\n", '', var_export($argument->getDefault(), true)).'`'
41  );
42  }
43 
47  protected function describeInputOption(InputOption $option, array $options = array())
48  {
49  $this->write(
50  '**'.$option->getName().':**'."\n\n"
51  .'* Name: `--'.$option->getName().'`'."\n"
52  .'* Shortcut: '.($option->getShortcut() ? '`-'.implode('|-', explode('|', $option->getShortcut())).'`' : '<none>')."\n"
53  .'* Accept value: '.($option->acceptValue() ? 'yes' : 'no')."\n"
54  .'* Is value required: '.($option->isValueRequired() ? 'yes' : 'no')."\n"
55  .'* Is multiple: '.($option->isArray() ? 'yes' : 'no')."\n"
56  .'* Description: '.preg_replace('/\s*[\r\n]\s*/', "\n ", $option->getDescription() ?: '<none>')."\n"
57  .'* Default: `'.str_replace("\n", '', var_export($option->getDefault(), true)).'`'
58  );
59  }
60 
64  protected function describeInputDefinition(InputDefinition $definition, array $options = array())
65  {
66  if ($showArguments = count($definition->getArguments()) > 0) {
67  $this->write('### Arguments:');
68  foreach ($definition->getArguments() as $argument) {
69  $this->write("\n\n");
70  $this->write($this->describeInputArgument($argument));
71  }
72  }
73 
74  if (count($definition->getOptions()) > 0) {
75  if ($showArguments) {
76  $this->write("\n\n");
77  }
78 
79  $this->write('### Options:');
80  foreach ($definition->getOptions() as $option) {
81  $this->write("\n\n");
82  $this->write($this->describeInputOption($option));
83  }
84  }
85  }
86 
90  protected function describeCommand(Command $command, array $options = array())
91  {
92  $command->getSynopsis();
93  $command->mergeApplicationDefinition(false);
94 
95  $this->write(
96  $command->getName()."\n"
97  .str_repeat('-', strlen($command->getName()))."\n\n"
98  .'* Description: '.($command->getDescription() ?: '<none>')."\n"
99  .'* Usage:'."\n\n"
100  .array_reduce(array_merge(array($command->getSynopsis()), $command->getAliases(), $command->getUsages()), function ($carry, $usage) {
101  return $carry .= ' * `'.$usage.'`'."\n";
102  })
103  );
104 
105  if ($help = $command->getProcessedHelp()) {
106  $this->write("\n");
107  $this->write($help);
108  }
109 
110  if ($command->getNativeDefinition()) {
111  $this->write("\n\n");
112  $this->describeInputDefinition($command->getNativeDefinition());
113  }
114  }
115 
119  protected function describeApplication(Application $application, array $options = array())
120  {
121  $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
122  $description = new ApplicationDescription($application, $describedNamespace);
123 
124  $this->write($application->getName()."\n".str_repeat('=', strlen($application->getName())));
125 
126  foreach ($description->getNamespaces() as $namespace) {
127  if (ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
128  $this->write("\n\n");
129  $this->write('**'.$namespace['id'].':**');
130  }
131 
132  $this->write("\n\n");
133  $this->write(implode("\n", array_map(function ($commandName) {
134  return '* '.$commandName;
135  }, $namespace['commands'])));
136  }
137 
138  foreach ($description->getCommands() as $command) {
139  $this->write("\n\n");
140  $this->write($this->describeCommand($command));
141  }
142  }
143 }