From ba2053bd3a0f0cfd9b734d4ab0a6ea8ad5ff2a39 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 22 Jul 2017 13:33:32 +0200 Subject: [PATCH] Dropped AnnotatedFactory in commands and replaced by ConfigAbstractFactory --- composer.json | 2 + module/CLI/config/dependencies.config.php | 68 ++++++++++++---- .../CLI/src/Command/Api/DisableKeyCommand.php | 9 --- .../src/Command/Api/GenerateKeyCommand.php | 11 +-- .../CLI/src/Command/Api/ListKeysCommand.php | 11 +-- .../Command/Config/GenerateCharsetCommand.php | 9 +-- .../Command/Config/GenerateSecretCommand.php | 9 +-- .../Shortcode/GeneratePreviewCommand.php | 11 --- .../Shortcode/GenerateShortcodeCommand.php | 10 --- .../Command/Shortcode/GetVisitsCommand.php | 9 --- .../Shortcode/ListShortcodesCommand.php | 12 +-- .../Command/Shortcode/ResolveUrlCommand.php | 9 --- .../CLI/src/Command/Tag/CreateTagCommand.php | 10 --- .../CLI/src/Command/Tag/DeleteTagsCommand.php | 10 --- .../CLI/src/Command/Tag/ListTagsCommand.php | 10 --- .../CLI/src/Command/Tag/RenameTagCommand.php | 10 --- .../Command/Visit/ProcessVisitsCommand.php | 11 --- module/Common/config/dependencies.config.php | 3 + .../DottedAccessConfigAbstractFactory.php | 79 +++++++++++++++++++ 19 files changed, 141 insertions(+), 162 deletions(-) create mode 100644 module/Common/src/Factory/DottedAccessConfigAbstractFactory.php diff --git a/composer.json b/composer.json index 637a66ab..4e909e5e 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,8 @@ "acelaya/zsm-annotated-services": "^1.0", "acelaya/ze-content-based-error-handler": "^2.0", "doctrine/orm": "^2.5", + "doctrine/collections": "^1.4 <1.5", + "doctrine/common": "^2.7 <2.8", "guzzlehttp/guzzle": "^6.2", "symfony/console": "^3.0", "symfony/process": "^3.0", diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index 565ab8bc..641b58db 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -1,8 +1,14 @@ [ Application::class => ApplicationFactory::class, - Command\Shortcode\GenerateShortcodeCommand::class => AnnotatedFactory::class, - Command\Shortcode\ResolveUrlCommand::class => AnnotatedFactory::class, - Command\Shortcode\ListShortcodesCommand::class => AnnotatedFactory::class, - Command\Shortcode\GetVisitsCommand::class => AnnotatedFactory::class, - Command\Shortcode\GeneratePreviewCommand::class => AnnotatedFactory::class, - Command\Visit\ProcessVisitsCommand::class => AnnotatedFactory::class, - Command\Config\GenerateCharsetCommand::class => AnnotatedFactory::class, - Command\Config\GenerateSecretCommand::class => AnnotatedFactory::class, - Command\Api\GenerateKeyCommand::class => AnnotatedFactory::class, - Command\Api\DisableKeyCommand::class => AnnotatedFactory::class, - Command\Api\ListKeysCommand::class => AnnotatedFactory::class, - Command\Tag\ListTagsCommand::class => AnnotatedFactory::class, - Command\Tag\CreateTagCommand::class => AnnotatedFactory::class, - Command\Tag\RenameTagCommand::class => AnnotatedFactory::class, - Command\Tag\DeleteTagsCommand::class => AnnotatedFactory::class, + Command\Shortcode\GenerateShortcodeCommand::class => ConfigAbstractFactory::class, + Command\Shortcode\ResolveUrlCommand::class => ConfigAbstractFactory::class, + Command\Shortcode\ListShortcodesCommand::class => ConfigAbstractFactory::class, + Command\Shortcode\GetVisitsCommand::class => ConfigAbstractFactory::class, + Command\Shortcode\GeneratePreviewCommand::class => ConfigAbstractFactory::class, + Command\Visit\ProcessVisitsCommand::class => ConfigAbstractFactory::class, + Command\Config\GenerateCharsetCommand::class => ConfigAbstractFactory::class, + Command\Config\GenerateSecretCommand::class => ConfigAbstractFactory::class, + Command\Api\GenerateKeyCommand::class => ConfigAbstractFactory::class, + Command\Api\DisableKeyCommand::class => ConfigAbstractFactory::class, + Command\Api\ListKeysCommand::class => ConfigAbstractFactory::class, + Command\Tag\ListTagsCommand::class => ConfigAbstractFactory::class, + Command\Tag\CreateTagCommand::class => ConfigAbstractFactory::class, + Command\Tag\RenameTagCommand::class => ConfigAbstractFactory::class, + Command\Tag\DeleteTagsCommand::class => ConfigAbstractFactory::class, ], ], + ConfigAbstractFactory::class => [ + Command\Shortcode\GenerateShortcodeCommand::class => [ + Service\UrlShortener::class, + 'translator', + 'config.url_shortener.domain', + ], + Command\Shortcode\ResolveUrlCommand::class => [Service\UrlShortener::class, 'translator'], + Command\Shortcode\ListShortcodesCommand::class => [Service\ShortUrlService::class, 'translator'], + Command\Shortcode\GetVisitsCommand::class => [Service\VisitsTracker::class, 'translator'], + Command\Shortcode\GeneratePreviewCommand::class => [ + Service\ShortUrlService::class, + PreviewGenerator::class, + 'translator' + ], + Command\Visit\ProcessVisitsCommand::class => [ + Service\VisitService::class, + IpLocationResolver::class, + 'translator' + ], + Command\Config\GenerateCharsetCommand::class => ['translator'], + Command\Config\GenerateSecretCommand::class => ['translator'], + Command\Api\GenerateKeyCommand::class => [ApiKeyService::class, 'translator'], + Command\Api\DisableKeyCommand::class => [ApiKeyService::class, 'translator'], + Command\Api\ListKeysCommand::class => [ApiKeyService::class, 'translator'], + Command\Tag\ListTagsCommand::class => [Service\Tag\TagService::class, Translator::class], + Command\Tag\CreateTagCommand::class => [Service\Tag\TagService::class, Translator::class], + Command\Tag\RenameTagCommand::class => [Service\Tag\TagService::class, Translator::class], + Command\Tag\DeleteTagsCommand::class => [Service\Tag\TagService::class, Translator::class], + ], + ]; diff --git a/module/CLI/src/Command/Api/DisableKeyCommand.php b/module/CLI/src/Command/Api/DisableKeyCommand.php index 48d9d564..fbf02992 100644 --- a/module/CLI/src/Command/Api/DisableKeyCommand.php +++ b/module/CLI/src/Command/Api/DisableKeyCommand.php @@ -1,8 +1,6 @@ apiKeyService = $apiKeyService; diff --git a/module/CLI/src/Command/Api/GenerateKeyCommand.php b/module/CLI/src/Command/Api/GenerateKeyCommand.php index 75d94e65..7d436669 100644 --- a/module/CLI/src/Command/Api/GenerateKeyCommand.php +++ b/module/CLI/src/Command/Api/GenerateKeyCommand.php @@ -1,8 +1,6 @@ apiKeyService = $apiKeyService; $this->translator = $translator; - parent::__construct(null); + parent::__construct(); } public function configure() diff --git a/module/CLI/src/Command/Api/ListKeysCommand.php b/module/CLI/src/Command/Api/ListKeysCommand.php index b08c1ece..8be23ec1 100644 --- a/module/CLI/src/Command/Api/ListKeysCommand.php +++ b/module/CLI/src/Command/Api/ListKeysCommand.php @@ -1,9 +1,7 @@ apiKeyService = $apiKeyService; $this->translator = $translator; - parent::__construct(null); + parent::__construct(); } public function configure() diff --git a/module/CLI/src/Command/Config/GenerateCharsetCommand.php b/module/CLI/src/Command/Config/GenerateCharsetCommand.php index 22369934..bf0f2b6d 100644 --- a/module/CLI/src/Command/Config/GenerateCharsetCommand.php +++ b/module/CLI/src/Command/Config/GenerateCharsetCommand.php @@ -1,7 +1,6 @@ translator = $translator; - parent::__construct(null); + parent::__construct(); } public function configure() diff --git a/module/CLI/src/Command/Config/GenerateSecretCommand.php b/module/CLI/src/Command/Config/GenerateSecretCommand.php index bef5c86a..e27bf751 100644 --- a/module/CLI/src/Command/Config/GenerateSecretCommand.php +++ b/module/CLI/src/Command/Config/GenerateSecretCommand.php @@ -1,7 +1,6 @@ translator = $translator; - parent::__construct(null); + parent::__construct(); } public function configure() diff --git a/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php b/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php index b224ad61..68b7189e 100644 --- a/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php +++ b/module/CLI/src/Command/Shortcode/GeneratePreviewCommand.php @@ -1,11 +1,8 @@ visitsTracker = $visitsTracker; diff --git a/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php b/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php index a8594db7..1c8e17f8 100644 --- a/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php +++ b/module/CLI/src/Command/Shortcode/ListShortcodesCommand.php @@ -1,10 +1,8 @@ shortUrlService = $shortUrlService; @@ -88,12 +79,11 @@ class ListShortcodesCommand extends Command public function execute(InputInterface $input, OutputInterface $output) { - $page = intval($input->getOption('page')); + $page = (int) $input->getOption('page'); $searchTerm = $input->getOption('searchTerm'); $tags = $input->getOption('tags'); $tags = ! empty($tags) ? explode(',', $tags) : []; $showTags = $input->getOption('showTags'); - $orderBy = $input->getOption('orderBy'); /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); diff --git a/module/CLI/src/Command/Shortcode/ResolveUrlCommand.php b/module/CLI/src/Command/Shortcode/ResolveUrlCommand.php index a94b47ad..3344b7af 100644 --- a/module/CLI/src/Command/Shortcode/ResolveUrlCommand.php +++ b/module/CLI/src/Command/Shortcode/ResolveUrlCommand.php @@ -1,9 +1,7 @@ urlShortener = $urlShortener; diff --git a/module/CLI/src/Command/Tag/CreateTagCommand.php b/module/CLI/src/Command/Tag/CreateTagCommand.php index 8a06d38c..530ee569 100644 --- a/module/CLI/src/Command/Tag/CreateTagCommand.php +++ b/module/CLI/src/Command/Tag/CreateTagCommand.php @@ -1,14 +1,11 @@ tagService = $tagService; diff --git a/module/CLI/src/Command/Tag/DeleteTagsCommand.php b/module/CLI/src/Command/Tag/DeleteTagsCommand.php index 0a4e271b..394eac40 100644 --- a/module/CLI/src/Command/Tag/DeleteTagsCommand.php +++ b/module/CLI/src/Command/Tag/DeleteTagsCommand.php @@ -1,14 +1,11 @@ tagService = $tagService; diff --git a/module/CLI/src/Command/Tag/ListTagsCommand.php b/module/CLI/src/Command/Tag/ListTagsCommand.php index eb120226..97037406 100644 --- a/module/CLI/src/Command/Tag/ListTagsCommand.php +++ b/module/CLI/src/Command/Tag/ListTagsCommand.php @@ -1,15 +1,12 @@ tagService = $tagService; diff --git a/module/CLI/src/Command/Tag/RenameTagCommand.php b/module/CLI/src/Command/Tag/RenameTagCommand.php index e3ee678e..89f267d3 100644 --- a/module/CLI/src/Command/Tag/RenameTagCommand.php +++ b/module/CLI/src/Command/Tag/RenameTagCommand.php @@ -1,15 +1,12 @@ tagService = $tagService; diff --git a/module/CLI/src/Command/Visit/ProcessVisitsCommand.php b/module/CLI/src/Command/Visit/ProcessVisitsCommand.php index 195d891c..8b4f82a8 100644 --- a/module/CLI/src/Command/Visit/ProcessVisitsCommand.php +++ b/module/CLI/src/Command/Visit/ProcessVisitsCommand.php @@ -1,12 +1,9 @@ 'Logger_Shlink', LoggerInterface::class => 'Logger_Shlink', ], + 'abstract_factories' => [ + Factory\DottedAccessConfigAbstractFactory::class, + ], ], ]; diff --git a/module/Common/src/Factory/DottedAccessConfigAbstractFactory.php b/module/Common/src/Factory/DottedAccessConfigAbstractFactory.php new file mode 100644 index 00000000..7d0e03b6 --- /dev/null +++ b/module/Common/src/Factory/DottedAccessConfigAbstractFactory.php @@ -0,0 +1,79 @@ + 0; + } + + /** + * Create an object + * + * @param ContainerInterface $container + * @param string $requestedName + * @param null|array $options + * @return object + * @throws InvalidArgumentException + * @throws ServiceNotFoundException if unable to resolve the 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) + { + $parts = explode('.', $requestedName); + $serviceName = array_shift($parts); + if (! $container->has($serviceName)) { + throw new ServiceNotCreatedException(sprintf( + 'Defined service "%s" could not be found in container after resolving dotted expression "%s".', + $serviceName, + $requestedName + )); + } + + $array = $container->get($serviceName); + return $this->readKeysFromArray($parts, $array); + } + + /** + * @param array $keys + * @param array|\ArrayAccess $array + * @return mixed|null + * @throws InvalidArgumentException + */ + private function readKeysFromArray(array $keys, $array) + { + $key = array_shift($keys); + + // When one of the provided keys is not found, throw an exception + if (! isset($array[$key])) { + throw new InvalidArgumentException(sprintf( + 'The key "%s" provided in the dotted notation could not be found in the array service', + $key + )); + } + + $value = $array[$key]; + if (! empty($keys) && (is_array($value) || $value instanceof \ArrayAccess)) { + $value = $this->readKeysFromArray($keys, $value); + } + + return $value; + } +}