diff --git a/bin/cli b/bin/cli
index 0aca7dd0..1816b80f 100755
--- a/bin/cli
+++ b/bin/cli
@@ -1,17 +1,14 @@
#!/usr/bin/env php
get(Application::class);
-$command = count($_SERVER['argv']) > 1 ? $_SERVER['argv'][1] : '';
-$request = ServerRequestFactory::fromGlobals()
- ->withMethod('CLI')
- ->withUri(new Uri(sprintf('/%s', $command)));
-$app->run($request);
+$app = new CliApp();
+$app->addCommands([
+ $container->get(GenerateShortcodeCommand::class),
+]);
+$app->run();
diff --git a/composer.json b/composer.json
index d43d7fbb..922e9067 100644
--- a/composer.json
+++ b/composer.json
@@ -20,7 +20,8 @@
"zendframework/zend-servicemanager": "^3.0",
"doctrine/orm": "^2.5",
"guzzlehttp/guzzle": "^6.2",
- "acelaya/zsm-annotated-services": "^0.2.0"
+ "acelaya/zsm-annotated-services": "^0.2.0",
+ "symfony/console": "^3.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php
index 300f1bf3..6027973d 100644
--- a/config/autoload/services.global.php
+++ b/config/autoload/services.global.php
@@ -1,4 +1,5 @@
AnnotatedFactory::class,
Cache::class => CacheFactory::class,
+ // Cli commands
+ CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class,
+
// Middleware
Middleware\CliRoutable\GenerateShortcodeMiddleware::class => AnnotatedFactory::class,
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,
diff --git a/src/CliCommands/GenerateShortcodeCommand.php b/src/CliCommands/GenerateShortcodeCommand.php
new file mode 100644
index 00000000..9f545dce
--- /dev/null
+++ b/src/CliCommands/GenerateShortcodeCommand.php
@@ -0,0 +1,95 @@
+urlShortener = $urlShortener;
+ $this->domainConfig = $domainConfig;
+ }
+
+ public function configure()
+ {
+ $this->setName('generate-shortcode')
+ ->setDescription('Generates a shortcode for provided URL and returns the short URL')
+ ->addArgument('longUrl', InputArgument::REQUIRED, 'The long URL to parse');
+ }
+
+ public function interact(InputInterface $input, OutputInterface $output)
+ {
+ $longUrl = $input->getArgument('longUrl');
+ if (! empty($longUrl)) {
+ return;
+ }
+
+ /** @var QuestionHelper $helper */
+ $helper = $this->getHelper('question');
+ $question = new Question(
+ 'A long URL was not provided. Which URL do you want to shorten?: '
+ );
+
+ $longUrl = $helper->ask($input, $output, $question);
+ if (! empty($longUrl)) {
+ $input->setArgument('longUrl', $longUrl);
+ }
+ }
+
+ public function execute(InputInterface $input, OutputInterface $output)
+ {
+ $longUrl = $input->getArgument('longUrl');
+
+ try {
+ if (! isset($longUrl)) {
+ $output->writeln('A URL was not provided!');
+ return;
+ }
+
+ $shortcode = $this->urlShortener->urlToShortCode(new Uri($longUrl));
+ $shortUrl = (new Uri())->withPath($shortcode)
+ ->withScheme($this->domainConfig['schema'])
+ ->withHost($this->domainConfig['hostname']);
+
+ $output->writeln([
+ sprintf('Processed URL %s', $longUrl),
+ sprintf('Generated URL %s', $shortUrl),
+ ]);
+ } catch (InvalidUrlException $e) {
+ $output->writeln(
+ sprintf('Provided URL "%s" is invalid. Try with a different one.', $longUrl)
+ );
+ } catch (\Exception $e) {
+ $output->writeln('' . $e . '');
+ }
+ }
+}