mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-17 07:49:54 +03:00
Split ContentBasedErrorHandler responsibilities into two separated classes
This commit is contained in:
parent
ab6aa99a6d
commit
2f5119d0b3
5 changed files with 57 additions and 21 deletions
|
@ -1,6 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
use Shlinkio\Shlink\Common\Expressive\ContentBasedErrorHandlerFactory;
|
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
|
||||||
use Shlinkio\Shlink\Rest\Expressive\JsonErrorHandler;
|
use Shlinkio\Shlink\Common\Expressive\ContentBasedErrorHandler;
|
||||||
|
use Shlinkio\Shlink\Common\Expressive\ErrorHandlerManager;
|
||||||
|
use Shlinkio\Shlink\Common\Expressive\ErrorHandlerManagerFactory;
|
||||||
use Zend\Expressive;
|
use Zend\Expressive;
|
||||||
use Zend\Expressive\Container;
|
use Zend\Expressive\Container;
|
||||||
use Zend\Expressive\Helper;
|
use Zend\Expressive\Helper;
|
||||||
|
@ -23,11 +25,13 @@ return [
|
||||||
Router\FastRouteRouter::class => InvokableFactory::class,
|
Router\FastRouteRouter::class => InvokableFactory::class,
|
||||||
|
|
||||||
// View
|
// View
|
||||||
'Zend\Expressive\FinalHandler' => ContentBasedErrorHandlerFactory::class,
|
ContentBasedErrorHandler::class => AnnotatedFactory::class,
|
||||||
|
ErrorHandlerManager::class => ErrorHandlerManagerFactory::class,
|
||||||
Template\TemplateRendererInterface::class => Twig\TwigRendererFactory::class,
|
Template\TemplateRendererInterface::class => Twig\TwigRendererFactory::class,
|
||||||
],
|
],
|
||||||
'aliases' => [
|
'aliases' => [
|
||||||
Router\RouterInterface::class => Router\FastRouteRouter::class,
|
Router\RouterInterface::class => Router\FastRouteRouter::class,
|
||||||
|
'Zend\Expressive\FinalHandler' => ContentBasedErrorHandler::class,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
|
@ -1,27 +1,29 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Shlinkio\Shlink\Common\Expressive;
|
namespace Shlinkio\Shlink\Common\Expressive;
|
||||||
|
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
||||||
use Zend\ServiceManager\AbstractPluginManager;
|
|
||||||
use Zend\ServiceManager\Exception\InvalidServiceException;
|
|
||||||
|
|
||||||
class ContentBasedErrorHandler extends AbstractPluginManager implements ErrorHandlerInterface
|
class ContentBasedErrorHandler implements ErrorHandlerInterface
|
||||||
{
|
{
|
||||||
const DEFAULT_CONTENT = 'text/html';
|
const DEFAULT_CONTENT = 'text/html';
|
||||||
|
|
||||||
public function validate($instance)
|
/**
|
||||||
{
|
* @var ErrorHandlerManagerInterface
|
||||||
if (is_callable($instance)) {
|
*/
|
||||||
return;
|
private $errorHandlerManager;
|
||||||
}
|
|
||||||
|
|
||||||
throw new InvalidServiceException(sprintf(
|
/**
|
||||||
'Only callables are valid plugins for "%s". "%s" provided',
|
* ContentBasedErrorHandler constructor.
|
||||||
__CLASS__,
|
* @param ErrorHandlerManagerInterface|ErrorHandlerManager $errorHandlerManager
|
||||||
is_object($instance) ? get_class($instance) : gettype($instance)
|
*
|
||||||
));
|
* @Inject({ErrorHandlerManager::class})
|
||||||
|
*/
|
||||||
|
public function __construct(ErrorHandlerManagerInterface $errorHandlerManager)
|
||||||
|
{
|
||||||
|
$this->errorHandlerManager = $errorHandlerManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -50,11 +52,11 @@ class ContentBasedErrorHandler extends AbstractPluginManager implements ErrorHan
|
||||||
$accepts = $request->hasHeader('Accept') ? $request->getHeaderLine('Accept') : self::DEFAULT_CONTENT;
|
$accepts = $request->hasHeader('Accept') ? $request->getHeaderLine('Accept') : self::DEFAULT_CONTENT;
|
||||||
$accepts = explode(',', $accepts);
|
$accepts = explode(',', $accepts);
|
||||||
foreach ($accepts as $accept) {
|
foreach ($accepts as $accept) {
|
||||||
if (! $this->has($accept)) {
|
if (! $this->errorHandlerManager->has($accept)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->get($accept);
|
return $this->errorHandlerManager->get($accept);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new InvalidArgumentException(sprintf(
|
throw new InvalidArgumentException(sprintf(
|
||||||
|
|
21
module/Common/src/Expressive/ErrorHandlerManager.php
Normal file
21
module/Common/src/Expressive/ErrorHandlerManager.php
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
namespace Shlinkio\Shlink\Common\Expressive;
|
||||||
|
|
||||||
|
use Zend\ServiceManager\AbstractPluginManager;
|
||||||
|
use Zend\ServiceManager\Exception\InvalidServiceException;
|
||||||
|
|
||||||
|
class ErrorHandlerManager extends AbstractPluginManager implements ErrorHandlerManagerInterface
|
||||||
|
{
|
||||||
|
public function validate($instance)
|
||||||
|
{
|
||||||
|
if (is_callable($instance)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new InvalidServiceException(sprintf(
|
||||||
|
'Only callables are valid plugins for "%s". "%s" provided',
|
||||||
|
__CLASS__,
|
||||||
|
is_object($instance) ? get_class($instance) : gettype($instance)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,7 @@ use Zend\ServiceManager\Exception\ServiceNotCreatedException;
|
||||||
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
use Zend\ServiceManager\Exception\ServiceNotFoundException;
|
||||||
use Zend\ServiceManager\Factory\FactoryInterface;
|
use Zend\ServiceManager\Factory\FactoryInterface;
|
||||||
|
|
||||||
class ContentBasedErrorHandlerFactory implements FactoryInterface
|
class ErrorHandlerManagerFactory implements FactoryInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Create an object
|
* Create an object
|
||||||
|
@ -25,6 +25,6 @@ class ContentBasedErrorHandlerFactory implements FactoryInterface
|
||||||
{
|
{
|
||||||
$config = $container->get('config')['error_handler'];
|
$config = $container->get('config')['error_handler'];
|
||||||
$plugins = isset($config['plugins']) ? $config['plugins'] : [];
|
$plugins = isset($config['plugins']) ? $config['plugins'] : [];
|
||||||
return new ContentBasedErrorHandler($container, $plugins);
|
return new ErrorHandlerManager($container, $plugins);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
namespace Shlinkio\Shlink\Common\Expressive;
|
||||||
|
|
||||||
|
use Interop\Container\ContainerInterface;
|
||||||
|
|
||||||
|
interface ErrorHandlerManagerInterface extends ContainerInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue