primo commit
This commit is contained in:
235
libraries/vendor/joomla/console/src/Descriptor/ApplicationDescription.php
vendored
Normal file
235
libraries/vendor/joomla/console/src/Descriptor/ApplicationDescription.php
vendored
Normal file
@ -0,0 +1,235 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Part of the Joomla Framework Console Package
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
namespace Joomla\Console\Descriptor;
|
||||
|
||||
use Joomla\Console\Application;
|
||||
use Joomla\Console\Command\AbstractCommand;
|
||||
use Symfony\Component\Console\Exception\CommandNotFoundException;
|
||||
|
||||
/**
|
||||
* Describes an application.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
final class ApplicationDescription
|
||||
{
|
||||
/**
|
||||
* Placeholder for commands in the global namespace.
|
||||
*
|
||||
* @var string
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public const GLOBAL_NAMESPACE = '_global';
|
||||
|
||||
/**
|
||||
* The application's aliased commands.
|
||||
*
|
||||
* @var AbstractCommand[]
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private $aliases;
|
||||
|
||||
/**
|
||||
* The application being described.
|
||||
*
|
||||
* @var Application
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private $application;
|
||||
|
||||
/**
|
||||
* The application's commands.
|
||||
*
|
||||
* @var AbstractCommand[]
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private $commands;
|
||||
|
||||
/**
|
||||
* The command namespace to process.
|
||||
*
|
||||
* @var string
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private $namespace = '';
|
||||
|
||||
/**
|
||||
* The application's command namespaces.
|
||||
*
|
||||
* @var array[]
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private $namespaces;
|
||||
|
||||
/**
|
||||
* Flag indicating hidden commands should be displayed.
|
||||
*
|
||||
* @var boolean
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private $showHidden;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param Application $application The application being described.
|
||||
* @param string $namespace The command namespace to process.
|
||||
* @param boolean $showHidden Flag indicating hidden commands should be displayed.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public function __construct(Application $application, string $namespace = '', bool $showHidden = false)
|
||||
{
|
||||
$this->application = $application;
|
||||
$this->namespace = $namespace;
|
||||
$this->showHidden = $showHidden;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the application's command namespaces.
|
||||
*
|
||||
* @return array[]
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public function getNamespaces(): array
|
||||
{
|
||||
if ($this->namespaces === null) {
|
||||
$this->inspectApplication();
|
||||
}
|
||||
|
||||
return $this->namespaces;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the application's commands.
|
||||
*
|
||||
* @return AbstractCommand[]
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public function getCommands(): array
|
||||
{
|
||||
if ($this->commands === null) {
|
||||
$this->inspectApplication();
|
||||
}
|
||||
|
||||
return $this->commands;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a command by name.
|
||||
*
|
||||
* @param string $name The name of the command to retrieve.
|
||||
*
|
||||
* @return AbstractCommand
|
||||
*
|
||||
* @since 2.0.0
|
||||
* @throws CommandNotFoundException
|
||||
*/
|
||||
public function getCommand(string $name): AbstractCommand
|
||||
{
|
||||
if (!isset($this->commands[$name]) && !isset($this->aliases[$name])) {
|
||||
throw new CommandNotFoundException(sprintf('Command %s does not exist.', $name));
|
||||
}
|
||||
|
||||
return $this->commands[$name] ?? $this->aliases[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the namespace part of the command name.
|
||||
*
|
||||
* @param string $name The command name to process
|
||||
* @param ?integer $limit The maximum number of parts of the namespace
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private function extractNamespace(string $name, ?int $limit = null): string
|
||||
{
|
||||
$parts = explode(':', $name);
|
||||
array_pop($parts);
|
||||
|
||||
return implode(':', $limit === null ? $parts : \array_slice($parts, 0, $limit));
|
||||
}
|
||||
|
||||
/**
|
||||
* Inspects the application.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private function inspectApplication(): void
|
||||
{
|
||||
$this->commands = [];
|
||||
$this->namespaces = [];
|
||||
|
||||
$all = $this->application->getAllCommands($this->namespace ? $this->application->findNamespace($this->namespace) : '');
|
||||
|
||||
foreach ($this->sortCommands($all) as $namespace => $commands) {
|
||||
$names = [];
|
||||
|
||||
foreach ($commands as $name => $command) {
|
||||
if (!$command->getName() || (!$this->showHidden && $command->isHidden())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($command->getName() === $name) {
|
||||
$this->commands[$name] = $command;
|
||||
} else {
|
||||
$this->aliases[$name] = $command;
|
||||
}
|
||||
|
||||
$names[] = $name;
|
||||
}
|
||||
|
||||
$this->namespaces[$namespace] = ['id' => $namespace, 'commands' => $names];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort a set of commands.
|
||||
*
|
||||
* @param AbstractCommand[] $commands The commands to sort.
|
||||
*
|
||||
* @return AbstractCommand[][]
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private function sortCommands(array $commands): array
|
||||
{
|
||||
$namespacedCommands = [];
|
||||
$globalCommands = [];
|
||||
|
||||
foreach ($commands as $name => $command) {
|
||||
$key = $this->extractNamespace($name, 1);
|
||||
|
||||
if (!$key) {
|
||||
$globalCommands[self::GLOBAL_NAMESPACE][$name] = $command;
|
||||
} else {
|
||||
$namespacedCommands[$key][$name] = $command;
|
||||
}
|
||||
}
|
||||
|
||||
ksort($namespacedCommands);
|
||||
$namespacedCommands = array_merge($globalCommands, $namespacedCommands);
|
||||
|
||||
foreach ($namespacedCommands as &$commandsSet) {
|
||||
ksort($commandsSet);
|
||||
}
|
||||
|
||||
// Unset reference to keep scope clear
|
||||
unset($commandsSet);
|
||||
|
||||
return $namespacedCommands;
|
||||
}
|
||||
}
|
||||
262
libraries/vendor/joomla/console/src/Descriptor/TextDescriptor.php
vendored
Normal file
262
libraries/vendor/joomla/console/src/Descriptor/TextDescriptor.php
vendored
Normal file
@ -0,0 +1,262 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Part of the Joomla Framework Console Package
|
||||
*
|
||||
* @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
|
||||
* @license GNU General Public License version 2 or later; see LICENSE
|
||||
*/
|
||||
|
||||
namespace Joomla\Console\Descriptor;
|
||||
|
||||
use Joomla\Console\Application;
|
||||
use Joomla\Console\Command\AbstractCommand;
|
||||
use Joomla\String\StringHelper;
|
||||
use Symfony\Component\Console\Descriptor\TextDescriptor as SymfonyTextDescriptor;
|
||||
use Symfony\Component\Console\Input\InputDefinition;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* Text object descriptor.
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
final class TextDescriptor extends SymfonyTextDescriptor
|
||||
{
|
||||
/**
|
||||
* Describes an object.
|
||||
*
|
||||
* @param OutputInterface $output The output object to use.
|
||||
* @param object $object The object to describe.
|
||||
* @param array $options Descriptor options.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public function describe(OutputInterface $output, object $object, array $options = []): void
|
||||
{
|
||||
$this->output = $output;
|
||||
|
||||
switch (true) {
|
||||
case $object instanceof Application:
|
||||
$this->describeJoomlaApplication($object, $options);
|
||||
|
||||
break;
|
||||
|
||||
case $object instanceof AbstractCommand:
|
||||
$this->describeConsoleCommand($object, $options);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
parent::describe($output, $object, $options);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats command aliases to show them in the command description.
|
||||
*
|
||||
* @param AbstractCommand $command The command to process
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private function getCommandAliasesText(AbstractCommand $command): string
|
||||
{
|
||||
$text = '';
|
||||
$aliases = $command->getAliases();
|
||||
|
||||
if ($aliases) {
|
||||
$text = '[' . implode('|', $aliases) . '] ';
|
||||
}
|
||||
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes a command.
|
||||
*
|
||||
* @param AbstractCommand $command The command being described.
|
||||
* @param array $options Descriptor options.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private function describeConsoleCommand(AbstractCommand $command, array $options): void
|
||||
{
|
||||
$command->getSynopsis(true);
|
||||
$command->getSynopsis(false);
|
||||
$command->mergeApplicationDefinition(false);
|
||||
|
||||
$this->writeText('<comment>Usage:</comment>', $options);
|
||||
|
||||
foreach (array_merge([$command->getSynopsis(true)], $command->getAliases()) as $usage) {
|
||||
$this->writeText("\n");
|
||||
$this->writeText(' ' . $usage, $options);
|
||||
}
|
||||
|
||||
$this->writeText("\n");
|
||||
|
||||
$definition = $command->getDefinition();
|
||||
|
||||
if ($definition->getOptions() || $definition->getArguments()) {
|
||||
$this->writeText("\n");
|
||||
$this->describeInputDefinition($definition, $options);
|
||||
$this->writeText("\n");
|
||||
}
|
||||
|
||||
if ($help = $command->getProcessedHelp()) {
|
||||
$this->writeText("\n");
|
||||
$this->writeText('<comment>Help:</comment>', $options);
|
||||
$this->writeText("\n");
|
||||
$this->writeText(' ' . str_replace("\n", "\n ", $help), $options);
|
||||
$this->writeText("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes an application.
|
||||
*
|
||||
* @param Application $app The application being described.
|
||||
* @param array $options Descriptor options.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private function describeJoomlaApplication(Application $app, array $options): void
|
||||
{
|
||||
$describedNamespace = $options['namespace'] ?? '';
|
||||
$description = new ApplicationDescription($app, $describedNamespace);
|
||||
|
||||
$version = $app->getLongVersion();
|
||||
|
||||
if ($version !== '') {
|
||||
$this->writeText("$version\n\n", $options);
|
||||
}
|
||||
|
||||
$this->writeText("<comment>Usage:</comment>\n");
|
||||
$this->writeText(" command [options] [arguments]\n\n");
|
||||
|
||||
$this->describeInputDefinition(new InputDefinition($app->getDefinition()->getOptions()), $options);
|
||||
|
||||
$this->writeText("\n");
|
||||
$this->writeText("\n");
|
||||
|
||||
$commands = $description->getCommands();
|
||||
$namespaces = $description->getNamespaces();
|
||||
|
||||
if ($describedNamespace && $namespaces) {
|
||||
// Ensure all aliased commands are included when describing a specific namespace
|
||||
$describedNamespaceInfo = reset($namespaces);
|
||||
|
||||
foreach ($describedNamespaceInfo['commands'] as $name) {
|
||||
$commands[$name] = $description->getCommand($name);
|
||||
}
|
||||
}
|
||||
|
||||
$width = $this->getColumnWidth(
|
||||
\call_user_func_array(
|
||||
'array_merge',
|
||||
array_map(
|
||||
function ($namespace) use ($commands) {
|
||||
return array_intersect($namespace['commands'], array_keys($commands));
|
||||
},
|
||||
array_values($namespaces)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
if ($describedNamespace) {
|
||||
$this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
|
||||
} else {
|
||||
$this->writeText('<comment>Available commands:</comment>', $options);
|
||||
}
|
||||
|
||||
foreach ($namespaces as $namespace) {
|
||||
$namespace['commands'] = array_filter(
|
||||
$namespace['commands'],
|
||||
function ($name) use ($commands) {
|
||||
return isset($commands[$name]);
|
||||
}
|
||||
);
|
||||
|
||||
if (!$namespace['commands']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$describedNamespace && $namespace['id'] !== ApplicationDescription::GLOBAL_NAMESPACE) {
|
||||
$this->writeText("\n");
|
||||
$this->writeText(' <comment>' . $namespace['id'] . '</comment>', $options);
|
||||
}
|
||||
|
||||
foreach ($namespace['commands'] as $name) {
|
||||
$this->writeText("\n");
|
||||
$spacingWidth = $width - StringHelper::strlen($name);
|
||||
$command = $commands[$name];
|
||||
$commandAliases = $name === $command->getName() ? $this->getCommandAliasesText($command) : '';
|
||||
|
||||
$this->writeText(
|
||||
sprintf(
|
||||
' <info>%s</info>%s%s',
|
||||
$name,
|
||||
str_repeat(' ', $spacingWidth),
|
||||
$commandAliases . $command->getDescription()
|
||||
),
|
||||
$options
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->writeText("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the column width for a group of commands.
|
||||
*
|
||||
* @param AbstractCommand[]|string[] $commands The commands to use for processing a width.
|
||||
*
|
||||
* @return integer
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private function getColumnWidth(array $commands): int
|
||||
{
|
||||
$widths = [];
|
||||
|
||||
foreach ($commands as $command) {
|
||||
if ($command instanceof AbstractCommand) {
|
||||
$widths[] = StringHelper::strlen($command->getName());
|
||||
|
||||
foreach ($command->getAliases() as $alias) {
|
||||
$widths[] = StringHelper::strlen($alias);
|
||||
}
|
||||
} else {
|
||||
$widths[] = StringHelper::strlen($command);
|
||||
}
|
||||
}
|
||||
|
||||
return $widths ? max($widths) + 2 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes text to the output.
|
||||
*
|
||||
* @param string $content The message.
|
||||
* @param array $options The options to use for formatting the output.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
private function writeText($content, array $options = []): void
|
||||
{
|
||||
$this->write(
|
||||
isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
|
||||
isset($options['raw_output']) ? !$options['raw_output'] : true
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user