1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bapao\Bapao; |
4
|
|
|
|
5
|
|
|
use Bapao\Bapao\Exception\InvalidMiddlewareDispatcherException; |
6
|
|
|
use Bapao\Bapao\Exception\InvalidResponseEmitterException; |
7
|
|
|
use Bapao\Bapao\Interfaces\MiddlewareDispatcherInterface; |
8
|
|
|
use Bapao\Bapao\Interfaces\ResponseEmitterInterface; |
9
|
|
|
use Interop\Container\ContainerInterface; |
10
|
|
|
use Psr\Http\Message\RequestInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Application. |
15
|
|
|
*/ |
16
|
|
|
class Application |
17
|
|
|
{ |
18
|
|
|
/** @const string */ |
19
|
|
|
const VERSION = '0.1.5'; |
20
|
|
|
|
21
|
|
|
/** @var ContainerInterface */ |
22
|
|
|
protected $container; |
23
|
|
|
|
24
|
|
|
/** @var array */ |
25
|
|
|
protected $middlewareStack = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set dependency injection container. |
29
|
|
|
* |
30
|
|
|
* @param ContainerInterface $container |
31
|
|
|
*/ |
32
|
8 |
|
public function setContainer(ContainerInterface $container) |
33
|
|
|
{ |
34
|
8 |
|
$this->container = $container; |
35
|
8 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get dependency injection container. |
39
|
|
|
* |
40
|
|
|
* @return ContainerInterface |
41
|
|
|
*/ |
42
|
5 |
|
public function getContainer() |
43
|
|
|
{ |
44
|
5 |
|
return $this->container; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get Router. |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
1 |
|
public function getRouter() |
53
|
|
|
{ |
54
|
1 |
|
return $this->container->get('router'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Add middleware to stack. |
59
|
|
|
* |
60
|
|
|
* @param mixed $middleware |
61
|
|
|
*/ |
62
|
1 |
|
public function addMiddleware($middleware) |
63
|
|
|
{ |
64
|
1 |
|
$this->middlewareStack[] = $middleware; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get middleware stack. |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
2 |
|
public function getMiddlewareStack() |
73
|
|
|
{ |
74
|
2 |
|
return $this->middlewareStack; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Run application. |
79
|
|
|
*/ |
80
|
1 |
|
public function run() |
81
|
|
|
{ |
82
|
1 |
|
$this->addMiddleware($this->container->get('router.middleware')); |
83
|
|
|
|
84
|
1 |
|
$response = $this->handleRequest($this->container->get('request')); |
85
|
|
|
|
86
|
1 |
|
$this->send($response); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Handle request via middleware. |
91
|
|
|
* |
92
|
|
|
* @param RequestInterface $request |
93
|
|
|
* @return ResponseInterface |
94
|
|
|
* @throws InvalidMiddlewareDispatcherException |
95
|
|
|
*/ |
96
|
2 |
|
public function handleRequest(RequestInterface $request) |
97
|
|
|
{ |
98
|
2 |
|
$middlewareDispatcher = $this->container->get('middleware.dispatcher'); |
99
|
|
|
|
100
|
2 |
|
if (!$middlewareDispatcher instanceof MiddlewareDispatcherInterface) { |
101
|
1 |
|
throw new InvalidMiddlewareDispatcherException( |
102
|
|
|
'Middleware dispatcher must implement '.MiddlewareDispatcherInterface::class |
103
|
1 |
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return $middlewareDispatcher->dispatch($this->middlewareStack, $request, $this->container->get('response')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Send response to client. |
111
|
|
|
* |
112
|
|
|
* @param ResponseInterface $response |
113
|
|
|
* @throws InvalidResponseEmitterException |
114
|
|
|
*/ |
115
|
2 |
|
public function send(ResponseInterface $response) |
116
|
|
|
{ |
117
|
2 |
|
$responseEmitter = $this->container->get('response.emitter'); |
118
|
|
|
|
119
|
2 |
|
if (!$responseEmitter instanceof ResponseEmitterInterface) { |
120
|
1 |
|
throw new InvalidResponseEmitterException( |
121
|
|
|
'Response emitter must implement '.ResponseEmitterInterface::class |
122
|
1 |
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
$responseEmitter->emit($response); |
126
|
1 |
|
} |
127
|
|
|
} |
128
|
|
|
|