Updated installer so that it no longer asks for a charset and instead just generates one

This commit is contained in:
Alejandro Celaya 2019-02-03 13:02:12 +01:00
parent 79c132219b
commit 2d18ef5cee
6 changed files with 35 additions and 19 deletions

View file

@ -1,7 +1,8 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
use Shlinkio\Shlink\Installer\Config\Plugin\DatabaseConfigCustomizer; use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Installer\Config\Plugin;
use Shlinkio\Shlink\Installer\Factory\InstallApplicationFactory; use Shlinkio\Shlink\Installer\Factory\InstallApplicationFactory;
use Symfony\Component\Console\Application; use Symfony\Component\Console\Application;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
@ -19,9 +20,13 @@ $container = new ServiceManager([
Filesystem::class => InvokableFactory::class, Filesystem::class => InvokableFactory::class,
], ],
'services' => [ 'services' => [
'random-chars-generator' => function () {
return str_shuffle(UrlShortenerOptions::DEFAULT_CHARS);
},
'config' => [ 'config' => [
ConfigAbstractFactory::class => [ ConfigAbstractFactory::class => [
DatabaseConfigCustomizer::class => [Filesystem::class], Plugin\DatabaseConfigCustomizer::class => [Filesystem::class],
Plugin\UrlShortenerConfigCustomizer::class => ['random-chars-generator'],
], ],
], ],
], ],

View file

@ -7,7 +7,7 @@ use Zend\Stdlib\AbstractOptions;
class UrlShortenerOptions extends AbstractOptions class UrlShortenerOptions extends AbstractOptions
{ {
public const DEFAULT_CHARS = '123456789bcdfghjkmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'; public const DEFAULT_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
// phpcs:disable // phpcs:disable
protected $__strictMode__ = false; protected $__strictMode__ = false;

View file

@ -71,13 +71,13 @@ class UrlShortenerTest extends TestCase
*/ */
public function urlIsProperlyShortened(): void public function urlIsProperlyShortened(): void
{ {
// 10 -> 12C1c // 10 -> 0Q1Y
$shortUrl = $this->urlShortener->urlToShortCode( $shortUrl = $this->urlShortener->urlToShortCode(
new Uri('http://foobar.com/12345/hello?foo=bar'), new Uri('http://foobar.com/12345/hello?foo=bar'),
[], [],
ShortUrlMeta::createEmpty() ShortUrlMeta::createEmpty()
); );
$this->assertEquals('12C1c', $shortUrl->getShortCode()); $this->assertEquals('0Q1Y', $shortUrl->getShortCode());
} }
/** /**
@ -215,7 +215,6 @@ class UrlShortenerTest extends TestCase
*/ */
public function shortCodeIsProperlyParsed(): void public function shortCodeIsProperlyParsed(): void
{ {
// 12C1c -> 10
$shortCode = '12C1c'; $shortCode = '12C1c';
$shortUrl = new ShortUrl('expected_url'); $shortUrl = new ShortUrl('expected_url');
$shortUrl->setShortCode($shortCode); $shortUrl->setShortCode($shortCode);

View file

@ -3,13 +3,13 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Installer\Config\Plugin; namespace Shlinkio\Shlink\Installer\Config\Plugin;
use Shlinkio\Shlink\Core\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig; use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
use Shlinkio\Shlink\Installer\Util\AskUtilsTrait; use Shlinkio\Shlink\Installer\Util\AskUtilsTrait;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use function array_diff; use function array_diff;
use function array_keys; use function array_keys;
use function str_shuffle; use function count;
use function Functional\contains;
class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface
{ {
@ -30,6 +30,14 @@ class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface
self::NOT_FOUND_REDIRECT_TO, self::NOT_FOUND_REDIRECT_TO,
]; ];
/** @var callable */
private $randomCharsGenerator;
public function __construct(callable $randomCharsGenerator)
{
$this->randomCharsGenerator = $randomCharsGenerator;
}
public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
{ {
$urlShortener = $appConfig->getUrlShortener(); $urlShortener = $appConfig->getUrlShortener();
@ -40,7 +48,11 @@ class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface
return; return;
} }
$io->title('URL SHORTENER'); // Print title if there are keys other than "chars"
$onlyKeyIsChars = count($keysToAskFor) === 1 && contains($keysToAskFor, self::CHARS);
if (! $onlyKeyIsChars) {
$io->title('URL SHORTENER');
}
foreach ($keysToAskFor as $key) { foreach ($keysToAskFor as $key) {
// Skip not found redirect URL when the user decided not to redirect // Skip not found redirect URL when the user decided not to redirect
if ($key === self::NOT_FOUND_REDIRECT_TO && ! $urlShortener[self::ENABLE_NOT_FOUND_REDIRECTION]) { if ($key === self::NOT_FOUND_REDIRECT_TO && ! $urlShortener[self::ENABLE_NOT_FOUND_REDIRECTION]) {
@ -64,9 +76,8 @@ class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface
case self::HOSTNAME: case self::HOSTNAME:
return $this->askRequired($io, 'hostname', 'Hostname for generated URLs'); return $this->askRequired($io, 'hostname', 'Hostname for generated URLs');
case self::CHARS: case self::CHARS:
return $io->ask( // This won't actually ask anything, just generate the chars. Asking for this was confusing for users
'Character set for generated short codes (leave empty to autogenerate one)' return ($this->randomCharsGenerator)();
) ?: str_shuffle(UrlShortenerOptions::DEFAULT_CHARS);
case self::VALIDATE_URL: case self::VALIDATE_URL:
return $io->confirm('Do you want to validate long urls by 200 HTTP status code on response'); return $io->confirm('Do you want to validate long urls by 200 HTTP status code on response');
case self::ENABLE_NOT_FOUND_REDIRECTION: case self::ENABLE_NOT_FOUND_REDIRECTION:

View file

@ -43,7 +43,7 @@ class InstallApplicationFactory implements FactoryInterface
$container->get(Filesystem::class), $container->get(Filesystem::class),
new ConfigCustomizerManager($container, ['factories' => [ new ConfigCustomizerManager($container, ['factories' => [
Plugin\DatabaseConfigCustomizer::class => ConfigAbstractFactory::class, Plugin\DatabaseConfigCustomizer::class => ConfigAbstractFactory::class,
Plugin\UrlShortenerConfigCustomizer::class => InvokableFactory::class, Plugin\UrlShortenerConfigCustomizer::class => ConfigAbstractFactory::class,
Plugin\LanguageConfigCustomizer::class => InvokableFactory::class, Plugin\LanguageConfigCustomizer::class => InvokableFactory::class,
Plugin\ApplicationConfigCustomizer::class => InvokableFactory::class, Plugin\ApplicationConfigCustomizer::class => InvokableFactory::class,
]]), ]]),

View file

@ -21,7 +21,9 @@ class UrlShortenerConfigCustomizerTest extends TestCase
{ {
$this->io = $this->prophesize(SymfonyStyle::class); $this->io = $this->prophesize(SymfonyStyle::class);
$this->io->title(Argument::any())->willReturn(null); $this->io->title(Argument::any())->willReturn(null);
$this->plugin = new UrlShortenerConfigCustomizer(); $this->plugin = new UrlShortenerConfigCustomizer(function () {
return 'the_chars';
});
} }
/** /**
@ -40,12 +42,12 @@ class UrlShortenerConfigCustomizerTest extends TestCase
$this->assertEquals([ $this->assertEquals([
'SCHEMA' => 'chosen', 'SCHEMA' => 'chosen',
'HOSTNAME' => 'asked', 'HOSTNAME' => 'asked',
'CHARS' => 'asked', 'CHARS' => 'the_chars',
'VALIDATE_URL' => true, 'VALIDATE_URL' => true,
'ENABLE_NOT_FOUND_REDIRECTION' => true, 'ENABLE_NOT_FOUND_REDIRECTION' => true,
'NOT_FOUND_REDIRECT_TO' => 'asked', 'NOT_FOUND_REDIRECT_TO' => 'asked',
], $config->getUrlShortener()); ], $config->getUrlShortener());
$ask->shouldHaveBeenCalledTimes(3); $ask->shouldHaveBeenCalledTimes(2);
$choice->shouldHaveBeenCalledOnce(); $choice->shouldHaveBeenCalledOnce();
$confirm->shouldHaveBeenCalledTimes(2); $confirm->shouldHaveBeenCalledTimes(2);
} }
@ -61,7 +63,6 @@ class UrlShortenerConfigCustomizerTest extends TestCase
$config = new CustomizableAppConfig(); $config = new CustomizableAppConfig();
$config->setUrlShortener([ $config->setUrlShortener([
'SCHEMA' => 'foo', 'SCHEMA' => 'foo',
'HOSTNAME' => 'foo',
'ENABLE_NOT_FOUND_REDIRECTION' => true, 'ENABLE_NOT_FOUND_REDIRECTION' => true,
'NOT_FOUND_REDIRECT_TO' => 'foo', 'NOT_FOUND_REDIRECT_TO' => 'foo',
]); ]);
@ -70,8 +71,8 @@ class UrlShortenerConfigCustomizerTest extends TestCase
$this->assertEquals([ $this->assertEquals([
'SCHEMA' => 'foo', 'SCHEMA' => 'foo',
'HOSTNAME' => 'foo', 'HOSTNAME' => 'asked',
'CHARS' => 'asked', 'CHARS' => 'the_chars',
'VALIDATE_URL' => false, 'VALIDATE_URL' => false,
'ENABLE_NOT_FOUND_REDIRECTION' => true, 'ENABLE_NOT_FOUND_REDIRECTION' => true,
'NOT_FOUND_REDIRECT_TO' => 'foo', 'NOT_FOUND_REDIRECT_TO' => 'foo',