This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathApplication.php
208 lines (185 loc) · 6.84 KB
/
Application.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);
namespace Zend\Expressive;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Expressive\Router\RouteCollector;
use Zend\HttpHandlerRunner\RequestHandlerRunner;
use Zend\Stratigility\MiddlewarePipeInterface;
use function Zend\Stratigility\path;
class Application implements MiddlewareInterface, RequestHandlerInterface
{
/**
* @var MiddlewareFactory
*/
private $factory;
/**
* @var MiddlewarePipeInterface
*/
private $pipeline;
/**
* @var RouteCollector
*/
private $routes;
/**
* @var RequestHandlerRunner
*/
private $runner;
public function __construct(
MiddlewareFactory $factory,
MiddlewarePipeInterface $pipeline,
RouteCollector $routes,
RequestHandlerRunner $runner
) {
$this->factory = $factory;
$this->pipeline = $pipeline;
$this->routes = $routes;
$this->runner = $runner;
}
/**
* Proxies to composed pipeline to handle.
* {@inheritDocs}
*/
public function handle(ServerRequestInterface $request) : ResponseInterface
{
return $this->pipeline->handle($request);
}
/**
* Proxies to composed pipeline to process.
* {@inheritDocs}
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
return $this->pipeline->process($request, $handler);
}
/**
* Run the application.
*
* Proxies to the RequestHandlerRunner::run() method.
*/
public function run() : void
{
$this->runner->run();
}
/**
* Pipe middleware to the pipeline.
*
* If two arguments are present, they are passed to pipe(), after first
* passing the second argument to the factory's prepare() method.
*
* If only one argument is presented, it is passed to the factory prepare()
* method.
*
* The resulting middleware, in both cases, is piped to the pipeline.
*
* @param string|array|callable|MiddlewareInterface|RequestHandlerInterface $middlewareOrPath
* Either the middleware to pipe, or the path to segregate the $middleware
* by, via a PathMiddlewareDecorator.
* @param null|string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware
* If present, middleware or request handler to segregate by the path
* specified in $middlewareOrPath.
*/
public function pipe($middlewareOrPath, $middleware = null) : void
{
$middleware = $middleware ?: $middlewareOrPath;
$path = $middleware === $middlewareOrPath ? '/' : $middlewareOrPath;
$middleware = $path !== '/'
? path($path, $this->factory->prepare($middleware))
: $this->factory->prepare($middleware);
$this->pipeline->pipe($middleware);
}
/**
* Add a route for the route middleware to match.
*
* @param string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware
* Middleware or request handler (or service name resolving to one of
* those types) to associate with route.
* @param null|array $methods HTTP method to accept; null indicates any.
* @param null|string $name The name of the route.
*/
public function route(string $path, $middleware, array $methods = null, string $name = null) : Router\Route
{
return $this->routes->route(
$path,
$this->factory->prepare($middleware),
$methods,
$name
);
}
/**
* @param string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware
* Middleware or request handler (or service name resolving to one of
* those types) to associate with route.
* @param null|string $name The name of the route.
*/
public function get(string $path, $middleware, string $name = null) : Router\Route
{
return $this->route($path, $middleware, ['GET'], $name);
}
/**
* @param string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware
* Middleware or request handler (or service name resolving to one of
* those types) to associate with route.
* @param null|string $name The name of the route.
*/
public function post(string $path, $middleware, $name = null) : Router\Route
{
return $this->route($path, $middleware, ['POST'], $name);
}
/**
* @param string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware
* Middleware or request handler (or service name resolving to one of
* those types) to associate with route.
* @param null|string $name The name of the route.
*/
public function put(string $path, $middleware, string $name = null) : Router\Route
{
return $this->route($path, $middleware, ['PUT'], $name);
}
/**
* @param string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware
* Middleware or request handler (or service name resolving to one of
* those types) to associate with route.
* @param null|string $name The name of the route.
*/
public function patch(string $path, $middleware, string $name = null) : Router\Route
{
return $this->route($path, $middleware, ['PATCH'], $name);
}
/**
* @param string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware
* Middleware or request handler (or service name resolving to one of
* those types) to associate with route.
* @param null|string $name The name of the route.
*/
public function delete(string $path, $middleware, string $name = null) : Router\Route
{
return $this->route($path, $middleware, ['DELETE'], $name);
}
/**
* @param string|array|callable|MiddlewareInterface|RequestHandlerInterface $middleware
* Middleware or request handler (or service name resolving to one of
* those types) to associate with route.
* @param null|string $name The name of the route.
*/
public function any(string $path, $middleware, string $name = null) : Router\Route
{
return $this->route($path, $middleware, null, $name);
}
/**
* Retrieve all directly registered routes with the application.
*
* @return Router\Route[]
*/
public function getRoutes() : array
{
return $this->routes->getRoutes();
}
}