From 5a8a8dfceb3f16d6fe35980d48e03a93ae76188f Mon Sep 17 00:00:00 2001 From: Korvin Szanto Date: Thu, 30 Sep 2021 12:47:44 -0700 Subject: [PATCH 1/2] Allow commands to override the default options that get applied --- src/Application.php | 11 ----- src/Command/Backup/BackupCommand.php | 4 +- src/Command/Backup/InspectCommand.php | 2 +- src/Command/Backup/RestoreCommand.php | 4 +- src/Command/Command.php | 64 +++++++++++++++++++++++++++ src/Command/CommandProvider.php | 2 +- src/Command/Database/DumpCommand.php | 2 +- src/Command/Site/InfoCommand.php | 2 +- src/Command/Site/SyncCommand.php | 2 +- 9 files changed, 73 insertions(+), 20 deletions(-) diff --git a/src/Application.php b/src/Application.php index 1701db8..9047c7c 100644 --- a/src/Application.php +++ b/src/Application.php @@ -76,17 +76,6 @@ public function doRun(InputInterface $input, OutputInterface $output) return parent::doRun($input, $output); } - protected function getDefaultInputDefinition() - { - $definition = parent::getDefaultInputDefinition(); - - $definition->addOptions([ - new InputOption('--instance', '-I', InputOption::VALUE_REQUIRED, 'Specify the concrete5 directory', '.'), - ]); - - return $definition; - } - public function getHelp() { $artWidth = 42; diff --git a/src/Command/Backup/BackupCommand.php b/src/Command/Backup/BackupCommand.php index 7d51012..a42ba60 100644 --- a/src/Command/Backup/BackupCommand.php +++ b/src/Command/Backup/BackupCommand.php @@ -90,8 +90,8 @@ public function __invoke(?string $filename, InputInterface $input) */ public static function register(Container $container, Application $console): void { - $console - ->command( + static::command( + $console, 'backup:backup [filename] [--skip-core] [--temp] [--dir=]', self::class, ['backup'] diff --git a/src/Command/Backup/InspectCommand.php b/src/Command/Backup/InspectCommand.php index 70812ec..9f45473 100644 --- a/src/Command/Backup/InspectCommand.php +++ b/src/Command/Backup/InspectCommand.php @@ -85,7 +85,7 @@ public function __invoke(string $backupFile, InputInterface $input, ManifestFact public static function register(Container $container, Application $console): void { - $console->command('backup:inspect backupfile [-r|--manifest-only] [--ls=]', self::class) + self::command($console, 'backup:inspect backupfile [-r|--manifest-only] [--ls=]', self::class) ->descriptions('Inspects a Concrete installation backup', [ 'backupfile' => 'The path to the backup file to inspect', '--manifest-only' => 'Output the raw manifest json, combine with jq command.', diff --git a/src/Command/Backup/RestoreCommand.php b/src/Command/Backup/RestoreCommand.php index f176e38..3eada70 100644 --- a/src/Command/Backup/RestoreCommand.php +++ b/src/Command/Backup/RestoreCommand.php @@ -86,8 +86,8 @@ public function __invoke( public static function register(Container $container, Application $console): void { - $console - ->command( + self::command( + $console, 'backup:restore backupFile [-D|--dryrun] [--skip-db] [--skip-core] [--skip-packages] [--skip-config] [--skip-files] [--skip-application] [--skip-index] [--skip-database] [--reload-fpm-command=]', diff --git a/src/Command/Command.php b/src/Command/Command.php index 7dfbc2c..fb80660 100644 --- a/src/Command/Command.php +++ b/src/Command/Command.php @@ -4,6 +4,7 @@ namespace Concrete\Console\Command; +use Concrete\Console\Application; use Concrete\Console\Concrete\Connection\ApplicationEnabledConnectionInterface; use Concrete\Console\Concrete\Connection\ConnectionAwareInterface; use Concrete\Console\Concrete\Connection\ConnectionAwareTrait; @@ -12,6 +13,7 @@ use Concrete\Console\Installation\InstallationAwareTrait; use League\Container\ContainerAwareInterface; use League\Container\ContainerAwareTrait; +use Silly\Command\Command as SillyCommand; abstract class Command implements ContainerAwareInterface, @@ -27,6 +29,21 @@ abstract class Command implements use ConnectionAwareTrait; use InstallationAwareTrait; + /** + * Additional options to add separate from declared commands + * + * @var array + */ + protected static $extendedOptions = [ + [ + 'long' => 'instance', + 'short' => 'I', + 'description' => 'Specify the concrete5 directory', + 'default' => '.', + 'optional' => true, + ] + ]; + /** * @throws \Concrete\Console\Exception\Installation\VersionMismatch */ @@ -39,4 +56,51 @@ public function getApplication(): \Concrete\Core\Application\Application return $connection->getApplication(); } + + /** + * Static function for resolving the command instance with $extendedOptions included. + * + * @param Application $console + * @param string $expression + * @param string|callable $callable + * @param array $aliases + * + * @return SillyCommand + */ + public static function command( + Application $console, + string $expression, + $callable, + array $aliases = [] + ): SillyCommand { + $extension = []; + $descriptions = []; + $defaults = []; + foreach (static::$extendedOptions as $option) { + [ + 'long' => $long, + 'short' => $short, + 'description' => $description, + 'default' => $default, + 'optional' => $optional + ] = $option; + + // Resolve the extension string + $optionalAddition = $optional ? '=' : ''; + $shortFlag = $short ? "-{$short}|" : ''; + $longFlag = "--{$long}{$optionalAddition}"; + $extension[] = "[{$shortFlag}{$longFlag}]"; + + // resolve defaults and descriptions + $defaults[$long] = $default; + $descriptions['--' . $long] = $description; + } + + $extension = $extension ? ' ' . implode(' ', $extension) : ''; + + // Create a new command with our extension string added + return $console->command("{$expression}{$extension}", $callable, $aliases) + ->descriptions('', $descriptions) + ->defaults($defaults); + } } diff --git a/src/Command/CommandProvider.php b/src/Command/CommandProvider.php index 47a79b7..2f3f382 100644 --- a/src/Command/CommandProvider.php +++ b/src/Command/CommandProvider.php @@ -37,7 +37,7 @@ private static function siteCommands(Container $container, Application $console) private static function pharCommands(Container $container, Application $console): void { - if (\Phar::running() === '') { + if (!($_SERVER['INCLUDE_SELF_UPDATE'] ?? false) && \Phar::running() === '') { return; } Phar\SelfUpdateCommand::register($container, $console); diff --git a/src/Command/Database/DumpCommand.php b/src/Command/Database/DumpCommand.php index 490bf15..353950b 100644 --- a/src/Command/Database/DumpCommand.php +++ b/src/Command/Database/DumpCommand.php @@ -66,7 +66,7 @@ public function __invoke(string $file, Input $input) public static function register(Container $container, Application $console): void { - $console->command('database:dump [file] [-z|--gz]', self::class) + self::command($console, 'database:dump [file] [-z|--gz]', self::class) ->descriptions('Dumps the Concrete database to a file', [ 'file' => 'Filename for the dump file', '--gz' => 'Flag to gzip', diff --git a/src/Command/Site/InfoCommand.php b/src/Command/Site/InfoCommand.php index 5d06002..50277e3 100644 --- a/src/Command/Site/InfoCommand.php +++ b/src/Command/Site/InfoCommand.php @@ -31,7 +31,7 @@ public function __invoke() */ public static function register(Container $container, Application $console): void { - $console->command('site:info', self::class, ['info']) + self::command($console, 'site:info', self::class, ['info']) ->descriptions('Get info about the current Concrete installation'); } } diff --git a/src/Command/Site/SyncCommand.php b/src/Command/Site/SyncCommand.php index 5984519..b72fb70 100644 --- a/src/Command/Site/SyncCommand.php +++ b/src/Command/Site/SyncCommand.php @@ -55,7 +55,7 @@ public function __invoke(string $from, InputInterface $input) public static function register(Container $container, Application $console): void { - $console->command('site:sync from [--config=]', self::class) + self::command($console, 'site:sync from [--config=]', self::class) ->descriptions( 'Sync a remote site into this site using backups.', [ From b294ed864a7c49e850764219584965f3de93db44 Mon Sep 17 00:00:00 2001 From: Korvin Szanto Date: Thu, 30 Sep 2021 14:05:27 -0700 Subject: [PATCH 2/2] Remove Phar::running override for now --- src/Command/CommandProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/CommandProvider.php b/src/Command/CommandProvider.php index 2f3f382..47a79b7 100644 --- a/src/Command/CommandProvider.php +++ b/src/Command/CommandProvider.php @@ -37,7 +37,7 @@ private static function siteCommands(Container $container, Application $console) private static function pharCommands(Container $container, Application $console): void { - if (!($_SERVER['INCLUDE_SELF_UPDATE'] ?? false) && \Phar::running() === '') { + if (\Phar::running() === '') { return; } Phar\SelfUpdateCommand::register($container, $console);