mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Created CLI module
This commit is contained in:
15
module/CLI/config/cli.config.php
Normal file
15
module/CLI/config/cli.config.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
use Shlinkio\Shlink\CLI\Command;
|
||||
|
||||
return [
|
||||
|
||||
'cli' => [
|
||||
'commands' => [
|
||||
Command\GenerateShortcodeCommand::class,
|
||||
Command\ResolveUrlCommand::class,
|
||||
Command\ListShortcodesCommand::class,
|
||||
Command\GetVisitsCommand::class,
|
||||
]
|
||||
],
|
||||
|
||||
];
|
||||
93
module/CLI/src/Command/GenerateShortcodeCommand.php
Normal file
93
module/CLI/src/Command/GenerateShortcodeCommand.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\CLI\Command;
|
||||
|
||||
use Acelaya\UrlShortener\Exception\InvalidUrlException;
|
||||
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\ChoiceQuestion;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
use Zend\Diactoros\Uri;
|
||||
|
||||
class GenerateShortcodeCommand extends Command
|
||||
{
|
||||
/**
|
||||
* @var UrlShortenerInterface
|
||||
*/
|
||||
private $urlShortener;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $domainConfig;
|
||||
|
||||
/**
|
||||
* GenerateShortcodeCommand constructor.
|
||||
* @param UrlShortenerInterface|UrlShortener $urlShortener
|
||||
* @param array $domainConfig
|
||||
*
|
||||
* @Inject({UrlShortener::class, "config.url_shortener.domain"})
|
||||
*/
|
||||
public function __construct(UrlShortenerInterface $urlShortener, array $domainConfig)
|
||||
{
|
||||
parent::__construct(null);
|
||||
$this->urlShortener = $urlShortener;
|
||||
$this->domainConfig = $domainConfig;
|
||||
}
|
||||
|
||||
public function configure()
|
||||
{
|
||||
$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');
|
||||
}
|
||||
|
||||
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(
|
||||
'<question>A long URL was not provided. Which URL do you want to shorten?:</question> '
|
||||
);
|
||||
|
||||
$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('<error>A URL was not provided!</error>');
|
||||
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 <info>%s</info>', $longUrl),
|
||||
sprintf('Generated URL <info>%s</info>', $shortUrl),
|
||||
]);
|
||||
} catch (InvalidUrlException $e) {
|
||||
$output->writeln(
|
||||
sprintf('<error>Provided URL "%s" is invalid. Try with a different one.</error>', $longUrl)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
77
module/CLI/src/Command/GetVisitsCommand.php
Normal file
77
module/CLI/src/Command/GetVisitsCommand.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\CLI\Command;
|
||||
|
||||
use Acelaya\UrlShortener\Service\VisitsTracker;
|
||||
use Acelaya\UrlShortener\Service\VisitsTrackerInterface;
|
||||
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\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
|
||||
class GetVisitsCommand extends Command
|
||||
{
|
||||
/**
|
||||
* @var VisitsTrackerInterface
|
||||
*/
|
||||
private $visitsTracker;
|
||||
|
||||
/**
|
||||
* GetVisitsCommand constructor.
|
||||
* @param VisitsTrackerInterface|VisitsTracker $visitsTracker
|
||||
*
|
||||
* @Inject({VisitsTracker::class})
|
||||
*/
|
||||
public function __construct(VisitsTrackerInterface $visitsTracker)
|
||||
{
|
||||
parent::__construct(null);
|
||||
$this->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(
|
||||
'<question>A short code was not provided. Which short code do you want to use?:</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');
|
||||
$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();
|
||||
}
|
||||
}
|
||||
84
module/CLI/src/Command/ListShortcodesCommand.php
Normal file
84
module/CLI/src/Command/ListShortcodesCommand.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\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\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
|
||||
*/
|
||||
private $shortUrlService;
|
||||
|
||||
/**
|
||||
* ListShortcodesCommand constructor.
|
||||
* @param ShortUrlServiceInterface|ShortUrlService $shortUrlService
|
||||
*
|
||||
* @Inject({ShortUrlService::class})
|
||||
*/
|
||||
public function __construct(ShortUrlServiceInterface $shortUrlService)
|
||||
{
|
||||
parent::__construct(null);
|
||||
$this->shortUrlService = $shortUrlService;
|
||||
}
|
||||
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('shortcode:list')
|
||||
->setDescription('List all short URLs')
|
||||
->addOption(
|
||||
'page',
|
||||
'p',
|
||||
InputOption::VALUE_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->getOption('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();
|
||||
|
||||
if ($this->isLastPage($result)) {
|
||||
$continue = false;
|
||||
$output->writeln('<info>You have reached last page</info>');
|
||||
} else {
|
||||
$continue = $helper->ask($input, $output, new ConfirmationQuestion(
|
||||
sprintf('<question>Continue with page <bg=cyan;options=bold>%s</>? (y/N)</question> ', $page),
|
||||
false
|
||||
));
|
||||
}
|
||||
} while ($continue);
|
||||
}
|
||||
}
|
||||
78
module/CLI/src/Command/ResolveUrlCommand.php
Normal file
78
module/CLI/src/Command/ResolveUrlCommand.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\CLI\Command;
|
||||
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
13
module/CLI/src/Config/ConfigProvider.php
Normal file
13
module/CLI/src/Config/ConfigProvider.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\CLI\Config;
|
||||
|
||||
use Zend\Config\Factory;
|
||||
use Zend\Stdlib\Glob;
|
||||
|
||||
class ConfigProvider
|
||||
{
|
||||
public function __invoke()
|
||||
{
|
||||
return Factory::fromFiles(Glob::glob(__DIR__ . '/../../config/{,*.}config.php', Glob::GLOB_BRACE));
|
||||
}
|
||||
}
|
||||
41
module/CLI/src/Factory/ApplicationFactory.php
Normal file
41
module/CLI/src/Factory/ApplicationFactory.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\CLI\Factory;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Interop\Container\Exception\ContainerException;
|
||||
use Symfony\Component\Console\Application as CliApp;
|
||||
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
||||
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||
|
||||
class ApplicationFactory implements FactoryInterface
|
||||
{
|
||||
/**
|
||||
* Create an object
|
||||
*
|
||||
* @param ContainerInterface $container
|
||||
* @param string $requestedName
|
||||
* @param null|array $options
|
||||
* @return object
|
||||
* @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)
|
||||
{
|
||||
$config = $container->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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user