diff --git a/CHANGELOG.md b/CHANGELOG.md index dfb5e9dc..b7b9aa56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ All notable changes to `Slytherin` will be documented in this file. ## [0.9.7](https://github.com/rougin/slytherin/compare/v0.9.6...master) - Unreleased +### Added +- `preferred` property in `HttpIntegration`, `RoutingIntegration` for specifying third-party integrations + ### Fixed - Type hinting of all classes using `PHPStan` diff --git a/src/Http/HttpIntegration.php b/src/Http/HttpIntegration.php index 63984849..42c50995 100644 --- a/src/Http/HttpIntegration.php +++ b/src/Http/HttpIntegration.php @@ -22,6 +22,11 @@ */ class HttpIntegration implements IntegrationInterface { + /** + * @var string|null + */ + protected $preferred = null; + /** * Defines the specified integration. * @@ -119,7 +124,13 @@ protected function headers(array $server) */ protected function resolve(ContainerInterface $container, ServerRequestInterface $request, ResponseInterface $response) { - if (class_exists('Zend\Diactoros\ServerRequestFactory')) + $empty = $this->preferred === null; + + $hasDiactoros = class_exists('Zend\Diactoros\ServerRequestFactory'); + + $wantDiactoros = $this->preferred === 'diactoros'; + + if (($empty || $wantDiactoros) && $hasDiactoros) { $response = new ZendResponse; diff --git a/src/Routing/RoutingIntegration.php b/src/Routing/RoutingIntegration.php index f63656a4..383021db 100644 --- a/src/Routing/RoutingIntegration.php +++ b/src/Routing/RoutingIntegration.php @@ -16,6 +16,11 @@ */ class RoutingIntegration implements IntegrationInterface { + /** + * @var string|null + */ + protected $preferred = null; + /** * Defines the specified integration. * @@ -25,15 +30,27 @@ class RoutingIntegration implements IntegrationInterface */ public function define(ContainerInterface $container, Configuration $config) { + $hasFastroute = interface_exists('FastRoute\Dispatcher'); + + $wantFastroute = $this->preferred === 'fastroute'; + + $hasPhroute = class_exists('Phroute\Phroute\Dispatcher'); + + $wantPhroute = $this->preferred === 'phroute'; + $dispatcher = new Dispatcher; $router = $config->get('app.router', new Router); - if (interface_exists('FastRoute\Dispatcher')) { + $empty = $this->preferred === null; + + if (($empty || $wantFastroute) && $hasFastroute) + { $dispatcher = new FastRouteDispatcher; } - if (class_exists('Phroute\Phroute\Dispatcher')) { + if (($empty || $wantPhroute) && $hasPhroute) + { $resolver = new PhrouteResolver($container); $dispatcher = new PhrouteDispatcher(null, $resolver);