Skip to content

Commit 48440a7

Browse files
authored
Merge pull request #2 from php-middleware/psr-15
PSR-15
2 parents 5ccd1eb + 9f6ee61 commit 48440a7

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: php
22

33
php:
4-
- 5.5
54
- 5.6
65
- 7.0
76
- 7.1

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
"maintenance"
1111
],
1212
"require": {
13-
"php": ">=5.5",
14-
"psr/http-message": "^1.0"
13+
"php": ">=5.6",
14+
"psr/http-message": "^1.0",
15+
"php-middleware/double-pass-compatibility": "^1.0",
16+
"http-interop/http-middleware": "^0.4.1"
1517
},
1618
"require-dev": {
17-
"phpunit/phpunit": "^4.8.6",
19+
"phpunit/phpunit": "^5.6 || ^6.1",
1820
"zendframework/zend-diactoros": "^1.1.3"
1921
},
2022
"autoload": {

src/MaintenanceMiddleware.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22

33
namespace PhpMiddleware\Maintenance;
44

5-
use Psr\Http\Message\ResponseInterface;
5+
use DateTime;
6+
use DateTimeInterface;
7+
use Interop\Http\ServerMiddleware\DelegateInterface;
8+
use Interop\Http\ServerMiddleware\MiddlewareInterface;
9+
use InvalidArgumentException;
10+
use PhpMiddleware\DoublePassCompatibilityTrait;
611
use Psr\Http\Message\ServerRequestInterface;
12+
use Zend\Diactoros\Response;
713

8-
final class MaintenanceMiddleware
14+
final class MaintenanceMiddleware implements MiddlewareInterface
915
{
16+
use DoublePassCompatibilityTrait;
17+
1018
/**
1119
* @var string
1220
*/
@@ -22,7 +30,7 @@ private function __construct()
2230
}
2331

2432
/**
25-
* @return \self
33+
* @return self
2634
*/
2735
public static function create()
2836
{
@@ -37,7 +45,7 @@ public static function create()
3745
public static function createWithRetryAsSeconds($seconds)
3846
{
3947
if (!is_int($seconds)) {
40-
throw new \InvalidArgumentException('Seconds must be integer');
48+
throw new InvalidArgumentException('Seconds must be integer');
4149
}
4250
$instance = new self();
4351
$instance->retryAfter = (string) $seconds;
@@ -59,25 +67,25 @@ public static function createWithRetryAsSecondsAndRefresh($seconds)
5967
}
6068

6169
/**
62-
* @param \DateTimeInterface $datetime
70+
* @param DateTimeInterface $datetime
6371
*
6472
* @return self
6573
*/
66-
public static function createWithRetryAsDateTime(\DateTimeInterface $datetime)
74+
public static function createWithRetryAsDateTime(DateTimeInterface $datetime)
6775
{
6876
$instance = new self();
6977

70-
$instance->retryAfter = $datetime->format(\DateTime::RFC2822);
78+
$instance->retryAfter = $datetime->format(DateTime::RFC2822);
7179

7280
return $instance;
7381
}
7482

7583
/**
76-
* @param \DateTimeInterface $datetime
84+
* @param DateTimeInterface $datetime
7785
*
7886
* @return self
7987
*/
80-
public static function createWithRetryAsDateTimeAndRefresh(\DateTimeInterface $datetime)
88+
public static function createWithRetryAsDateTimeAndRefresh(DateTimeInterface $datetime)
8189
{
8290
$instance = self::createWithRetryAsDateTime($datetime);
8391
$diff = time() - $datetime->getTimestamp();
@@ -86,23 +94,19 @@ public static function createWithRetryAsDateTimeAndRefresh(\DateTimeInterface $d
8694
return $instance;
8795
}
8896

89-
/**
90-
* @param ServerRequestInterface $request
91-
* @param ResponseInterface $response
92-
* @param callable $next
93-
*
94-
* @return ResponseInterface
95-
*/
96-
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
97+
public function process(ServerRequestInterface $request, DelegateInterface $delegate)
9798
{
99+
$headers = [];
100+
98101
if ($this->retryAfter !== '') {
99-
$response = $response->withHeader('Retry-After', $this->retryAfter);
102+
$headers['Retry-After'] = $this->retryAfter;
100103

101104
if ($this->refresh > 0) {
102-
$response = $response->withHeader('Refresh', (string) $this->refresh);
105+
$headers['Refresh'] = (string) $this->refresh;
103106
}
104107
}
105108

106-
return $response->withStatus(503);
109+
return new Response('php://memory', 503, $headers);
107110
}
111+
108112
}

test/MaintenanceMiddlewareTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
use DateTime;
66
use Exception;
77
use PhpMiddleware\Maintenance\MaintenanceMiddleware;
8-
use PHPUnit_Framework_TestCase;
8+
use PHPUnit\Framework\TestCase;
99
use Zend\Diactoros\Response;
1010
use Zend\Diactoros\ServerRequest;
1111

12-
class MaintenanceMiddlewareTest extends PHPUnit_Framework_TestCase
12+
class MaintenanceMiddlewareTest extends TestCase
1313
{
1414
public function testWithoutRetry()
1515
{

0 commit comments

Comments
 (0)