1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Simply\Application; |
4
|
|
|
|
5
|
|
|
use Simply\Application\Handler\MiddlewareHandler; |
6
|
|
|
use Simply\Application\HttpFactory\HttpFactoryInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The core application that runs the main middleware stack. |
10
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
11
|
|
|
* @copyright Copyright (c) 2018 Riikka Kalliomäki |
12
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
class Application |
15
|
|
|
{ |
16
|
|
|
/** @var HttpFactoryInterface The factory used to generate the request from globals */ |
17
|
|
|
private $httpFactory; |
18
|
|
|
|
19
|
|
|
/** @var MiddlewareHandler The middleware stack that is run for every request */ |
20
|
|
|
private $middlewareStack; |
21
|
|
|
|
22
|
|
|
/** @var HttpClient The http client the application uses to communicate to the browser */ |
23
|
|
|
private $httpClient; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Application constructor. |
27
|
|
|
* @param HttpFactoryInterface $factory The factory used to generate the request from globals |
28
|
|
|
* @param MiddlewareHandler $stack The middleware stack that is run for every request |
29
|
|
|
* @param HttpClient $client The http client the application uses to communicate to the browser |
30
|
|
|
*/ |
31
|
12 |
|
public function __construct(HttpFactoryInterface $factory, MiddlewareHandler $stack, HttpClient $client) |
32
|
|
|
{ |
33
|
12 |
|
$this->httpFactory = $factory; |
34
|
12 |
|
$this->middlewareStack = $stack; |
35
|
12 |
|
$this->httpClient = $client; |
36
|
12 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Runs the application middleware stack for the request and sends the response. |
40
|
|
|
*/ |
41
|
12 |
|
public function run(): void |
42
|
|
|
{ |
43
|
12 |
|
$request = $this->httpFactory->createServerRequestFromGlobals(); |
44
|
12 |
|
$response = $this->middlewareStack->handle($request); |
45
|
|
|
|
46
|
12 |
|
if ($request->getMethod() === 'HEAD') { |
47
|
1 |
|
$this->httpClient->omitBody(); |
48
|
|
|
} |
49
|
|
|
|
50
|
12 |
|
$this->httpClient->send($response); |
51
|
12 |
|
} |
52
|
|
|
} |
53
|
|
|
|