mirror of
https://github.com/shlinkio/shlink.git
synced 2025-01-15 19:22:14 -06:00
Created new CLI command to parse a shortcode
This commit is contained in:
parent
490b72539e
commit
9ce5e255f1
2
bin/cli
2
bin/cli
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
use Acelaya\UrlShortener\CliCommands\GenerateShortcodeCommand;
|
use Acelaya\UrlShortener\CliCommands\GenerateShortcodeCommand;
|
||||||
|
use Acelaya\UrlShortener\CliCommands\ResolveUrlCommand;
|
||||||
use Interop\Container\ContainerInterface;
|
use Interop\Container\ContainerInterface;
|
||||||
use Symfony\Component\Console\Application as CliApp;
|
use Symfony\Component\Console\Application as CliApp;
|
||||||
|
|
||||||
@ -10,5 +11,6 @@ $container = include __DIR__ . '/../config/container.php';
|
|||||||
$app = new CliApp();
|
$app = new CliApp();
|
||||||
$app->addCommands([
|
$app->addCommands([
|
||||||
$container->get(GenerateShortcodeCommand::class),
|
$container->get(GenerateShortcodeCommand::class),
|
||||||
|
$container->get(ResolveUrlCommand::class),
|
||||||
]);
|
]);
|
||||||
$app->run();
|
$app->run();
|
||||||
|
@ -43,6 +43,7 @@ return [
|
|||||||
|
|
||||||
// Cli commands
|
// Cli commands
|
||||||
CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class,
|
CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class,
|
||||||
|
CliCommands\ResolveUrlCommand::class => AnnotatedFactory::class,
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,
|
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,
|
||||||
|
@ -41,7 +41,7 @@ class GenerateShortcodeCommand extends Command
|
|||||||
|
|
||||||
public function configure()
|
public function configure()
|
||||||
{
|
{
|
||||||
$this->setName('generate-shortcode')
|
$this->setName('shortcode:generate')
|
||||||
->setDescription('Generates a shortcode for provided URL and returns the short URL')
|
->setDescription('Generates a shortcode for provided URL and returns the short URL')
|
||||||
->addArgument('longUrl', InputArgument::REQUIRED, 'The long URL to parse');
|
->addArgument('longUrl', InputArgument::REQUIRED, 'The long URL to parse');
|
||||||
}
|
}
|
||||||
|
80
src/CliCommands/ResolveUrlCommand.php
Normal file
80
src/CliCommands/ResolveUrlCommand.php
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
namespace Acelaya\UrlShortener\CliCommands;
|
||||||
|
|
||||||
|
use Acelaya\UrlShortener\Exception\InvalidShortCodeException;
|
||||||
|
use Acelaya\UrlShortener\Service\UrlShortener;
|
||||||
|
use Acelaya\UrlShortener\Service\UrlShortenerInterface;
|
||||||
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||||
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Symfony\Component\Console\Question\Question;
|
||||||
|
|
||||||
|
class ResolveUrlCommand extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var UrlShortenerInterface
|
||||||
|
*/
|
||||||
|
private $urlShortener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ResolveUrlCommand constructor.
|
||||||
|
* @param UrlShortenerInterface|UrlShortener $urlShortener
|
||||||
|
*
|
||||||
|
* @Inject({UrlShortener::class})
|
||||||
|
*/
|
||||||
|
public function __construct(UrlShortenerInterface $urlShortener)
|
||||||
|
{
|
||||||
|
parent::__construct(null);
|
||||||
|
$this->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(
|
||||||
|
'<question>A short code was not provided. Which short code do you want to parse?:</question> '
|
||||||
|
);
|
||||||
|
|
||||||
|
$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('<error>No URL found for short code "%s"</error>', $shortCode));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$output->writeln(sprintf('Long URL <info>%s</info>', $longUrl));
|
||||||
|
} catch (InvalidShortCodeException $e) {
|
||||||
|
$output->writeln(
|
||||||
|
sprintf('<error>Provided short code "%s" has an invalid format.</error>', $shortCode)
|
||||||
|
);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$output->writeln('<error>' . $e . '</error>');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user