Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Added MiddlewarePipeInterface as abstraction layer for MiddlewarePipe #146

Merged
merged 6 commits into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/book/v3/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ internal logic.
- `Next::handle()`: the method now provides a return typehint of
`Psr\Http\Message\ResponseInterface`.

- `MiddlewarePipe` class is marked now as `final` and implements new interface
`MiddlewarePipeInterface.`

- `MiddlewarePipe::pipe()`: reduces the number of arguments to one, which now
typehints against `Psr\Http\Server\MiddlewareInterface`. This means the method
can no longer be used to segregate middleware by path. If you want to do that,
Expand All @@ -88,6 +91,11 @@ internal logic.

### Class additions

- `Zend\Stratigility\MiddlewarePipeInterface` extends
`Psr\Http\Server\MiddlewareInterface` and `Psr\Http\Server\RequestHandlerInterface`
and define method `pipe(Psr\Http\Server\MiddlewareInterface $middleware) : void`.
It is implemented by `MiddlewarePipe`, because this is marked as `final` class.

- `Zend\Stratigility\Middleware\HostMiddlewareDecorator` allows you to segregate
middleware by a static host name. This allows executing middleware only
if a particular host matches.
Expand Down
2 changes: 1 addition & 1 deletion src/MiddlewarePipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*
* @see https://github.com/sencha/connect
*/
final class MiddlewarePipe implements MiddlewareInterface, RequestHandlerInterface
final class MiddlewarePipe implements MiddlewarePipeInterface
{
/**
* @var SplQueue
Expand Down
17 changes: 17 additions & 0 deletions src/MiddlewarePipeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* @see https://github.com/zendframework/zend-stratigility for the canonical source repository
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-stratigility/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);

namespace Zend\Stratigility;

use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

interface MiddlewarePipeInterface extends MiddlewareInterface, RequestHandlerInterface
{
public function pipe(MiddlewareInterface $middleware) : void;
}
31 changes: 31 additions & 0 deletions test/MiddlewarePipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use ReflectionClass;
use ReflectionMethod;
use ReflectionObject;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest as Request;
use Zend\Stratigility\Exception;
use Zend\Stratigility\MiddlewarePipe;
use Zend\Stratigility\MiddlewarePipeInterface;

class MiddlewarePipeTest extends TestCase
{
Expand Down Expand Up @@ -179,4 +183,31 @@ public function testHandleProcessesEnqueuedMiddleware()

$this->assertSame($response, $pipeline->handle($this->request));
}

public function testMiddlewarePipeOnlyImplementsMiddlewarePipeInterfaceApi()
{
$pipeline = new MiddlewarePipe();

$r = new ReflectionObject($pipeline);
$methods = $r->getMethods(ReflectionMethod::IS_PUBLIC);
$actual = [];
foreach ($methods as $method) {
if (strpos($method->getName(), '__') !== 0) {
$actual[] = $method->getName();
}
}
sort($actual);

$interfaceReflection = new ReflectionClass(MiddlewarePipeInterface::class);
$interfaceMethods = $interfaceReflection->getMethods(ReflectionMethod::IS_PUBLIC);
$expected = [];
foreach ($interfaceMethods as $method) {
$expected[] = $method->getName();
}
sort($expected);

self::assertTrue($r->isFinal());
self::assertEquals($expected, $actual);
self::assertInstanceOf(MiddlewarePipeInterface::class, $pipeline);
}
}