mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-16 23:39:54 +03:00
Merge branch 'feature/9' into develop
This commit is contained in:
commit
32d0e2d091
10 changed files with 108 additions and 273 deletions
17
bin/cli
17
bin/cli
|
@ -1,17 +1,14 @@
|
|||
#!/usr/bin/env php
|
||||
<?php
|
||||
use Acelaya\UrlShortener\CliCommands\GenerateShortcodeCommand;
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Zend\Diactoros\ServerRequestFactory;
|
||||
use Zend\Diactoros\Uri;
|
||||
use Zend\Expressive\Application;
|
||||
use Symfony\Component\Console\Application as CliApp;
|
||||
|
||||
/** @var ContainerInterface $container */
|
||||
$container = include __DIR__ . '/../config/container.php';
|
||||
/** @var Application $app */
|
||||
$app = $container->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();
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
<?php
|
||||
use Acelaya\UrlShortener\Middleware\CliRoutable;
|
||||
|
||||
return [
|
||||
|
||||
'routes' => [
|
||||
[
|
||||
'name' => 'cli-generate-shortcode',
|
||||
'path' => '/generate-shortcode',
|
||||
'middleware' => CliRoutable\GenerateShortcodeMiddleware::class,
|
||||
'allowed_methods' => ['CLI'],
|
||||
],
|
||||
],
|
||||
|
||||
];
|
|
@ -1,5 +1,4 @@
|
|||
<?php
|
||||
use Acelaya\UrlShortener\Middleware\CliParamsMiddleware;
|
||||
use Zend\Expressive\Container\ApplicationFactory;
|
||||
use Zend\Expressive\Helper;
|
||||
|
||||
|
@ -16,7 +15,6 @@ return [
|
|||
'routing' => [
|
||||
'middleware' => [
|
||||
ApplicationFactory::ROUTING_MIDDLEWARE,
|
||||
CliParamsMiddleware::class,
|
||||
Helper\UrlHelperMiddleware::class,
|
||||
ApplicationFactory::DISPATCH_MIDDLEWARE,
|
||||
],
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
use Acelaya\UrlShortener\CliCommands;
|
||||
use Acelaya\UrlShortener\Factory\CacheFactory;
|
||||
use Acelaya\UrlShortener\Factory\EntityManagerFactory;
|
||||
use Acelaya\UrlShortener\Middleware;
|
||||
|
@ -38,10 +39,11 @@ return [
|
|||
Service\VisitsTracker::class => 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,
|
||||
Middleware\CliParamsMiddleware::class => Middleware\Factory\CliParamsMiddlewareFactory::class,
|
||||
],
|
||||
'aliases' => [
|
||||
'em' => EntityManager::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>');
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
<?php
|
||||
namespace Acelaya\UrlShortener\Middleware\CliRoutable;
|
||||
|
||||
use Acelaya\UrlShortener\Exception\InvalidUrlException;
|
||||
use Acelaya\UrlShortener\Service\UrlShortener;
|
||||
use Acelaya\UrlShortener\Service\UrlShortenerInterface;
|
||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Zend\Diactoros\Uri;
|
||||
use Zend\Stratigility\MiddlewareInterface;
|
||||
|
||||
class GenerateShortcodeMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @var UrlShortenerInterface
|
||||
*/
|
||||
private $urlShortener;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $domainConfig;
|
||||
|
||||
/**
|
||||
* GenerateShortcodeMiddleware constructor.
|
||||
*
|
||||
* @param UrlShortenerInterface|UrlShortener $urlShortener
|
||||
* @param array $domainConfig
|
||||
*
|
||||
* @Inject({UrlShortener::class, "config.url_shortener.domain"})
|
||||
*/
|
||||
public function __construct(UrlShortenerInterface $urlShortener, array $domainConfig)
|
||||
{
|
||||
$this->urlShortener = $urlShortener;
|
||||
$this->domainConfig = $domainConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process an incoming request and/or response.
|
||||
*
|
||||
* Accepts a server-side request and a response instance, and does
|
||||
* something with them.
|
||||
*
|
||||
* If the response is not complete and/or further processing would not
|
||||
* interfere with the work done in the middleware, or if the middleware
|
||||
* wants to delegate to another process, it can use the `$out` callable
|
||||
* if present.
|
||||
*
|
||||
* If the middleware does not return a value, execution of the current
|
||||
* request is considered complete, and the response instance provided will
|
||||
* be considered the response to return.
|
||||
*
|
||||
* Alternately, the middleware may return a response instance.
|
||||
*
|
||||
* Often, middleware will `return $out();`, with the assumption that a
|
||||
* later middleware will return a response.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param null|callable $out
|
||||
* @return null|Response
|
||||
*/
|
||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
||||
{
|
||||
$longUrl = $request->getAttribute('longUrl');
|
||||
|
||||
try {
|
||||
if (! isset($longUrl)) {
|
||||
$response->getBody()->write('A URL was not provided!' . PHP_EOL);
|
||||
return;
|
||||
}
|
||||
|
||||
$shortcode = $this->urlShortener->urlToShortCode(new Uri($longUrl));
|
||||
$shortUrl = (new Uri())->withPath($shortcode)
|
||||
->withScheme($this->domainConfig['schema'])
|
||||
->withHost($this->domainConfig['hostname']);
|
||||
|
||||
$response->getBody()->write(
|
||||
sprintf('Processed URL "%s".%sGenerated short URL "%s"', $longUrl, PHP_EOL, $shortUrl) . PHP_EOL
|
||||
);
|
||||
} catch (InvalidUrlException $e) {
|
||||
$response->getBody()->write(
|
||||
sprintf('Provided URL "%s" is invalid. Try with a different one.', $longUrl) . PHP_EOL
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$response->getBody()->write($e);
|
||||
} finally {
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
<?php
|
||||
namespace Acelaya\UrlShortener\Middleware\Factory;
|
||||
|
||||
use Acelaya\UrlShortener\Middleware\CliParamsMiddleware;
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Interop\Container\Exception\ContainerException;
|
||||
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
||||
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||
|
||||
class CliParamsMiddlewareFactory 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)
|
||||
{
|
||||
return new CliParamsMiddleware(
|
||||
isset($_SERVER['argv']) ? $_SERVER['argv'] : [],
|
||||
php_sapi_name()
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
<?php
|
||||
namespace AcelayaTest\UrlShortener\Middleware;
|
||||
|
||||
use Acelaya\UrlShortener\Middleware\CliParamsMiddleware;
|
||||
use PHPUnit_Framework_TestCase as TestCase;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response;
|
||||
use Zend\Diactoros\ServerRequestFactory;
|
||||
use Zend\Expressive\Router\RouteResult;
|
||||
|
||||
class CliParamsMiddlewareTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function nonCliRequestsJustInvokeNextMiddleware()
|
||||
{
|
||||
$middleware = new CliParamsMiddleware([], 'non-cli');
|
||||
|
||||
$invoked = false;
|
||||
$originalResponse = new Response();
|
||||
|
||||
$response = $middleware->__invoke(
|
||||
ServerRequestFactory::fromGlobals(),
|
||||
$originalResponse,
|
||||
function ($req, $resp) use (&$invoked) {
|
||||
$invoked = true;
|
||||
return $resp;
|
||||
}
|
||||
);
|
||||
|
||||
$this->assertSame($originalResponse, $response);
|
||||
$this->assertTrue($invoked);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function nonSuccessRouteResultJustInvokesNextMiddleware()
|
||||
{
|
||||
$middleware = new CliParamsMiddleware([], 'cli');
|
||||
|
||||
$invoked = false;
|
||||
$originalResponse = new Response();
|
||||
$routeResult = $this->prophesize(RouteResult::class);
|
||||
$routeResult->isSuccess()->willReturn(false)->shouldBeCalledTimes(1);
|
||||
|
||||
$response = $middleware->__invoke(
|
||||
ServerRequestFactory::fromGlobals()->withAttribute(RouteResult::class, $routeResult->reveal()),
|
||||
$originalResponse,
|
||||
function ($req, $resp) use (&$invoked) {
|
||||
$invoked = true;
|
||||
return $resp;
|
||||
}
|
||||
);
|
||||
|
||||
$this->assertSame($originalResponse, $response);
|
||||
$this->assertTrue($invoked);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function properRouteWillInjectAttributeInResponse()
|
||||
{
|
||||
$expectedLongUrl = 'http://www.google.com';
|
||||
$middleware = new CliParamsMiddleware(['foo', 'bar', $expectedLongUrl], 'cli');
|
||||
|
||||
$invoked = false;
|
||||
$originalResponse = new Response();
|
||||
$routeResult = $this->prophesize(RouteResult::class);
|
||||
$routeResult->isSuccess()->willReturn(true)->shouldBeCalledTimes(1);
|
||||
$routeResult->getMatchedRouteName()->willReturn('cli-generate-shortcode')->shouldBeCalledTimes(1);
|
||||
/** @var ServerRequestInterface $request */
|
||||
$request = null;
|
||||
|
||||
$response = $middleware->__invoke(
|
||||
ServerRequestFactory::fromGlobals()->withAttribute(RouteResult::class, $routeResult->reveal()),
|
||||
$originalResponse,
|
||||
function ($req, $resp) use (&$invoked, &$request) {
|
||||
$invoked = true;
|
||||
$request = $req;
|
||||
return $resp;
|
||||
}
|
||||
);
|
||||
|
||||
$this->assertSame($originalResponse, $response);
|
||||
$this->assertEquals($expectedLongUrl, $request->getAttribute('longUrl'));
|
||||
$this->assertTrue($invoked);
|
||||
}
|
||||
}
|
|
@ -1,29 +0,0 @@
|
|||
<?php
|
||||
namespace AcelayaTest\UrlShortener\Middleware\Factory;
|
||||
|
||||
use Acelaya\UrlShortener\Middleware\CliParamsMiddleware;
|
||||
use Acelaya\UrlShortener\Middleware\Factory\CliParamsMiddlewareFactory;
|
||||
use PHPUnit_Framework_TestCase as TestCase;
|
||||
use Zend\ServiceManager\ServiceManager;
|
||||
|
||||
class CliParamsMiddlewareFactoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var CliParamsMiddlewareFactory
|
||||
*/
|
||||
protected $factory;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->factory = new CliParamsMiddlewareFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function serviceIsCreated()
|
||||
{
|
||||
$instance = $this->factory->__invoke(new ServiceManager(), '');
|
||||
$this->assertInstanceOf(CliParamsMiddleware::class, $instance);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue