mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-20 01:09:56 +03:00
Added support for custom slug in shortcode command
This commit is contained in:
parent
1f7a94794d
commit
fd468cd4e9
1 changed files with 14 additions and 1 deletions
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
namespace Shlinkio\Shlink\CLI\Command\Shortcode;
|
namespace Shlinkio\Shlink\CLI\Command\Shortcode;
|
||||||
|
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Helper\QuestionHelper;
|
use Symfony\Component\Console\Helper\QuestionHelper;
|
||||||
|
@ -61,6 +62,9 @@ class GenerateShortcodeCommand extends Command
|
||||||
->addOption('validUntil', 'u', InputOption::VALUE_REQUIRED, $this->translator->translate(
|
->addOption('validUntil', 'u', InputOption::VALUE_REQUIRED, $this->translator->translate(
|
||||||
'The date until which this short URL will be valid. '
|
'The date until which this short URL will be valid. '
|
||||||
. 'If someone tries to access it after this date, it will not be found.'
|
. 'If someone tries to access it after this date, it will not be found.'
|
||||||
|
))
|
||||||
|
->addOption('customSlug', 'c', InputOption::VALUE_REQUIRED, $this->translator->translate(
|
||||||
|
'If provided, this slug will be used instead of generating a short code'
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +98,7 @@ class GenerateShortcodeCommand extends Command
|
||||||
$processedTags = array_merge($processedTags, $explodedTags);
|
$processedTags = array_merge($processedTags, $explodedTags);
|
||||||
}
|
}
|
||||||
$tags = $processedTags;
|
$tags = $processedTags;
|
||||||
|
$customSlug = $input->getOption('customSlug');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (! isset($longUrl)) {
|
if (! isset($longUrl)) {
|
||||||
|
@ -105,7 +110,8 @@ class GenerateShortcodeCommand extends Command
|
||||||
new Uri($longUrl),
|
new Uri($longUrl),
|
||||||
$tags,
|
$tags,
|
||||||
$this->getOptionalDate($input, 'validSince'),
|
$this->getOptionalDate($input, 'validSince'),
|
||||||
$this->getOptionalDate($input, 'validUntil')
|
$this->getOptionalDate($input, 'validUntil'),
|
||||||
|
$customSlug
|
||||||
);
|
);
|
||||||
$shortUrl = (new Uri())->withPath($shortCode)
|
$shortUrl = (new Uri())->withPath($shortCode)
|
||||||
->withScheme($this->domainConfig['schema'])
|
->withScheme($this->domainConfig['schema'])
|
||||||
|
@ -122,6 +128,13 @@ class GenerateShortcodeCommand extends Command
|
||||||
) . '</error>',
|
) . '</error>',
|
||||||
$longUrl
|
$longUrl
|
||||||
));
|
));
|
||||||
|
} catch (NonUniqueSlugException $e) {
|
||||||
|
$output->writeln(sprintf(
|
||||||
|
'<error>' . $this->translator->translate(
|
||||||
|
'Provided slug "%s" is already in use by another URL. Try with a different one.'
|
||||||
|
) . '</error>',
|
||||||
|
$customSlug
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue