Skip to content

Commit

Permalink
Add "preferred" for specified integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 25, 2023
1 parent 305f0fa commit d6987f4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
13 changes: 12 additions & 1 deletion src/Http/HttpIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
*/
class HttpIntegration implements IntegrationInterface
{
/**
* @var string|null
*/
protected $preferred = null;

/**
* Defines the specified integration.
*
Expand Down Expand Up @@ -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;

Expand Down
21 changes: 19 additions & 2 deletions src/Routing/RoutingIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
class RoutingIntegration implements IntegrationInterface
{
/**
* @var string|null
*/
protected $preferred = null;

/**
* Defines the specified integration.
*
Expand All @@ -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);
Expand Down

0 comments on commit d6987f4

Please sign in to comment.