diff --git a/composer.json b/composer.json index 1f9bf7bd..b8dc3f63 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "zendframework/zend-i18n": "^2.7", "mtymek/expressive-config-manager": "^0.4", "acelaya/zsm-annotated-services": "^0.2.0", + "acelaya/ze-content-based-error-handler": "^1.0", "doctrine/orm": "^2.5", "guzzlehttp/guzzle": "^6.2", "symfony/console": "^3.0", diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.global.php index 209334b2..c0f4b585 100644 --- a/config/autoload/dependencies.global.php +++ b/config/autoload/dependencies.global.php @@ -1,5 +1,4 @@ [ Router\RouterInterface::class => Router\FastRouteRouter::class, - 'Zend\Expressive\FinalHandler' => ContentBasedErrorHandler::class, ], ], diff --git a/config/autoload/errorhandler.local.php.dist b/config/autoload/errorhandler.local.php.dist index 7e361e0a..40316fd9 100644 --- a/config/autoload/errorhandler.local.php.dist +++ b/config/autoload/errorhandler.local.php.dist @@ -1,5 +1,5 @@ EntityManagerFactory::class, GuzzleHttp\Client::class => InvokableFactory::class, Cache::class => CacheFactory::class, - LoggerInterface::class => LoggerFactory::class, 'Logger_Shlink' => LoggerFactory::class, Translator::class => TranslatorFactory::class, @@ -30,17 +28,15 @@ return [ LocaleMiddleware::class => AnnotatedFactory::class, IpLocationResolver::class => AnnotatedFactory::class, - - ErrorHandler\ContentBasedErrorHandler::class => AnnotatedFactory::class, - ErrorHandler\ErrorHandlerManager::class => ErrorHandler\ErrorHandlerManagerFactory::class, ], 'aliases' => [ 'em' => EntityManager::class, 'httpClient' => GuzzleHttp\Client::class, 'translator' => Translator::class, 'logger' => LoggerInterface::class, - Logger::class => LoggerInterface::class, AnnotatedFactory::CACHE_SERVICE => Cache::class, + Logger::class => 'Logger_Shlink', + LoggerInterface::class => 'Logger_Shlink', ], ], diff --git a/module/Common/config/error-handler.config.php b/module/Common/config/error-handler.config.php deleted file mode 100644 index d19b9ac6..00000000 --- a/module/Common/config/error-handler.config.php +++ /dev/null @@ -1,22 +0,0 @@ - [ - 'plugins' => [ - 'invokables' => [ - 'text/plain' => FinalHandler::class, - ], - 'factories' => [ - ContentBasedErrorHandler::DEFAULT_CONTENT => TemplatedErrorHandlerFactory::class, - ], - 'aliases' => [ - 'application/xhtml+xml' => ContentBasedErrorHandler::DEFAULT_CONTENT, - ], - ], - ], - -]; diff --git a/module/Common/src/ErrorHandler/ContentBasedErrorHandler.php b/module/Common/src/ErrorHandler/ContentBasedErrorHandler.php deleted file mode 100644 index be19e848..00000000 --- a/module/Common/src/ErrorHandler/ContentBasedErrorHandler.php +++ /dev/null @@ -1,76 +0,0 @@ -errorHandlerManager = $errorHandlerManager; - } - - /** - * Final handler for an application. - * - * @param Request $request - * @param Response $response - * @param null|mixed $err - * @return Response - */ - public function __invoke(Request $request, Response $response, $err = null) - { - // Try to get an error handler for provided request accepted type - $errorHandler = $this->resolveErrorHandlerFromAcceptHeader($request); - return $errorHandler($request, $response, $err); - } - - /** - * Tries to resolve - * - * @param Request $request - * @return callable - */ - protected function resolveErrorHandlerFromAcceptHeader(Request $request) - { - // Try to find an error handler for one of the accepted content types - $accepts = $request->hasHeader('Accept') ? $request->getHeaderLine('Accept') : self::DEFAULT_CONTENT; - $accepts = explode(',', $accepts); - foreach ($accepts as $accept) { - if (! $this->errorHandlerManager->has($accept)) { - continue; - } - - return $this->errorHandlerManager->get($accept); - } - - // If it wasn't possible to find an error handler for accepted content type, use default one if registered - if ($this->errorHandlerManager->has(self::DEFAULT_CONTENT)) { - return $this->errorHandlerManager->get(self::DEFAULT_CONTENT); - } - - // It wasn't possible to find an error handler - throw new InvalidArgumentException(sprintf( - 'It wasn\'t possible to find an error handler for ["%s"] content types. ' - . 'Make sure you have registered at least the default "%s" content type', - implode('", "', $accepts), - self::DEFAULT_CONTENT - )); - } -} diff --git a/module/Common/src/ErrorHandler/ErrorHandlerInterface.php b/module/Common/src/ErrorHandler/ErrorHandlerInterface.php deleted file mode 100644 index 9676c40a..00000000 --- a/module/Common/src/ErrorHandler/ErrorHandlerInterface.php +++ /dev/null @@ -1,18 +0,0 @@ -get('config')['error_handler']; - $plugins = isset($config['plugins']) ? $config['plugins'] : []; - return new ErrorHandlerManager($container, $plugins); - } -} diff --git a/module/Common/src/ErrorHandler/ErrorHandlerManagerInterface.php b/module/Common/src/ErrorHandler/ErrorHandlerManagerInterface.php deleted file mode 100644 index 323bfe4b..00000000 --- a/module/Common/src/ErrorHandler/ErrorHandlerManagerInterface.php +++ /dev/null @@ -1,9 +0,0 @@ -configProvider->__invoke(); - $this->assertArrayHasKey('error_handler', $config); $this->assertArrayHasKey('middleware_pipeline', $config); $this->assertArrayHasKey('dependencies', $config); $this->assertArrayHasKey('twig', $config); diff --git a/module/Common/test/ErrorHandler/ContentBasedErrorHandlerTest.php b/module/Common/test/ErrorHandler/ContentBasedErrorHandlerTest.php deleted file mode 100644 index 6b480e54..00000000 --- a/module/Common/test/ErrorHandler/ContentBasedErrorHandlerTest.php +++ /dev/null @@ -1,75 +0,0 @@ -errorHandler = new ContentBasedErrorHandler(new ErrorHandlerManager(new ServiceManager(), [ - 'factories' => [ - 'text/html' => [$this, 'factory'], - 'application/json' => [$this, 'factory'], - ], - ])); - } - - public function factory($container, $name) - { - return function () use ($name) { - return $name; - }; - } - - /** - * @test - */ - public function correctAcceptHeaderValueInvokesErrorHandler() - { - $request = ServerRequestFactory::fromGlobals()->withHeader('Accept', 'foo/bar,application/json'); - $result = $this->errorHandler->__invoke($request, new Response()); - $this->assertEquals('application/json', $result); - } - - /** - * @test - */ - public function defaultContentTypeIsUsedWhenNoAcceptHeaderisPresent() - { - $request = ServerRequestFactory::fromGlobals(); - $result = $this->errorHandler->__invoke($request, new Response()); - $this->assertEquals('text/html', $result); - } - - /** - * @test - */ - public function defaultContentTypeIsUsedWhenAcceptedContentIsNotSupported() - { - $request = ServerRequestFactory::fromGlobals()->withHeader('Accept', 'foo/bar,text/xml'); - $result = $this->errorHandler->__invoke($request, new Response()); - $this->assertEquals('text/html', $result); - } - - /** - * @test - * @expectedException \Shlinkio\Shlink\Common\Exception\InvalidArgumentException - */ - public function ifNoErrorHandlerIsFoundAnExceptionIsThrown() - { - $this->errorHandler = new ContentBasedErrorHandler(new ErrorHandlerManager(new ServiceManager(), [])); - $request = ServerRequestFactory::fromGlobals()->withHeader('Accept', 'foo/bar,text/xml'); - $result = $this->errorHandler->__invoke($request, new Response()); - } -} diff --git a/module/Common/test/ErrorHandler/ErrorHandlerManagerFactoryTest.php b/module/Common/test/ErrorHandler/ErrorHandlerManagerFactoryTest.php deleted file mode 100644 index be6d4e6d..00000000 --- a/module/Common/test/ErrorHandler/ErrorHandlerManagerFactoryTest.php +++ /dev/null @@ -1,35 +0,0 @@ -factory = new ErrorHandlerManagerFactory(); - } - - /** - * @test - */ - public function serviceIsCreated() - { - $instance = $this->factory->__invoke(new ServiceManager(['services' => [ - 'config' => [ - 'error_handler' => [ - 'plugins' => [], - ], - ], - ]]), ''); - $this->assertInstanceOf(ErrorHandlerManager::class, $instance); - } -} diff --git a/module/Common/test/ErrorHandler/ErrorHandlerManagerTest.php b/module/Common/test/ErrorHandler/ErrorHandlerManagerTest.php deleted file mode 100644 index 4b14f113..00000000 --- a/module/Common/test/ErrorHandler/ErrorHandlerManagerTest.php +++ /dev/null @@ -1,45 +0,0 @@ -pluginManager = new ErrorHandlerManager(new ServiceManager(), [ - 'services' => [ - 'foo' => function () { - }, - ], - 'invokables' => [ - 'invalid' => \stdClass::class, - ] - ]); - } - - /** - * @test - */ - public function callablesAreReturned() - { - $instance = $this->pluginManager->get('foo'); - $this->assertInstanceOf(\Closure::class, $instance); - } - - /** - * @test - * @expectedException \Zend\ServiceManager\Exception\InvalidServiceException - */ - public function nonCallablesThrowException() - { - $this->pluginManager->get('invalid'); - } -} diff --git a/module/Rest/src/ErrorHandler/JsonErrorHandler.php b/module/Rest/src/ErrorHandler/JsonErrorHandler.php index 248bb6ae..79667c73 100644 --- a/module/Rest/src/ErrorHandler/JsonErrorHandler.php +++ b/module/Rest/src/ErrorHandler/JsonErrorHandler.php @@ -1,9 +1,9 @@