mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-14 04:00:57 +03:00
Created options to enable redirection to external page when short code is not found
This commit is contained in:
parent
95d4cde649
commit
3eddacdff8
6 changed files with 51 additions and 95 deletions
|
@ -13,6 +13,10 @@ return [
|
|||
],
|
||||
'shortcode_chars' => env('SHORTCODE_CHARS', UrlShortener::DEFAULT_CHARS),
|
||||
'validate_url' => true,
|
||||
'not_found_short_url' => [
|
||||
'enable_redirection' => false,
|
||||
'redirect_to' => null,
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
|
|
|
@ -16,8 +16,9 @@ return [
|
|||
|
||||
'dependencies' => [
|
||||
'factories' => [
|
||||
Options\AppOptions::class => Options\AppOptionsFactory::class,
|
||||
Options\DeleteShortUrlsOptions::class => Options\DeleteShortUrlsOptionsFactory::class,
|
||||
Options\AppOptions::class => ConfigAbstractFactory::class,
|
||||
Options\DeleteShortUrlsOptions::class => ConfigAbstractFactory::class,
|
||||
Options\NotFoundShortUrlOptions::class => ConfigAbstractFactory::class,
|
||||
NotFoundHandler::class => ConfigAbstractFactory::class,
|
||||
|
||||
// Services
|
||||
|
@ -40,6 +41,10 @@ return [
|
|||
ConfigAbstractFactory::class => [
|
||||
NotFoundHandler::class => [TemplateRendererInterface::class],
|
||||
|
||||
Options\AppOptions::class => ['config.app_options'],
|
||||
Options\DeleteShortUrlsOptions::class => ['config.delete_short_urls'],
|
||||
Options\NotFoundShortUrlOptions::class => ['config.url_shortener.not_found_short_url'],
|
||||
|
||||
// Services
|
||||
Service\UrlShortener::class => [
|
||||
'httpClient',
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Options;
|
||||
|
||||
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 AppOptionsFactory 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->has('config') ? $container->get('config') : [];
|
||||
return new AppOptions($config['app_options'] ?? []);
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Options;
|
||||
|
||||
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 DeleteShortUrlsOptionsFactory 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->has('config') ? $container->get('config') : [];
|
||||
return new DeleteShortUrlsOptions($config['delete_short_urls'] ?? []);
|
||||
}
|
||||
}
|
40
module/Core/src/Options/NotFoundShortUrlOptions.php
Normal file
40
module/Core/src/Options/NotFoundShortUrlOptions.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Options;
|
||||
|
||||
use Zend\Stdlib\AbstractOptions;
|
||||
|
||||
class NotFoundShortUrlOptions extends AbstractOptions
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $enableRedirection = false;
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
private $redirectTo;
|
||||
|
||||
public function isRedirectionEnabled(): bool
|
||||
{
|
||||
return $this->enableRedirection;
|
||||
}
|
||||
|
||||
protected function enableRedirection(bool $enableRedirection = true): self
|
||||
{
|
||||
$this->enableRedirection = $enableRedirection;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRedirectTo(): ?string
|
||||
{
|
||||
return $this->redirectTo;
|
||||
}
|
||||
|
||||
protected function setRedirectTo(string $redirectTo): self
|
||||
{
|
||||
$this->redirectTo = $redirectTo;
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioTest\Shlink\Core\Options;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Shlinkio\Shlink\Core\Options\AppOptions;
|
||||
use Shlinkio\Shlink\Core\Options\AppOptionsFactory;
|
||||
use Zend\ServiceManager\ServiceManager;
|
||||
|
||||
class AppOptionsFactoryTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var AppOptionsFactory
|
||||
*/
|
||||
protected $factory;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->factory = new AppOptionsFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function serviceIsCreated()
|
||||
{
|
||||
$instance = $this->factory->__invoke(new ServiceManager([]), '');
|
||||
$this->assertInstanceOf(AppOptions::class, $instance);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue