PageRenderTime 37ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/App/Library/vendor/symfony/console/Descriptor/TextDescriptor.php

https://gitlab.com/aleksbenmaza/PPE_NEW
PHP | 320 lines | 213 code | 46 blank | 61 comment | 34 complexity | 7cb5c06f5424bc9aeaf9d98c3bbecd5f MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Descriptor;
  11. use Symfony\Component\Console\Application;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Formatter\OutputFormatter;
  14. use Symfony\Component\Console\Helper\Helper;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputDefinition;
  17. use Symfony\Component\Console\Input\InputOption;
  18. /**
  19. * Text descriptor.
  20. *
  21. * @author Jean-François Simon <contact@jfsimon.fr>
  22. *
  23. * @internal
  24. */
  25. class TextDescriptor extends Descriptor
  26. {
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function describeInputArgument(InputArgument $argument, array $options = array())
  31. {
  32. if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
  33. $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($argument->getDefault()));
  34. } else {
  35. $default = '';
  36. }
  37. $totalWidth = isset($options['total_width']) ? $options['total_width'] : Helper::strlen($argument->getName());
  38. $spacingWidth = $totalWidth - strlen($argument->getName());
  39. $this->writeText(sprintf(' <info>%s</info> %s%s%s',
  40. $argument->getName(),
  41. str_repeat(' ', $spacingWidth),
  42. // + 4 = 2 spaces before <info>, 2 spaces after </info>
  43. preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $argument->getDescription()),
  44. $default
  45. ), $options);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. protected function describeInputOption(InputOption $option, array $options = array())
  51. {
  52. if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
  53. $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault()));
  54. } else {
  55. $default = '';
  56. }
  57. $value = '';
  58. if ($option->acceptValue()) {
  59. $value = '='.strtoupper($option->getName());
  60. if ($option->isValueOptional()) {
  61. $value = '['.$value.']';
  62. }
  63. }
  64. $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions(array($option));
  65. $synopsis = sprintf('%s%s',
  66. $option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : ' ',
  67. sprintf('--%s%s', $option->getName(), $value)
  68. );
  69. $spacingWidth = $totalWidth - Helper::strlen($synopsis);
  70. $this->writeText(sprintf(' <info>%s</info> %s%s%s%s',
  71. $synopsis,
  72. str_repeat(' ', $spacingWidth),
  73. // + 4 = 2 spaces before <info>, 2 spaces after </info>
  74. preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $option->getDescription()),
  75. $default,
  76. $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''
  77. ), $options);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. protected function describeInputDefinition(InputDefinition $definition, array $options = array())
  83. {
  84. $totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
  85. foreach ($definition->getArguments() as $argument) {
  86. $totalWidth = max($totalWidth, Helper::strlen($argument->getName()));
  87. }
  88. if ($definition->getArguments()) {
  89. $this->writeText('<comment>Arguments:</comment>', $options);
  90. $this->writeText("\n");
  91. foreach ($definition->getArguments() as $argument) {
  92. $this->describeInputArgument($argument, array_merge($options, array('total_width' => $totalWidth)));
  93. $this->writeText("\n");
  94. }
  95. }
  96. if ($definition->getArguments() && $definition->getOptions()) {
  97. $this->writeText("\n");
  98. }
  99. if ($definition->getOptions()) {
  100. $laterOptions = array();
  101. $this->writeText('<comment>Options:</comment>', $options);
  102. foreach ($definition->getOptions() as $option) {
  103. if (strlen($option->getShortcut()) > 1) {
  104. $laterOptions[] = $option;
  105. continue;
  106. }
  107. $this->writeText("\n");
  108. $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
  109. }
  110. foreach ($laterOptions as $option) {
  111. $this->writeText("\n");
  112. $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
  113. }
  114. }
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. protected function describeCommand(Command $command, array $options = array())
  120. {
  121. $command->getSynopsis(true);
  122. $command->getSynopsis(false);
  123. $command->mergeApplicationDefinition(false);
  124. $this->writeText('<comment>Usage:</comment>', $options);
  125. foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) {
  126. $this->writeText("\n");
  127. $this->writeText(' '.$usage, $options);
  128. }
  129. $this->writeText("\n");
  130. $definition = $command->getNativeDefinition();
  131. if ($definition->getOptions() || $definition->getArguments()) {
  132. $this->writeText("\n");
  133. $this->describeInputDefinition($definition, $options);
  134. $this->writeText("\n");
  135. }
  136. if ($help = $command->getProcessedHelp()) {
  137. $this->writeText("\n");
  138. $this->writeText('<comment>Help:</comment>', $options);
  139. $this->writeText("\n");
  140. $this->writeText(' '.str_replace("\n", "\n ", $help), $options);
  141. $this->writeText("\n");
  142. }
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. protected function describeApplication(Application $application, array $options = array())
  148. {
  149. $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
  150. $description = new ApplicationDescription($application, $describedNamespace);
  151. if (isset($options['raw_text']) && $options['raw_text']) {
  152. $width = $this->getColumnWidth($description->getCommands());
  153. foreach ($description->getCommands() as $command) {
  154. $this->writeText(sprintf("%-{$width}s %s", $command->getName(), $command->getDescription()), $options);
  155. $this->writeText("\n");
  156. }
  157. } else {
  158. if ('' != $help = $application->getHelp()) {
  159. $this->writeText("$help\n\n", $options);
  160. }
  161. $this->writeText("<comment>Usage:</comment>\n", $options);
  162. $this->writeText(" command [options] [arguments]\n\n", $options);
  163. $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options);
  164. $this->writeText("\n");
  165. $this->writeText("\n");
  166. $width = $this->getColumnWidth($description->getCommands());
  167. if ($describedNamespace) {
  168. $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
  169. } else {
  170. $this->writeText('<comment>Available commands:</comment>', $options);
  171. }
  172. // add commands by namespace
  173. $commands = $description->getCommands();
  174. foreach ($description->getNamespaces() as $namespace) {
  175. if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
  176. $this->writeText("\n");
  177. $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options);
  178. }
  179. foreach ($namespace['commands'] as $name) {
  180. if (isset($commands[$name])) {
  181. $this->writeText("\n");
  182. $spacingWidth = $width - Helper::strlen($name);
  183. $command = $commands[$name];
  184. $commandAliases = $this->getCommandAliasesText($command);
  185. $this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options);
  186. }
  187. }
  188. }
  189. $this->writeText("\n");
  190. }
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. private function writeText($content, array $options = array())
  196. {
  197. $this->write(
  198. isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
  199. isset($options['raw_output']) ? !$options['raw_output'] : true
  200. );
  201. }
  202. /**
  203. * Formats command aliases to show them in the command description.
  204. *
  205. * @param Command $command
  206. *
  207. * @return string
  208. */
  209. private function getCommandAliasesText($command)
  210. {
  211. $text = '';
  212. $aliases = $command->getAliases();
  213. if ($aliases) {
  214. $text = '['.implode('|', $aliases).'] ';
  215. }
  216. return $text;
  217. }
  218. /**
  219. * Formats input option/argument default value.
  220. *
  221. * @param mixed $default
  222. *
  223. * @return string
  224. */
  225. private function formatDefaultValue($default)
  226. {
  227. if (is_string($default)) {
  228. $default = OutputFormatter::escape($default);
  229. } elseif (is_array($default)) {
  230. foreach ($default as $key => $value) {
  231. if (is_string($value)) {
  232. $default[$key] = OutputFormatter::escape($value);
  233. }
  234. }
  235. }
  236. return str_replace('\\\\', '\\', json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
  237. }
  238. /**
  239. * @param Command[] $commands
  240. *
  241. * @return int
  242. */
  243. private function getColumnWidth(array $commands)
  244. {
  245. $widths = array();
  246. foreach ($commands as $command) {
  247. $widths[] = Helper::strlen($command->getName());
  248. foreach ($command->getAliases() as $alias) {
  249. $widths[] = Helper::strlen($alias);
  250. }
  251. }
  252. return max($widths) + 2;
  253. }
  254. /**
  255. * @param InputOption[] $options
  256. *
  257. * @return int
  258. */
  259. private function calculateTotalWidthForOptions($options)
  260. {
  261. $totalWidth = 0;
  262. foreach ($options as $option) {
  263. // "-" + shortcut + ", --" + name
  264. $nameLength = 1 + max(Helper::strlen($option->getShortcut()), 1) + 4 + Helper::strlen($option->getName());
  265. if ($option->acceptValue()) {
  266. $valueLength = 1 + Helper::strlen($option->getName()); // = + value
  267. $valueLength += $option->isValueOptional() ? 2 : 0; // [ + ]
  268. $nameLength += $valueLength;
  269. }
  270. $totalWidth = max($totalWidth, $nameLength);
  271. }
  272. return $totalWidth;
  273. }
  274. }