-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathApplication.php
More file actions
83 lines (69 loc) · 2.64 KB
/
Copy pathApplication.php
File metadata and controls
83 lines (69 loc) · 2.64 KB
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
<?php
namespace Framework\Http;
use Framework\Http\Pipeline\MiddlewareResolver;
use Framework\Http\Router\RouteData;
use Framework\Http\Router\Router;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Stratigility\Middleware\PathMiddlewareDecorator;
use Zend\Stratigility\MiddlewarePipe;
class Application implements MiddlewareInterface, RequestHandlerInterface
{
private $resolver;
private $router;
private $default;
private $pipeline;
public function __construct(MiddlewareResolver $resolver, Router $router, RequestHandlerInterface $default)
{
$this->resolver = $resolver;
$this->router = $router;
$this->pipeline = new MiddlewarePipe();
$this->default = $default;
}
public function pipe($path, $middleware = null): void
{
if ($middleware === null) {
$this->pipeline->pipe($this->resolver->resolve($path));
} else {
$this->pipeline->pipe(new PathMiddlewareDecorator($path, $this->resolver->resolve($middleware)));
}
}
private function route($name, $path, $handler, array $methods, array $options = []): void
{
$this->router->addRoute(new RouteData($name, $path, $handler, $methods, $options));
}
public function any($name, $path, $handler, array $options = []): void
{
$this->route($name, $path, $handler, ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'], $options);
}
public function get($name, $path, $handler, array $options = []): void
{
$this->route($name, $path, $handler, ['GET'], $options);
}
public function post($name, $path, $handler, array $options = []): void
{
$this->route($name, $path, $handler, ['POST'], $options);
}
public function put($name, $path, $handler, array $options = []): void
{
$this->route($name, $path, $handler, ['PUT'], $options);
}
public function patch($name, $path, $handler, array $options = []): void
{
$this->route($name, $path, $handler, ['PATCH'], $options);
}
public function delete($name, $path, $handler, array $options = []): void
{
$this->route($name, $path, $handler, ['DELETE'], $options);
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
return $this->pipeline->process($request, $this->default);
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return $this->pipeline->process($request, $handler);
}
}