From 490b72539eabbadafaa7f88c85b64e57bb060974 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Jul 2016 20:09:10 +0200 Subject: [PATCH 1/7] Created CHANGELOG --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..b6f92d162 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,12 @@ +## CHANGELOG + +### 0.2.0 + +**Enhancements:** + +* [9: Use symfony/console to dispatch console requests, instead of trying to integrate the process with expressive](https://github.com/acelaya/url-shortener/issues/9) +* [8: Create a REST API](https://github.com/acelaya/url-shortener/issues/8) + +**Tasks** + +* [5: Create CHANGELOG file](https://github.com/acelaya/url-shortener/issues/5) From 9ce5e255f1122250e07506936c6a22fb599ed13f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Jul 2016 23:16:23 +0200 Subject: [PATCH 2/7] Created new CLI command to parse a shortcode --- bin/cli | 2 + config/autoload/services.global.php | 1 + src/CliCommands/GenerateShortcodeCommand.php | 2 +- src/CliCommands/ResolveUrlCommand.php | 80 ++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/CliCommands/ResolveUrlCommand.php diff --git a/bin/cli b/bin/cli index 1816b80fa..8a2983c11 100755 --- a/bin/cli +++ b/bin/cli @@ -1,6 +1,7 @@ #!/usr/bin/env php addCommands([ $container->get(GenerateShortcodeCommand::class), + $container->get(ResolveUrlCommand::class), ]); $app->run(); diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index b08229e75..b3103ac37 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -43,6 +43,7 @@ // Cli commands CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class, + CliCommands\ResolveUrlCommand::class => AnnotatedFactory::class, // Middleware Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class, diff --git a/src/CliCommands/GenerateShortcodeCommand.php b/src/CliCommands/GenerateShortcodeCommand.php index 9f545dcee..68b076c35 100644 --- a/src/CliCommands/GenerateShortcodeCommand.php +++ b/src/CliCommands/GenerateShortcodeCommand.php @@ -41,7 +41,7 @@ public function __construct(UrlShortenerInterface $urlShortener, array $domainCo public function configure() { - $this->setName('generate-shortcode') + $this->setName('shortcode:generate') ->setDescription('Generates a shortcode for provided URL and returns the short URL') ->addArgument('longUrl', InputArgument::REQUIRED, 'The long URL to parse'); } diff --git a/src/CliCommands/ResolveUrlCommand.php b/src/CliCommands/ResolveUrlCommand.php new file mode 100644 index 000000000..e10cd73e3 --- /dev/null +++ b/src/CliCommands/ResolveUrlCommand.php @@ -0,0 +1,80 @@ +urlShortener = $urlShortener; + } + + public function configure() + { + $this->setName('shortcode:parse') + ->setDescription('Returns the long URL behind a short code') + ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code to parse'); + } + + public function interact(InputInterface $input, OutputInterface $output) + { + $shortCode = $input->getArgument('shortCode'); + if (! empty($shortCode)) { + return; + } + + /** @var QuestionHelper $helper */ + $helper = $this->getHelper('question'); + $question = new Question( + 'A short code was not provided. Which short code do you want to parse?: ' + ); + + $shortCode = $helper->ask($input, $output, $question); + if (! empty($shortCode)) { + $input->setArgument('shortCode', $shortCode); + } + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $shortCode = $input->getArgument('shortCode'); + + try { + $longUrl = $this->urlShortener->shortCodeToUrl($shortCode); + if (! isset($longUrl)) { + $output->writeln(sprintf('No URL found for short code "%s"', $shortCode)); + return; + } + + $output->writeln(sprintf('Long URL %s', $longUrl)); + } catch (InvalidShortCodeException $e) { + $output->writeln( + sprintf('Provided short code "%s" has an invalid format.', $shortCode) + ); + } catch (\Exception $e) { + $output->writeln('' . $e . ''); + } + } +} From 96478f34006bd43031690f375bbd4133238fd9f3 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 5 Jul 2016 23:25:39 +0200 Subject: [PATCH 3/7] Created the console application via a factory --- bin/cli | 8 +--- config/autoload/cli.global.php | 13 ++++++ config/autoload/services.global.php | 12 +++--- .../Command}/GenerateShortcodeCommand.php | 2 +- .../Command}/ResolveUrlCommand.php | 2 +- src/CLI/Factory/ApplicationFactory.php | 41 +++++++++++++++++++ 6 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 config/autoload/cli.global.php rename src/{CliCommands => CLI/Command}/GenerateShortcodeCommand.php (98%) rename src/{CliCommands => CLI/Command}/ResolveUrlCommand.php (98%) create mode 100644 src/CLI/Factory/ApplicationFactory.php diff --git a/bin/cli b/bin/cli index 8a2983c11..e400bef8c 100755 --- a/bin/cli +++ b/bin/cli @@ -1,16 +1,10 @@ #!/usr/bin/env php addCommands([ - $container->get(GenerateShortcodeCommand::class), - $container->get(ResolveUrlCommand::class), -]); +$app = $container->get(CliApp::class); $app->run(); diff --git a/config/autoload/cli.global.php b/config/autoload/cli.global.php new file mode 100644 index 000000000..f1a36b7d0 --- /dev/null +++ b/config/autoload/cli.global.php @@ -0,0 +1,13 @@ + [ + 'commands' => [ + Command\GenerateShortcodeCommand::class, + Command\ResolveUrlCommand::class, + ] + ], + +]; diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index b3103ac37..9f4447f42 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -1,5 +1,5 @@ [ 'factories' => [ - Application::class => Container\ApplicationFactory::class, + Expressive\Application::class => Container\ApplicationFactory::class, + Console\Application::class => CLI\Factory\ApplicationFactory::class, // Url helpers Helper\UrlHelper::class => Helper\UrlHelperFactory::class, @@ -42,8 +44,8 @@ Cache::class => CacheFactory::class, // Cli commands - CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class, - CliCommands\ResolveUrlCommand::class => AnnotatedFactory::class, + CLI\Command\GenerateShortcodeCommand::class => AnnotatedFactory::class, + CLI\Command\ResolveUrlCommand::class => AnnotatedFactory::class, // Middleware Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class, diff --git a/src/CliCommands/GenerateShortcodeCommand.php b/src/CLI/Command/GenerateShortcodeCommand.php similarity index 98% rename from src/CliCommands/GenerateShortcodeCommand.php rename to src/CLI/Command/GenerateShortcodeCommand.php index 68b076c35..ea6fc929d 100644 --- a/src/CliCommands/GenerateShortcodeCommand.php +++ b/src/CLI/Command/GenerateShortcodeCommand.php @@ -1,5 +1,5 @@ get('config')['cli']; + $app = new CliApp(); + + $commands = isset($config['commands']) ? $config['commands'] : []; + foreach ($commands as $command) { + if (! $container->has($command)) { + continue; + } + + $app->add($container->get($command)); + } + + return $app; + } +} From 60f5e5290eb789376bc3f8b90c9c23be9f9460ae Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 6 Jul 2016 19:41:24 +0200 Subject: [PATCH 4/7] Created new command to list short urls --- config/autoload/cli.global.php | 1 + config/autoload/services.global.php | 1 + src/CLI/Command/ListShortcodesCommand.php | 72 +++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 src/CLI/Command/ListShortcodesCommand.php diff --git a/config/autoload/cli.global.php b/config/autoload/cli.global.php index f1a36b7d0..1d5ea78f1 100644 --- a/config/autoload/cli.global.php +++ b/config/autoload/cli.global.php @@ -7,6 +7,7 @@ 'commands' => [ Command\GenerateShortcodeCommand::class, Command\ResolveUrlCommand::class, + Command\ListShortcodesCommand::class, ] ], diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index 9f4447f42..1133642dc 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -46,6 +46,7 @@ // Cli commands CLI\Command\GenerateShortcodeCommand::class => AnnotatedFactory::class, CLI\Command\ResolveUrlCommand::class => AnnotatedFactory::class, + CLI\Command\ListShortcodesCommand::class => AnnotatedFactory::class, // Middleware Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class, diff --git a/src/CLI/Command/ListShortcodesCommand.php b/src/CLI/Command/ListShortcodesCommand.php new file mode 100644 index 000000000..7df61588d --- /dev/null +++ b/src/CLI/Command/ListShortcodesCommand.php @@ -0,0 +1,72 @@ +shortUrlService = $shortUrlService; + } + + public function configure() + { + $this->setName('shortcode:list') + ->setDescription('List all short URLs') + ->addArgument( + 'page', + InputArgument::OPTIONAL, + sprintf('The first page to list (%s items per page)', PaginableRepositoryAdapter::ITEMS_PER_PAGE), + 1 + ); + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $page = intval($input->getArgument('page')); + /** @var QuestionHelper $helper */ + $helper = $this->getHelper('question'); + + do { + $result = $this->shortUrlService->listShortUrls($page); + $page++; + $table = new Table($output); + $table->setHeaders([ + 'Short code', + 'Original URL', + 'Date created', + 'Visits count', + ]); + + foreach ($result as $row) { + $table->addRow(array_values($row->jsonSerialize())); + } + $table->render(); + + $question = new ConfirmationQuestion('Continue with next page? (y/N) ', false); + } while ($helper->ask($input, $output, $question)); + } +} From 43f1f790ddb92308cd403d860022ba4b3154303a Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 6 Jul 2016 20:10:19 +0200 Subject: [PATCH 5/7] Improved ListShortciodesCommand --- src/CLI/Command/ListShortcodesCommand.php | 24 ++++++++++++++----- .../Rest/ListShortcodesMiddleware.php | 4 ++-- ...lizerTrait.php => PaginatorUtilsTrait.php} | 13 +++++++++- 3 files changed, 32 insertions(+), 9 deletions(-) rename src/Paginator/Util/{PaginatorSerializerTrait.php => PaginatorUtilsTrait.php} (61%) diff --git a/src/CLI/Command/ListShortcodesCommand.php b/src/CLI/Command/ListShortcodesCommand.php index 7df61588d..ac85d50f0 100644 --- a/src/CLI/Command/ListShortcodesCommand.php +++ b/src/CLI/Command/ListShortcodesCommand.php @@ -2,19 +2,22 @@ namespace Acelaya\UrlShortener\CLI\Command; use Acelaya\UrlShortener\Paginator\Adapter\PaginableRepositoryAdapter; +use Acelaya\UrlShortener\Paginator\Util\PaginatorUtilsTrait; use Acelaya\UrlShortener\Service\ShortUrlService; use Acelaya\UrlShortener\Service\ShortUrlServiceInterface; use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Helper\Table; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; class ListShortcodesCommand extends Command { + use PaginatorUtilsTrait; + /** * @var ShortUrlServiceInterface */ @@ -36,9 +39,10 @@ public function configure() { $this->setName('shortcode:list') ->setDescription('List all short URLs') - ->addArgument( + ->addOption( 'page', - InputArgument::OPTIONAL, + 'p', + InputOption::VALUE_OPTIONAL, sprintf('The first page to list (%s items per page)', PaginableRepositoryAdapter::ITEMS_PER_PAGE), 1 ); @@ -46,7 +50,7 @@ public function configure() public function execute(InputInterface $input, OutputInterface $output) { - $page = intval($input->getArgument('page')); + $page = intval($input->getOption('page')); /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); @@ -66,7 +70,15 @@ public function execute(InputInterface $input, OutputInterface $output) } $table->render(); - $question = new ConfirmationQuestion('Continue with next page? (y/N) ', false); - } while ($helper->ask($input, $output, $question)); + if ($this->isLastPage($result)) { + $continue = false; + $output->writeln('You have reached last page'); + } else { + $continue = $helper->ask($input, $output, new ConfirmationQuestion( + sprintf('Continue with page %s? (y/N) ', $page), + false + )); + } + } while ($continue); } } diff --git a/src/Middleware/Rest/ListShortcodesMiddleware.php b/src/Middleware/Rest/ListShortcodesMiddleware.php index 6b74241ce..99d82454b 100644 --- a/src/Middleware/Rest/ListShortcodesMiddleware.php +++ b/src/Middleware/Rest/ListShortcodesMiddleware.php @@ -1,7 +1,7 @@ getCurrentPageNumber() >= $paginator->count(); + } } From 2e00a8dec69c2979873fd811db633f434f3687a5 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 6 Jul 2016 20:21:34 +0200 Subject: [PATCH 6/7] Created command to list visits for a shortcode --- config/autoload/cli.global.php | 1 + config/autoload/services.global.php | 1 + src/CLI/Command/GenerateShortcodeCommand.php | 2 - src/CLI/Command/GetVisitsCommand.php | 77 ++++++++++++++++++++ src/CLI/Command/ResolveUrlCommand.php | 2 - 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 src/CLI/Command/GetVisitsCommand.php diff --git a/config/autoload/cli.global.php b/config/autoload/cli.global.php index 1d5ea78f1..1276cc9e4 100644 --- a/config/autoload/cli.global.php +++ b/config/autoload/cli.global.php @@ -8,6 +8,7 @@ Command\GenerateShortcodeCommand::class, Command\ResolveUrlCommand::class, Command\ListShortcodesCommand::class, + Command\GetVisitsCommand::class, ] ], diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index 1133642dc..4d6cc40e3 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -47,6 +47,7 @@ CLI\Command\GenerateShortcodeCommand::class => AnnotatedFactory::class, CLI\Command\ResolveUrlCommand::class => AnnotatedFactory::class, CLI\Command\ListShortcodesCommand::class => AnnotatedFactory::class, + CLI\Command\GetVisitsCommand::class => AnnotatedFactory::class, // Middleware Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class, diff --git a/src/CLI/Command/GenerateShortcodeCommand.php b/src/CLI/Command/GenerateShortcodeCommand.php index ea6fc929d..ffc355db2 100644 --- a/src/CLI/Command/GenerateShortcodeCommand.php +++ b/src/CLI/Command/GenerateShortcodeCommand.php @@ -88,8 +88,6 @@ public function execute(InputInterface $input, OutputInterface $output) $output->writeln( sprintf('Provided URL "%s" is invalid. Try with a different one.', $longUrl) ); - } catch (\Exception $e) { - $output->writeln('' . $e . ''); } } } diff --git a/src/CLI/Command/GetVisitsCommand.php b/src/CLI/Command/GetVisitsCommand.php new file mode 100644 index 000000000..3c4e796da --- /dev/null +++ b/src/CLI/Command/GetVisitsCommand.php @@ -0,0 +1,77 @@ +visitsTracker = $visitsTracker; + } + + public function configure() + { + $this->setName('shortcode:visits') + ->setDescription('Returns the detailed visits information for provided short code') + ->addArgument('shortCode', InputArgument::REQUIRED, 'The short code which visits we want to get'); + } + + public function interact(InputInterface $input, OutputInterface $output) + { + $shortCode = $input->getArgument('shortCode'); + if (! empty($shortCode)) { + return; + } + + /** @var QuestionHelper $helper */ + $helper = $this->getHelper('question'); + $question = new Question( + 'A short code was not provided. Which short code do you want to use?: ' + ); + + $shortCode = $helper->ask($input, $output, $question); + if (! empty($shortCode)) { + $input->setArgument('shortCode', $shortCode); + } + } + + public function execute(InputInterface $input, OutputInterface $output) + { + $shortCode = $input->getArgument('shortCode'); + $visits = $this->visitsTracker->info($shortCode); + $table = new Table($output); + $table->setHeaders([ + 'Referer', + 'Date', + 'Temote Address', + 'User agent', + ]); + + foreach ($visits as $row) { + $table->addRow(array_values($row->jsonSerialize())); + } + $table->render(); + } +} diff --git a/src/CLI/Command/ResolveUrlCommand.php b/src/CLI/Command/ResolveUrlCommand.php index 73b330d90..4eb5ff412 100644 --- a/src/CLI/Command/ResolveUrlCommand.php +++ b/src/CLI/Command/ResolveUrlCommand.php @@ -73,8 +73,6 @@ public function execute(InputInterface $input, OutputInterface $output) $output->writeln( sprintf('Provided short code "%s" has an invalid format.', $shortCode) ); - } catch (\Exception $e) { - $output->writeln('' . $e . ''); } } } From cdeffe9cc785047c18d1916c9fa9de00ce943049 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 6 Jul 2016 20:23:38 +0200 Subject: [PATCH 7/7] Added missing issue to changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6f92d162..c08baeb75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * [9: Use symfony/console to dispatch console requests, instead of trying to integrate the process with expressive](https://github.com/acelaya/url-shortener/issues/9) * [8: Create a REST API](https://github.com/acelaya/url-shortener/issues/8) +* [10: Add more CLI functionality](https://github.com/acelaya/url-shortener/issues/10) **Tasks**