mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Replaced cli execution using expressive middleware by symfony/console
This commit is contained in:
parent
1c6250618a
commit
35147fecb2
17
bin/cli
17
bin/cli
@ -1,17 +1,14 @@
|
|||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
|
use Acelaya\UrlShortener\CliCommands\GenerateShortcodeCommand;
|
||||||
use Interop\Container\ContainerInterface;
|
use Interop\Container\ContainerInterface;
|
||||||
use Zend\Diactoros\ServerRequestFactory;
|
use Symfony\Component\Console\Application as CliApp;
|
||||||
use Zend\Diactoros\Uri;
|
|
||||||
use Zend\Expressive\Application;
|
|
||||||
|
|
||||||
/** @var ContainerInterface $container */
|
/** @var ContainerInterface $container */
|
||||||
$container = include __DIR__ . '/../config/container.php';
|
$container = include __DIR__ . '/../config/container.php';
|
||||||
/** @var Application $app */
|
|
||||||
$app = $container->get(Application::class);
|
|
||||||
|
|
||||||
$command = count($_SERVER['argv']) > 1 ? $_SERVER['argv'][1] : '';
|
$app = new CliApp();
|
||||||
$request = ServerRequestFactory::fromGlobals()
|
$app->addCommands([
|
||||||
->withMethod('CLI')
|
$container->get(GenerateShortcodeCommand::class),
|
||||||
->withUri(new Uri(sprintf('/%s', $command)));
|
]);
|
||||||
$app->run($request);
|
$app->run();
|
||||||
|
@ -20,7 +20,8 @@
|
|||||||
"zendframework/zend-servicemanager": "^3.0",
|
"zendframework/zend-servicemanager": "^3.0",
|
||||||
"doctrine/orm": "^2.5",
|
"doctrine/orm": "^2.5",
|
||||||
"guzzlehttp/guzzle": "^6.2",
|
"guzzlehttp/guzzle": "^6.2",
|
||||||
"acelaya/zsm-annotated-services": "^0.2.0"
|
"acelaya/zsm-annotated-services": "^0.2.0",
|
||||||
|
"symfony/console": "^3.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^4.8",
|
"phpunit/phpunit": "^4.8",
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
use Acelaya\UrlShortener\CliCommands;
|
||||||
use Acelaya\UrlShortener\Factory\CacheFactory;
|
use Acelaya\UrlShortener\Factory\CacheFactory;
|
||||||
use Acelaya\UrlShortener\Factory\EntityManagerFactory;
|
use Acelaya\UrlShortener\Factory\EntityManagerFactory;
|
||||||
use Acelaya\UrlShortener\Middleware;
|
use Acelaya\UrlShortener\Middleware;
|
||||||
@ -38,6 +39,9 @@ return [
|
|||||||
Service\VisitsTracker::class => AnnotatedFactory::class,
|
Service\VisitsTracker::class => AnnotatedFactory::class,
|
||||||
Cache::class => CacheFactory::class,
|
Cache::class => CacheFactory::class,
|
||||||
|
|
||||||
|
// Cli commands
|
||||||
|
CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class,
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
Middleware\CliRoutable\GenerateShortcodeMiddleware::class => AnnotatedFactory::class,
|
Middleware\CliRoutable\GenerateShortcodeMiddleware::class => AnnotatedFactory::class,
|
||||||
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,
|
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,
|
||||||
|
95
src/CliCommands/GenerateShortcodeCommand.php
Normal file
95
src/CliCommands/GenerateShortcodeCommand.php
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
<?php
|
||||||
|
namespace Acelaya\UrlShortener\CliCommands;
|
||||||
|
|
||||||
|
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('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(
|
||||||
|
'<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)
|
||||||
|
);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
$output->writeln('<error>' . $e . '</error>');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user