comparison vendor/symfony/console/Descriptor/TextDescriptor.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 1fec387a4317
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
29 class TextDescriptor extends Descriptor 29 class TextDescriptor extends Descriptor
30 { 30 {
31 /** 31 /**
32 * {@inheritdoc} 32 * {@inheritdoc}
33 */ 33 */
34 protected function describeInputArgument(InputArgument $argument, array $options = array()) 34 protected function describeInputArgument(InputArgument $argument, array $options = [])
35 { 35 {
36 if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) { 36 if (null !== $argument->getDefault() && (!\is_array($argument->getDefault()) || \count($argument->getDefault()))) {
37 $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($argument->getDefault())); 37 $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($argument->getDefault()));
38 } else { 38 } else {
39 $default = ''; 39 $default = '';
40 } 40 }
41 41
42 $totalWidth = isset($options['total_width']) ? $options['total_width'] : Helper::strlen($argument->getName()); 42 $totalWidth = isset($options['total_width']) ? $options['total_width'] : Helper::strlen($argument->getName());
43 $spacingWidth = $totalWidth - strlen($argument->getName()); 43 $spacingWidth = $totalWidth - \strlen($argument->getName());
44 44
45 $this->writeText(sprintf(' <info>%s</info> %s%s%s', 45 $this->writeText(sprintf(' <info>%s</info> %s%s%s',
46 $argument->getName(), 46 $argument->getName(),
47 str_repeat(' ', $spacingWidth), 47 str_repeat(' ', $spacingWidth),
48 // + 4 = 2 spaces before <info>, 2 spaces after </info> 48 // + 4 = 2 spaces before <info>, 2 spaces after </info>
52 } 52 }
53 53
54 /** 54 /**
55 * {@inheritdoc} 55 * {@inheritdoc}
56 */ 56 */
57 protected function describeInputOption(InputOption $option, array $options = array()) 57 protected function describeInputOption(InputOption $option, array $options = [])
58 { 58 {
59 if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) { 59 if ($option->acceptValue() && null !== $option->getDefault() && (!\is_array($option->getDefault()) || \count($option->getDefault()))) {
60 $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault())); 60 $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault()));
61 } else { 61 } else {
62 $default = ''; 62 $default = '';
63 } 63 }
64 64
69 if ($option->isValueOptional()) { 69 if ($option->isValueOptional()) {
70 $value = '['.$value.']'; 70 $value = '['.$value.']';
71 } 71 }
72 } 72 }
73 73
74 $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions(array($option)); 74 $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions([$option]);
75 $synopsis = sprintf('%s%s', 75 $synopsis = sprintf('%s%s',
76 $option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : ' ', 76 $option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : ' ',
77 sprintf('--%s%s', $option->getName(), $value) 77 sprintf('--%s%s', $option->getName(), $value)
78 ); 78 );
79 79
90 } 90 }
91 91
92 /** 92 /**
93 * {@inheritdoc} 93 * {@inheritdoc}
94 */ 94 */
95 protected function describeInputDefinition(InputDefinition $definition, array $options = array()) 95 protected function describeInputDefinition(InputDefinition $definition, array $options = [])
96 { 96 {
97 $totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions()); 97 $totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
98 foreach ($definition->getArguments() as $argument) { 98 foreach ($definition->getArguments() as $argument) {
99 $totalWidth = max($totalWidth, Helper::strlen($argument->getName())); 99 $totalWidth = max($totalWidth, Helper::strlen($argument->getName()));
100 } 100 }
101 101
102 if ($definition->getArguments()) { 102 if ($definition->getArguments()) {
103 $this->writeText('<comment>Arguments:</comment>', $options); 103 $this->writeText('<comment>Arguments:</comment>', $options);
104 $this->writeText("\n"); 104 $this->writeText("\n");
105 foreach ($definition->getArguments() as $argument) { 105 foreach ($definition->getArguments() as $argument) {
106 $this->describeInputArgument($argument, array_merge($options, array('total_width' => $totalWidth))); 106 $this->describeInputArgument($argument, array_merge($options, ['total_width' => $totalWidth]));
107 $this->writeText("\n"); 107 $this->writeText("\n");
108 } 108 }
109 } 109 }
110 110
111 if ($definition->getArguments() && $definition->getOptions()) { 111 if ($definition->getArguments() && $definition->getOptions()) {
112 $this->writeText("\n"); 112 $this->writeText("\n");
113 } 113 }
114 114
115 if ($definition->getOptions()) { 115 if ($definition->getOptions()) {
116 $laterOptions = array(); 116 $laterOptions = [];
117 117
118 $this->writeText('<comment>Options:</comment>', $options); 118 $this->writeText('<comment>Options:</comment>', $options);
119 foreach ($definition->getOptions() as $option) { 119 foreach ($definition->getOptions() as $option) {
120 if (strlen($option->getShortcut()) > 1) { 120 if (\strlen($option->getShortcut()) > 1) {
121 $laterOptions[] = $option; 121 $laterOptions[] = $option;
122 continue; 122 continue;
123 } 123 }
124 $this->writeText("\n"); 124 $this->writeText("\n");
125 $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth))); 125 $this->describeInputOption($option, array_merge($options, ['total_width' => $totalWidth]));
126 } 126 }
127 foreach ($laterOptions as $option) { 127 foreach ($laterOptions as $option) {
128 $this->writeText("\n"); 128 $this->writeText("\n");
129 $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth))); 129 $this->describeInputOption($option, array_merge($options, ['total_width' => $totalWidth]));
130 } 130 }
131 } 131 }
132 } 132 }
133 133
134 /** 134 /**
135 * {@inheritdoc} 135 * {@inheritdoc}
136 */ 136 */
137 protected function describeCommand(Command $command, array $options = array()) 137 protected function describeCommand(Command $command, array $options = [])
138 { 138 {
139 $command->getSynopsis(true); 139 $command->getSynopsis(true);
140 $command->getSynopsis(false); 140 $command->getSynopsis(false);
141 $command->mergeApplicationDefinition(false); 141 $command->mergeApplicationDefinition(false);
142 142
143 $this->writeText('<comment>Usage:</comment>', $options); 143 $this->writeText('<comment>Usage:</comment>', $options);
144 foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) { 144 foreach (array_merge([$command->getSynopsis(true)], $command->getAliases(), $command->getUsages()) as $usage) {
145 $this->writeText("\n"); 145 $this->writeText("\n");
146 $this->writeText(' '.OutputFormatter::escape($usage), $options); 146 $this->writeText(' '.OutputFormatter::escape($usage), $options);
147 } 147 }
148 $this->writeText("\n"); 148 $this->writeText("\n");
149 149
164 } 164 }
165 165
166 /** 166 /**
167 * {@inheritdoc} 167 * {@inheritdoc}
168 */ 168 */
169 protected function describeApplication(Application $application, array $options = array()) 169 protected function describeApplication(Application $application, array $options = [])
170 { 170 {
171 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null; 171 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
172 $description = new ApplicationDescription($application, $describedNamespace); 172 $description = new ApplicationDescription($application, $describedNamespace);
173 173
174 if (isset($options['raw_text']) && $options['raw_text']) { 174 if (isset($options['raw_text']) && $options['raw_text']) {
200 $commands[$name] = $description->getCommand($name); 200 $commands[$name] = $description->getCommand($name);
201 } 201 }
202 } 202 }
203 203
204 // calculate max. width based on available commands per namespace 204 // calculate max. width based on available commands per namespace
205 $width = $this->getColumnWidth(call_user_func_array('array_merge', array_map(function ($namespace) use ($commands) { 205 $width = $this->getColumnWidth(\call_user_func_array('array_merge', array_map(function ($namespace) use ($commands) {
206 return array_intersect($namespace['commands'], array_keys($commands)); 206 return array_intersect($namespace['commands'], array_keys($commands));
207 }, $namespaces))); 207 }, $namespaces)));
208 208
209 if ($describedNamespace) { 209 if ($describedNamespace) {
210 $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options); 210 $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
240 } 240 }
241 241
242 /** 242 /**
243 * {@inheritdoc} 243 * {@inheritdoc}
244 */ 244 */
245 private function writeText($content, array $options = array()) 245 private function writeText($content, array $options = [])
246 { 246 {
247 $this->write( 247 $this->write(
248 isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content, 248 isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
249 isset($options['raw_output']) ? !$options['raw_output'] : true 249 isset($options['raw_output']) ? !$options['raw_output'] : true
250 ); 250 );
278 { 278 {
279 if (INF === $default) { 279 if (INF === $default) {
280 return 'INF'; 280 return 'INF';
281 } 281 }
282 282
283 if (is_string($default)) { 283 if (\is_string($default)) {
284 $default = OutputFormatter::escape($default); 284 $default = OutputFormatter::escape($default);
285 } elseif (is_array($default)) { 285 } elseif (\is_array($default)) {
286 foreach ($default as $key => $value) { 286 foreach ($default as $key => $value) {
287 if (is_string($value)) { 287 if (\is_string($value)) {
288 $default[$key] = OutputFormatter::escape($value); 288 $default[$key] = OutputFormatter::escape($value);
289 } 289 }
290 } 290 }
291 } 291 }
292 292
298 * 298 *
299 * @return int 299 * @return int
300 */ 300 */
301 private function getColumnWidth(array $commands) 301 private function getColumnWidth(array $commands)
302 { 302 {
303 $widths = array(); 303 $widths = [];
304 304
305 foreach ($commands as $command) { 305 foreach ($commands as $command) {
306 if ($command instanceof Command) { 306 if ($command instanceof Command) {
307 $widths[] = Helper::strlen($command->getName()); 307 $widths[] = Helper::strlen($command->getName());
308 foreach ($command->getAliases() as $alias) { 308 foreach ($command->getAliases() as $alias) {