‪TYPO3CMS  ‪main
TextDescriptor.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Symfony\Component\Console\Application;
21 use Symfony\Component\Console\Descriptor\ApplicationDescription;
22 use Symfony\Component\Console\Descriptor\TextDescriptor as SymfonyTextDescriptor;
23 use Symfony\Component\Console\Helper\Helper;
24 use Symfony\Component\Console\Input\InputDefinition;
26 
32 class ‪TextDescriptor extends SymfonyTextDescriptor
33 {
35  private bool ‪$degraded;
36 
38  {
39  $this->commandRegistry = ‪$commandRegistry;
40  $this->degraded = ‪$degraded;
41  }
42 
46  protected function ‪describeApplication(Application $application, array $options = []): void
47  {
48  $describedNamespace = $options['namespace'] ?? null;
49  $rawOutput = $options['raw_text'] ?? false;
50 
51  $commands = $this->commandRegistry->filter($describedNamespace);
52 
53  if ($rawOutput) {
54  $width = $this->‪getColumnWidth(['' => ['commands' => array_keys($commands)]]);
55 
56  foreach ($commands as $command) {
57  $this->write(sprintf("%-{$width}s %s\n", $command['name'], strip_tags($command['description'] ?? '')), true);
58  }
59  return;
60  }
61 
62  if ($this->degraded) {
63  $this->write("<error>Failed to boot dependency injection, only lowlevel commands are available.</error>\n\n", true);
64  }
65 
66  $namespaces = $this->commandRegistry->getNamespaces();
67  $help = $application->getHelp();
68  if ($help !== '') {
69  $this->write($help . "\n\n", true);
70  }
71 
72  $this->write("<comment>Usage:</comment>\n", true);
73  $this->write(" command [options] [arguments]\n\n");
74 
75  $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()));
76 
77  $this->write("\n\n");
78 
79  if ($describedNamespace) {
80  $this->write(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), true);
81  $namespace = $namespaces[$describedNamespace] ?? [];
82  $width = $this->‪getColumnWidth(['' => $namespace]);
83  $this->‪describeNamespace($namespace, $commands, $width);
84  } else {
85  $this->write('<comment>Available commands:</comment>', true);
86  // calculate max. width based on available commands per namespace
87  $width = $this->‪getColumnWidth($namespaces);
88  foreach ($namespaces as $namespace) {
89  if ($namespace['id'] !== ApplicationDescription::GLOBAL_NAMESPACE) {
90  $this->write("\n");
91  $this->write(' <comment>' . $namespace['id'] . '</comment>', true);
92  }
93  $this->‪describeNamespace($namespace, $commands, $width);
94  }
95  }
96 
97  $this->write("\n");
98 
99  if ($this->degraded) {
100  $this->write("\n<error>Failed to boot dependency injection, only lowlevel commands are available.</error>\n", true);
101  }
102  }
103 
104  private function ‪describeNamespace(array $namespace, array $commands, int $width): void
105  {
106  foreach ($namespace['commands'] as $name) {
107  $this->write("\n");
108  $spacingWidth = $width - ‪Helper::length($name);
109  $command = $commands[$name];
110 
111  $aliases = count($command['aliases']) ? '[' . implode('|', $command['aliases']) . '] ' : '';
112  $this->write(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $aliases . $command['description']), true);
113  }
114  }
115 
116  private function ‪getColumnWidth(array $namespaces): int
117  {
118  $widths = [];
119  foreach ($namespaces as $name => $namespace) {
120  $widths[] = ‪Helper::length($name);
121  foreach ($namespace['commands'] as $commandName) {
122  $widths[] = ‪Helper::length($commandName);
123  }
124  }
125 
126  return $widths ? max($widths) + 2 : 0;
127  }
128 }
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\length
‪@ length
Definition: HashType.php:33
‪TYPO3\CMS\Core\Command\Descriptor\TextDescriptor\describeNamespace
‪describeNamespace(array $namespace, array $commands, int $width)
Definition: TextDescriptor.php:104
‪TYPO3\CMS\Core\Command\Descriptor\TextDescriptor\$commandRegistry
‪CommandRegistry $commandRegistry
Definition: TextDescriptor.php:34
‪TYPO3\CMS\Core\Command\Descriptor\TextDescriptor\describeApplication
‪describeApplication(Application $application, array $options=[])
Definition: TextDescriptor.php:46
‪TYPO3\CMS\Core\Command\Descriptor\TextDescriptor\__construct
‪__construct(CommandRegistry $commandRegistry, bool $degraded)
Definition: TextDescriptor.php:37
‪TYPO3\CMS\Core\Command\Descriptor\TextDescriptor\getColumnWidth
‪getColumnWidth(array $namespaces)
Definition: TextDescriptor.php:116
‪TYPO3\CMS\Core\Console\CommandRegistry
Definition: CommandRegistry.php:31
‪TYPO3\CMS\Core\Command\Descriptor
Definition: TextDescriptor.php:18
‪TYPO3\CMS\Core\Command\Descriptor\TextDescriptor\$degraded
‪bool $degraded
Definition: TextDescriptor.php:35
‪TYPO3\CMS\Core\Command\Descriptor\TextDescriptor
Definition: TextDescriptor.php:33