This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
MiddlewarePipe.php
176 lines (158 loc) · 5.01 KB
/
MiddlewarePipe.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @see http://github.com/zendframework/zend-stratigility for the canonical source repository
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-stratigility/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Stratigility;
use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use SplQueue;
use Zend\Diactoros\Uri;
/**
* Pipe middleware like unix pipes.
*
* This class implements a pipe-line of middleware, which can be attached using
* the `pipe()` method, and is itself middleware.
*
* The request and response objects are decorated using the Zend\Stratigility\Http
* variants in this package, ensuring that the request may store arbitrary
* properties, and the response exposes the convenience `write()`, `end()`, and
* `isComplete()` methods.
*
* It creates an instance of `Next` internally, invoking it with the provided
* request and response instances; if no `$out` argument is provided, it will
* create a `FinalHandler` instance and pass that to `Next` as well.
*
* Inspired by Sencha Connect.
*
* @see https://github.com/sencha/connect
*/
class MiddlewarePipe implements MiddlewareInterface
{
/**
* @var SplQueue
*/
protected $pipeline;
/**
* Constructor
*
* Initializes the queue.
*/
public function __construct()
{
$this->pipeline = new SplQueue();
}
/**
* Handle a request
*
* Takes the pipeline, creates a Next handler, and delegates to the
* Next handler.
*
* If $out is a callable, it is used as the "final handler" when
* $next has exhausted the pipeline; otherwise, a FinalHandler instance
* is created and passed to $next during initialization.
*
* @param Request $request
* @param Response $response
* @param callable $out
* @return Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
$request = $this->decorateRequest($request);
$response = $this->decorateResponse($response);
$done = $out ?: new FinalHandler();
$next = new Next($this->pipeline, $done);
$result = $next($request, $response);
return ($result instanceof Response ? $result : $response);
}
/**
* Attach middleware to the pipeline.
*
* Each middleware can be associated with a particular path; if that
* path is matched when that middleware is invoked, it will be processed;
* otherwise it is skipped.
*
* No path means it should be executed every request cycle.
*
* A handler CAN implement MiddlewareInterface, but MUST be callable.
*
* Handlers with arity >= 4 or those implementing ErrorMiddlewareInterface
* are considered error handlers, and will be executed when a handler calls
* $next with an error or raises an exception.
*
* @see MiddlewareInterface
* @see ErrorMiddlewareInterface
* @see Next
* @param string|callable|object $path Either a URI path prefix, or middleware.
* @param null|callable|object $middleware Middleware
* @return self
*/
public function pipe($path, $middleware = null)
{
if (null === $middleware && is_callable($path)) {
$middleware = $path;
$path = '/';
}
// Ensure we have a valid handler
if (! is_callable($middleware)) {
throw new InvalidArgumentException('Middleware must be callable');
}
$this->pipeline->enqueue(new Route(
$this->normalizePipePath($path),
$middleware
));
// @todo Trigger event here with route details?
return $this;
}
/**
* Normalize a path used when defining a pipe
*
* Strips trailing slashes, and prepends a slash.
*
* @param string $path
* @return string
*/
private function normalizePipePath($path)
{
// Prepend slash if missing
if (empty($path) || $path[0] !== '/') {
$path = '/' . $path;
}
// Trim trailing slash if present
if (strlen($path) > 1 && '/' === substr($path, -1)) {
$path = rtrim($path, '/');
}
return $path;
}
/**
* Decorate the Request instance
*
* @param Request $request
* @return Http\Request
*/
private function decorateRequest(Request $request)
{
if ($request instanceof Http\Request) {
return $request;
}
return new Http\Request($request);
}
/**
* Decorate the Response instance
*
* @param Response $response
* @return Http\Response
*/
private function decorateResponse(Response $response)
{
if ($response instanceof Http\Response) {
return $response;
}
return new Http\Response($response);
}
}