diff --git a/.travis.yml b/.travis.yml index 8c71a48..84c7bac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 5.5 - 5.6 - 7.0 - 7.1 diff --git a/composer.json b/composer.json index 2f1b50f..a66bf6c 100644 --- a/composer.json +++ b/composer.json @@ -10,11 +10,13 @@ "maintenance" ], "require": { - "php": ">=5.5", - "psr/http-message": "^1.0" + "php": ">=5.6", + "psr/http-message": "^1.0", + "php-middleware/double-pass-compatibility": "^1.0", + "http-interop/http-middleware": "^0.4.1" }, "require-dev": { - "phpunit/phpunit": "^4.8.6", + "phpunit/phpunit": "^5.6 || ^6.1", "zendframework/zend-diactoros": "^1.1.3" }, "autoload": { diff --git a/src/MaintenanceMiddleware.php b/src/MaintenanceMiddleware.php index d38925d..c403de1 100644 --- a/src/MaintenanceMiddleware.php +++ b/src/MaintenanceMiddleware.php @@ -2,11 +2,19 @@ namespace PhpMiddleware\Maintenance; -use Psr\Http\Message\ResponseInterface; +use DateTime; +use DateTimeInterface; +use Interop\Http\ServerMiddleware\DelegateInterface; +use Interop\Http\ServerMiddleware\MiddlewareInterface; +use InvalidArgumentException; +use PhpMiddleware\DoublePassCompatibilityTrait; use Psr\Http\Message\ServerRequestInterface; +use Zend\Diactoros\Response; -final class MaintenanceMiddleware +final class MaintenanceMiddleware implements MiddlewareInterface { + use DoublePassCompatibilityTrait; + /** * @var string */ @@ -22,7 +30,7 @@ private function __construct() } /** - * @return \self + * @return self */ public static function create() { @@ -37,7 +45,7 @@ public static function create() public static function createWithRetryAsSeconds($seconds) { if (!is_int($seconds)) { - throw new \InvalidArgumentException('Seconds must be integer'); + throw new InvalidArgumentException('Seconds must be integer'); } $instance = new self(); $instance->retryAfter = (string) $seconds; @@ -59,25 +67,25 @@ public static function createWithRetryAsSecondsAndRefresh($seconds) } /** - * @param \DateTimeInterface $datetime + * @param DateTimeInterface $datetime * * @return self */ - public static function createWithRetryAsDateTime(\DateTimeInterface $datetime) + public static function createWithRetryAsDateTime(DateTimeInterface $datetime) { $instance = new self(); - $instance->retryAfter = $datetime->format(\DateTime::RFC2822); + $instance->retryAfter = $datetime->format(DateTime::RFC2822); return $instance; } /** - * @param \DateTimeInterface $datetime + * @param DateTimeInterface $datetime * * @return self */ - public static function createWithRetryAsDateTimeAndRefresh(\DateTimeInterface $datetime) + public static function createWithRetryAsDateTimeAndRefresh(DateTimeInterface $datetime) { $instance = self::createWithRetryAsDateTime($datetime); $diff = time() - $datetime->getTimestamp(); @@ -86,23 +94,19 @@ public static function createWithRetryAsDateTimeAndRefresh(\DateTimeInterface $d return $instance; } - /** - * @param ServerRequestInterface $request - * @param ResponseInterface $response - * @param callable $next - * - * @return ResponseInterface - */ - public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) + public function process(ServerRequestInterface $request, DelegateInterface $delegate) { + $headers = []; + if ($this->retryAfter !== '') { - $response = $response->withHeader('Retry-After', $this->retryAfter); + $headers['Retry-After'] = $this->retryAfter; if ($this->refresh > 0) { - $response = $response->withHeader('Refresh', (string) $this->refresh); + $headers['Refresh'] = (string) $this->refresh; } } - return $response->withStatus(503); + return new Response('php://memory', 503, $headers); } + } diff --git a/test/MaintenanceMiddlewareTest.php b/test/MaintenanceMiddlewareTest.php index 25930e8..6375ba4 100644 --- a/test/MaintenanceMiddlewareTest.php +++ b/test/MaintenanceMiddlewareTest.php @@ -5,11 +5,11 @@ use DateTime; use Exception; use PhpMiddleware\Maintenance\MaintenanceMiddleware; -use PHPUnit_Framework_TestCase; +use PHPUnit\Framework\TestCase; use Zend\Diactoros\Response; use Zend\Diactoros\ServerRequest; -class MaintenanceMiddlewareTest extends PHPUnit_Framework_TestCase +class MaintenanceMiddlewareTest extends TestCase { public function testWithoutRetry() {