|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/http |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\Http\Server\Middleware; |
|
11
|
|
|
|
|
12
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
15
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
16
|
|
|
use Slick\Http\Message\Response; |
|
17
|
|
|
use Slick\Http\Message\Stream\TextStream; |
|
18
|
|
|
use Slick\Http\Server\Exception\UnexpectedValueException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Callable Middleware |
|
22
|
|
|
* |
|
23
|
|
|
* @package Slick\Http\Server\Middleware |
|
24
|
|
|
*/ |
|
25
|
|
|
class CallableMiddleware implements MiddlewareInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var callable |
|
29
|
|
|
*/ |
|
30
|
|
|
private $callable; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Creates a callable Middleware |
|
34
|
|
|
* |
|
35
|
|
|
* @param callable $callable |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(callable $callable) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->callable = $callable; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param callable $callable |
|
44
|
|
|
* @param array $arguments |
|
45
|
|
|
* |
|
46
|
|
|
* @return ResponseInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function execute(callable $callable, array $arguments) |
|
49
|
|
|
{ |
|
50
|
|
|
$return = call_user_func_array($callable, $arguments); |
|
51
|
|
|
|
|
52
|
|
|
if ($return instanceof ResponseInterface) { |
|
53
|
|
|
return $return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$canBeUsedAsText = is_null($return) |
|
57
|
|
|
|| is_scalar($return) |
|
58
|
|
|
|| (is_object($return) && method_exists($return, '__toString')); |
|
59
|
|
|
|
|
60
|
|
|
if (! $canBeUsedAsText) { |
|
61
|
|
|
throw new UnexpectedValueException( |
|
62
|
|
|
'The value returned must be scalar or an object with __toString method' |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return new Response(200, new TextStream((string) $return)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Process an incoming server request and return a response, optionally delegating |
|
71
|
|
|
* response creation to an handler. |
|
72
|
|
|
* |
|
73
|
|
|
* @param ServerRequestInterface $request |
|
74
|
|
|
* @param RequestHandlerInterface $handler |
|
75
|
|
|
* |
|
76
|
|
|
* @return ResponseInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
public function process( |
|
79
|
|
|
ServerRequestInterface $request, |
|
80
|
|
|
RequestHandlerInterface $handler |
|
81
|
|
|
): ResponseInterface { |
|
82
|
|
|
$response = self::execute($this->callable, [$request, $handler]); |
|
83
|
|
|
if (!$response instanceof ResponseInterface) { |
|
|
|
|
|
|
84
|
|
|
throw new UnexpectedValueException( |
|
85
|
|
|
sprintf('The middleware must return an instance of %s', ResponseInterface::class) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
return $response; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|