From af0ff0f65bf0e7dc2680c297f35bf6119169fcc3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 27 Dec 2017 15:37:26 +0100 Subject: [PATCH] Console commands are now lazy loaded --- bin/cli | 2 ++ config/autoload/app_options.global.php | 6 ++-- module/CLI/config/cli.config.php | 34 +++++++++++-------- .../CLI/src/Command/Api/DisableKeyCommand.php | 4 ++- .../src/Command/Api/GenerateKeyCommand.php | 4 ++- .../CLI/src/Command/Api/ListKeysCommand.php | 4 ++- .../Command/Config/GenerateCharsetCommand.php | 4 ++- .../Command/Config/GenerateSecretCommand.php | 4 ++- .../Shortcode/GeneratePreviewCommand.php | 4 ++- .../Shortcode/GenerateShortcodeCommand.php | 4 ++- .../Command/Shortcode/GetVisitsCommand.php | 4 ++- .../Shortcode/ListShortcodesCommand.php | 4 ++- .../Command/Shortcode/ResolveUrlCommand.php | 4 ++- .../CLI/src/Command/Tag/CreateTagCommand.php | 4 ++- .../CLI/src/Command/Tag/DeleteTagsCommand.php | 4 ++- .../CLI/src/Command/Tag/ListTagsCommand.php | 4 ++- .../CLI/src/Command/Tag/RenameTagCommand.php | 4 ++- .../Command/Visit/ProcessVisitsCommand.php | 3 +- module/CLI/src/Factory/ApplicationFactory.php | 22 ++++++------ 19 files changed, 78 insertions(+), 45 deletions(-) diff --git a/bin/cli b/bin/cli index 263df59e..ea8cb5c3 100755 --- a/bin/cli +++ b/bin/cli @@ -1,5 +1,7 @@ #!/usr/bin/env php [ 'name' => 'Shlink', - 'version' => '1.2.0', - 'secret_key' => Common\env('SECRET_KEY'), + 'version' => '1.7.0', + 'secret_key' => env('SECRET_KEY'), ], ]; diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index be1bda6b..c1f9043b 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -9,21 +9,25 @@ return [ 'cli' => [ 'locale' => Common\env('CLI_LOCALE', 'en'), 'commands' => [ - Command\Shortcode\GenerateShortcodeCommand::class, - Command\Shortcode\ResolveUrlCommand::class, - Command\Shortcode\ListShortcodesCommand::class, - Command\Shortcode\GetVisitsCommand::class, - Command\Shortcode\GeneratePreviewCommand::class, - Command\Visit\ProcessVisitsCommand::class, - Command\Config\GenerateCharsetCommand::class, - Command\Config\GenerateSecretCommand::class, - Command\Api\GenerateKeyCommand::class, - Command\Api\DisableKeyCommand::class, - Command\Api\ListKeysCommand::class, - Command\Tag\ListTagsCommand::class, - Command\Tag\CreateTagCommand::class, - Command\Tag\RenameTagCommand::class, - Command\Tag\DeleteTagsCommand::class, + Command\Shortcode\GenerateShortcodeCommand::NAME => Command\Shortcode\GenerateShortcodeCommand::class, + Command\Shortcode\ResolveUrlCommand::NAME => Command\Shortcode\ResolveUrlCommand::class, + Command\Shortcode\ListShortcodesCommand::NAME => Command\Shortcode\ListShortcodesCommand::class, + Command\Shortcode\GetVisitsCommand::NAME => Command\Shortcode\GetVisitsCommand::class, + Command\Shortcode\GeneratePreviewCommand::NAME => Command\Shortcode\GeneratePreviewCommand::class, + + Command\Visit\ProcessVisitsCommand::NAME => Command\Visit\ProcessVisitsCommand::class, + + Command\Config\GenerateCharsetCommand::NAME => Command\Config\GenerateCharsetCommand::class, + Command\Config\GenerateSecretCommand::NAME => Command\Config\GenerateSecretCommand::class, + + Command\Api\GenerateKeyCommand::NAME => Command\Api\GenerateKeyCommand::class, + Command\Api\DisableKeyCommand::NAME => Command\Api\DisableKeyCommand::class, + Command\Api\ListKeysCommand::NAME => Command\Api\ListKeysCommand::class, + + Command\Tag\ListTagsCommand::NAME => Command\Tag\ListTagsCommand::class, + Command\Tag\CreateTagCommand::NAME => Command\Tag\CreateTagCommand::class, + Command\Tag\RenameTagCommand::NAME => Command\Tag\RenameTagCommand::class, + Command\Tag\DeleteTagsCommand::NAME => Command\Tag\DeleteTagsCommand::class, ], ], diff --git a/module/CLI/src/Command/Api/DisableKeyCommand.php b/module/CLI/src/Command/Api/DisableKeyCommand.php index e4390203..824229dd 100644 --- a/module/CLI/src/Command/Api/DisableKeyCommand.php +++ b/module/CLI/src/Command/Api/DisableKeyCommand.php @@ -12,6 +12,8 @@ use Zend\I18n\Translator\TranslatorInterface; class DisableKeyCommand extends Command { + const NAME = 'api-key:disable'; + /** * @var ApiKeyServiceInterface */ @@ -30,7 +32,7 @@ class DisableKeyCommand extends Command public function configure() { - $this->setName('api-key:disable') + $this->setName(self::NAME) ->setDescription($this->translator->translate('Disables an API key.')) ->addArgument('apiKey', InputArgument::REQUIRED, $this->translator->translate('The API key to disable')); } diff --git a/module/CLI/src/Command/Api/GenerateKeyCommand.php b/module/CLI/src/Command/Api/GenerateKeyCommand.php index 27006ceb..debcb7a5 100644 --- a/module/CLI/src/Command/Api/GenerateKeyCommand.php +++ b/module/CLI/src/Command/Api/GenerateKeyCommand.php @@ -12,6 +12,8 @@ use Zend\I18n\Translator\TranslatorInterface; class GenerateKeyCommand extends Command { + const NAME = 'api-key:generate'; + /** * @var ApiKeyServiceInterface */ @@ -30,7 +32,7 @@ class GenerateKeyCommand extends Command public function configure() { - $this->setName('api-key:generate') + $this->setName(self::NAME) ->setDescription($this->translator->translate('Generates a new valid API key.')) ->addOption( 'expirationDate', diff --git a/module/CLI/src/Command/Api/ListKeysCommand.php b/module/CLI/src/Command/Api/ListKeysCommand.php index a32c9160..d672025f 100644 --- a/module/CLI/src/Command/Api/ListKeysCommand.php +++ b/module/CLI/src/Command/Api/ListKeysCommand.php @@ -14,6 +14,8 @@ use Zend\I18n\Translator\TranslatorInterface; class ListKeysCommand extends Command { + const NAME = 'api-key:list'; + /** * @var ApiKeyServiceInterface */ @@ -32,7 +34,7 @@ class ListKeysCommand extends Command public function configure() { - $this->setName('api-key:list') + $this->setName(self::NAME) ->setDescription($this->translator->translate('Lists all the available API keys.')) ->addOption( 'enabledOnly', diff --git a/module/CLI/src/Command/Config/GenerateCharsetCommand.php b/module/CLI/src/Command/Config/GenerateCharsetCommand.php index 189dedf3..d1a1b267 100644 --- a/module/CLI/src/Command/Config/GenerateCharsetCommand.php +++ b/module/CLI/src/Command/Config/GenerateCharsetCommand.php @@ -11,6 +11,8 @@ use Zend\I18n\Translator\TranslatorInterface; class GenerateCharsetCommand extends Command { + const NAME = 'config:generate-charset'; + /** * @var TranslatorInterface */ @@ -24,7 +26,7 @@ class GenerateCharsetCommand extends Command public function configure() { - $this->setName('config:generate-charset') + $this->setName(self::NAME) ->setDescription(sprintf($this->translator->translate( 'Generates a character set sample just by shuffling the default one, "%s". ' . 'Then it can be set in the SHORTCODE_CHARS environment variable' diff --git a/module/CLI/src/Command/Config/GenerateSecretCommand.php b/module/CLI/src/Command/Config/GenerateSecretCommand.php index 6bb1e232..685ab4c5 100644 --- a/module/CLI/src/Command/Config/GenerateSecretCommand.php +++ b/module/CLI/src/Command/Config/GenerateSecretCommand.php @@ -13,6 +13,8 @@ class GenerateSecretCommand extends Command { use StringUtilsTrait; + const NAME = 'config:generate-secret'; + /** * @var TranslatorInterface */ @@ -26,7 +28,7 @@ class GenerateSecretCommand extends Command public function configure() { - $this->setName('config:generate-secret') + $this->setName(self::NAME) ->setDescription($this->translator->translate( 'Generates a random secret string that can be used for JWT token encryption' )); diff --git a/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php b/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php index 23579931..fec666bf 100644 --- a/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php +++ b/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php @@ -13,6 +13,8 @@ use Zend\I18n\Translator\TranslatorInterface; class GeneratePreviewCommand extends Command { + const NAME = 'shortcode:process-previews'; + /** * @var PreviewGeneratorInterface */ @@ -39,7 +41,7 @@ class GeneratePreviewCommand extends Command public function configure() { - $this->setName('shortcode:process-previews') + $this->setName(self::NAME) ->setDescription( $this->translator->translate( 'Processes and generates the previews for every URL, improving performance for later web requests.' diff --git a/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php b/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php index e685e876..c35f0736 100644 --- a/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php +++ b/module/CLI/src/Command/Shortcode/GenerateShortcodeCommand.php @@ -18,6 +18,8 @@ use Zend\I18n\Translator\TranslatorInterface; class GenerateShortcodeCommand extends Command { + const NAME = 'shortcode:generate'; + /** * @var UrlShortenerInterface */ @@ -44,7 +46,7 @@ class GenerateShortcodeCommand extends Command public function configure() { - $this->setName('shortcode:generate') + $this->setName(self::NAME) ->setDescription( $this->translator->translate('Generates a short code for provided URL and returns the short URL') ) diff --git a/module/CLI/src/Command/Shortcode/GetVisitsCommand.php b/module/CLI/src/Command/Shortcode/GetVisitsCommand.php index 40192dde..264d086b 100644 --- a/module/CLI/src/Command/Shortcode/GetVisitsCommand.php +++ b/module/CLI/src/Command/Shortcode/GetVisitsCommand.php @@ -17,6 +17,8 @@ use Zend\I18n\Translator\TranslatorInterface; class GetVisitsCommand extends Command { + const NAME = 'shortcode:visits'; + /** * @var VisitsTrackerInterface */ @@ -35,7 +37,7 @@ class GetVisitsCommand extends Command public function configure() { - $this->setName('shortcode:visits') + $this->setName(self::NAME) ->setDescription( $this->translator->translate('Returns the detailed visits information for provided short code') ) diff --git a/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php b/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php index 4d402522..31ba923e 100644 --- a/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php +++ b/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php @@ -19,6 +19,8 @@ class ListShortcodesCommand extends Command { use PaginatorUtilsTrait; + const NAME = 'shortcode:list'; + /** * @var ShortUrlServiceInterface */ @@ -37,7 +39,7 @@ class ListShortcodesCommand extends Command public function configure() { - $this->setName('shortcode:list') + $this->setName(self::NAME) ->setDescription($this->translator->translate('List all short URLs')) ->addOption( 'page', diff --git a/module/CLI/src/Command/Shortcode/ResolveUrlCommand.php b/module/CLI/src/Command/Shortcode/ResolveUrlCommand.php index fe79ee16..8c9af2fd 100644 --- a/module/CLI/src/Command/Shortcode/ResolveUrlCommand.php +++ b/module/CLI/src/Command/Shortcode/ResolveUrlCommand.php @@ -16,6 +16,8 @@ use Zend\I18n\Translator\TranslatorInterface; class ResolveUrlCommand extends Command { + const NAME = 'shortcode:parse'; + /** * @var UrlShortenerInterface */ @@ -34,7 +36,7 @@ class ResolveUrlCommand extends Command public function configure() { - $this->setName('shortcode:parse') + $this->setName(self::NAME) ->setDescription($this->translator->translate('Returns the long URL behind a short code')) ->addArgument( 'shortCode', diff --git a/module/CLI/src/Command/Tag/CreateTagCommand.php b/module/CLI/src/Command/Tag/CreateTagCommand.php index 183dbe51..40b28449 100644 --- a/module/CLI/src/Command/Tag/CreateTagCommand.php +++ b/module/CLI/src/Command/Tag/CreateTagCommand.php @@ -12,6 +12,8 @@ use Zend\I18n\Translator\TranslatorInterface; class CreateTagCommand extends Command { + const NAME = 'tag:create'; + /** * @var TagServiceInterface */ @@ -31,7 +33,7 @@ class CreateTagCommand extends Command protected function configure() { $this - ->setName('tag:create') + ->setName(self::NAME) ->setDescription($this->translator->translate('Creates one or more tags.')) ->addOption( 'name', diff --git a/module/CLI/src/Command/Tag/DeleteTagsCommand.php b/module/CLI/src/Command/Tag/DeleteTagsCommand.php index de7cf5f4..0654138b 100644 --- a/module/CLI/src/Command/Tag/DeleteTagsCommand.php +++ b/module/CLI/src/Command/Tag/DeleteTagsCommand.php @@ -12,6 +12,8 @@ use Zend\I18n\Translator\TranslatorInterface; class DeleteTagsCommand extends Command { + const NAME = 'tag:delete'; + /** * @var TagServiceInterface */ @@ -31,7 +33,7 @@ class DeleteTagsCommand extends Command protected function configure() { $this - ->setName('tag:delete') + ->setName(self::NAME) ->setDescription($this->translator->translate('Deletes one or more tags.')) ->addOption( 'name', diff --git a/module/CLI/src/Command/Tag/ListTagsCommand.php b/module/CLI/src/Command/Tag/ListTagsCommand.php index 44a3f48e..0474fc97 100644 --- a/module/CLI/src/Command/Tag/ListTagsCommand.php +++ b/module/CLI/src/Command/Tag/ListTagsCommand.php @@ -13,6 +13,8 @@ use Zend\I18n\Translator\TranslatorInterface; class ListTagsCommand extends Command { + const NAME = 'tag:list'; + /** * @var TagServiceInterface */ @@ -32,7 +34,7 @@ class ListTagsCommand extends Command protected function configure() { $this - ->setName('tag:list') + ->setName(self::NAME) ->setDescription($this->translator->translate('Lists existing tags.')); } diff --git a/module/CLI/src/Command/Tag/RenameTagCommand.php b/module/CLI/src/Command/Tag/RenameTagCommand.php index d5473369..a38a8da1 100644 --- a/module/CLI/src/Command/Tag/RenameTagCommand.php +++ b/module/CLI/src/Command/Tag/RenameTagCommand.php @@ -13,6 +13,8 @@ use Zend\I18n\Translator\TranslatorInterface; class RenameTagCommand extends Command { + const NAME = 'tag:rename'; + /** * @var TagServiceInterface */ @@ -32,7 +34,7 @@ class RenameTagCommand extends Command protected function configure() { $this - ->setName('tag:rename') + ->setName(self::NAME) ->setDescription($this->translator->translate('Renames one existing tag.')) ->addArgument('oldName', InputArgument::REQUIRED, $this->translator->translate('Current name of the tag.')) ->addArgument('newName', InputArgument::REQUIRED, $this->translator->translate('New name of the tag.')); diff --git a/module/CLI/src/Command/Visit/ProcessVisitsCommand.php b/module/CLI/src/Command/Visit/ProcessVisitsCommand.php index 51adc356..333a9239 100644 --- a/module/CLI/src/Command/Visit/ProcessVisitsCommand.php +++ b/module/CLI/src/Command/Visit/ProcessVisitsCommand.php @@ -15,6 +15,7 @@ use Zend\I18n\Translator\TranslatorInterface; class ProcessVisitsCommand extends Command { const LOCALHOST = '127.0.0.1'; + const NAME = 'visit:process'; /** * @var VisitServiceInterface @@ -42,7 +43,7 @@ class ProcessVisitsCommand extends Command public function configure() { - $this->setName('visit:process') + $this->setName(self::NAME) ->setDescription( $this->translator->translate('Processes visits where location is not set yet') ); diff --git a/module/CLI/src/Factory/ApplicationFactory.php b/module/CLI/src/Factory/ApplicationFactory.php index 71278dac..21ae46fd 100644 --- a/module/CLI/src/Factory/ApplicationFactory.php +++ b/module/CLI/src/Factory/ApplicationFactory.php @@ -5,8 +5,11 @@ namespace Shlinkio\Shlink\CLI\Factory; use Interop\Container\ContainerInterface; use Interop\Container\Exception\ContainerException; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use Shlinkio\Shlink\Core\Options\AppOptions; use Symfony\Component\Console\Application as CliApp; +use Symfony\Component\Console\CommandLoader\ContainerCommandLoader; use Zend\I18n\Translator\Translator; use Zend\ServiceManager\Exception\ServiceNotCreatedException; use Zend\ServiceManager\Exception\ServiceNotFoundException; @@ -20,28 +23,23 @@ class ApplicationFactory implements FactoryInterface * @param ContainerInterface $container * @param string $requestedName * @param null|array $options - * @return object + * @return CliApp + * @throws NotFoundExceptionInterface + * @throws ContainerExceptionInterface * @throws ServiceNotFoundException if unable to resolve the service. - * @throws ServiceNotCreatedException if an exception is raised when - * creating a service. + * @throws ServiceNotCreatedException if an exception is raised when creating a service. * @throws ContainerException if any other error occurs */ - public function __invoke(ContainerInterface $container, $requestedName, array $options = null) + public function __invoke(ContainerInterface $container, $requestedName, array $options = null): CliApp { $config = $container->get('config')['cli']; $appOptions = $container->get(AppOptions::class); $translator = $container->get(Translator::class); $translator->setLocale($config['locale']); - $commands = isset($config['commands']) ? $config['commands'] : []; + $commands = $config['commands'] ?? []; $app = new CliApp($appOptions->getName(), $appOptions->getVersion()); - foreach ($commands as $command) { - if (! $container->has($command)) { - continue; - } - - $app->add($container->get($command)); - } + $app->setCommandLoader(new ContainerCommandLoader($container, $commands)); return $app; }