Skip to content

Commit

Permalink
Add "preferred" in MiddlewareIntegration
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 25, 2023
1 parent e7c6f62 commit e72c612
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
- `preferred` property in integrations

### Fixed
- Type hinting of all classes using `PHPStan`
Expand Down
13 changes: 11 additions & 2 deletions src/Middleware/MiddlewareIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
*/
class MiddlewareIntegration implements IntegrationInterface
{
/**
* @var string|null
*/
protected $preferred = null;

/**
* Defines the specified integration.
*
Expand Down Expand Up @@ -61,9 +66,13 @@ protected function dispatcher(ResponseInterface $response, $stack)
{
$dispatcher = new Dispatcher($stack, $response);

$exists = class_exists('Zend\Stratigility\MiddlewarePipe');
$empty = $this->preferred === null;

$hasZend = class_exists('Zend\Stratigility\MiddlewarePipe');

$wantZend = $this->preferred === 'stratigility';

if (! $exists) return $dispatcher;
if (($empty || (! $wantZend)) || ! $hasZend) return $dispatcher;

$pipe = new MiddlewarePipe;

Expand Down
18 changes: 17 additions & 1 deletion tests/Forward/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,26 @@ public function test_run_with_sample_text()
{
$builder = new Builder;

$builder->setUrl('GET', '/v1/hello');
$builder->setUrl('GET', '/hello');

$this->expectOutputString('Hello world!');

$builder->make()->run();
}

/**
* @runInSeparateProcess
*
* @return void
*/
public function test_run_with_arguments_in_uri()
{
$builder = new Builder;

$builder->setUrl('GET', '/hi/Rougin');

$this->expectOutputString('Hello, Rougin!');

$builder->make()->run();
}
}
6 changes: 3 additions & 3 deletions tests/Forward/Fixture/Config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@

$packages = array();

// Testing Packages -----------------------------------------------------
// Testing Packages --------------------------------------------------------
$packages[] = 'Rougin\Slytherin\Forward\Fixture\Packages\HttpPackage';
$packages[] = 'Rougin\Slytherin\Forward\Fixture\Packages\MiddlewarePackage';
$packages[] = 'Rougin\Slytherin\Forward\Fixture\Packages\RoutingPackage';
// ----------------------------------------------------------------------
// -------------------------------------------------------------------------

// Slytherin Integrations --------------------------------------------
$packages[] = 'Rougin\Slytherin\Integration\ConfigurationIntegration';
$packages[] = 'Rougin\Slytherin\Middleware\MiddlewareIntegration';
// -------------------------------------------------------------------

$config['router'] = new Router;
Expand Down
22 changes: 22 additions & 0 deletions tests/Forward/Fixture/Depots/SestDepot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rougin\Slytherin\Forward\Fixture\Depots;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class SestDepot
{
protected $test;

public function __construct(TestDepot $test)
{
$this->test = $test;
}

public function text($data)
{
return $this->test->text($data);
}
}
31 changes: 31 additions & 0 deletions tests/Forward/Fixture/Depots/TestDepot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rougin\Slytherin\Forward\Fixture\Depots;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class TestDepot
{
protected $request;

protected $response;

public function __construct(ServerRequestInterface $request, ResponseInterface $response)
{
$this->request = $request;

$this->response = $response;
}

public function text($data)
{
$this->response->getBody()->write($data);

return $this->response;
}
}
14 changes: 14 additions & 0 deletions tests/Forward/Fixture/Packages/MiddlewarePackage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rougin\Slytherin\Forward\Fixture\Packages;

use Rougin\Slytherin\Middleware\MiddlewareIntegration;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class MiddlewarePackage extends MiddlewareIntegration
{
protected $preferred = 'slytherin';
}
7 changes: 4 additions & 3 deletions tests/Forward/Fixture/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ class Router extends Slytherin

public function routes($parsed = false)
{
// Default routes ------------------
$this->get('/v1/hello', 'Hello@index');
// Default routes --------------------
$this->get('/hello', 'Hello@index');
$this->get('/hi/:name', 'Hello@name');
$this->get('/', 'Home@index');
// ---------------------------------
// -----------------------------------

return parent::routes($parsed);
}
Expand Down
14 changes: 11 additions & 3 deletions tests/Forward/Fixture/Routes/Hello.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Rougin\Slytherin\Forward\Fixture\Routes;

use Rougin\Slytherin\Forward\Fixture\Depots\TestDepot;

/**
* @package Slytherin
* @author Rougin Gutib <rougingutib@gmail.com>
Expand All @@ -11,10 +13,16 @@ class Hello extends Route
/**
* @return \Psr\Http\Message\ResponseInterface
*/
public function index()
public function index(TestDepot $test)
{
$this->response->getBody()->write('Hello world!');
return $test->text('Hello world!');
}

return $this->response;
/**
* @return \Psr\Http\Message\ResponseInterface
*/
public function name($name, TestDepot $test)
{
return $test->text("Hello, $name!");
}
}

0 comments on commit e72c612

Please sign in to comment.