|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Spiral\Bootloader\Http; |
|
11
|
|
|
|
|
12
|
|
|
use Psr\Container\ContainerInterface; |
|
13
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
14
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
15
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
|
16
|
|
|
use Spiral\Boot\KernelInterface; |
|
17
|
|
|
use Spiral\Bootloader\ServerBootloader; |
|
18
|
|
|
use Spiral\Config\ConfiguratorInterface; |
|
19
|
|
|
use Spiral\Config\Patch\Append; |
|
20
|
|
|
use Spiral\Core\Container\SingletonInterface; |
|
21
|
|
|
use Spiral\Core\FactoryInterface; |
|
22
|
|
|
use Spiral\Http\Config\HttpConfig; |
|
23
|
|
|
use Spiral\Http\Emitter\SapiEmitter; |
|
24
|
|
|
use Spiral\Http\EmitterInterface; |
|
25
|
|
|
use Spiral\Http\Http; |
|
26
|
|
|
use Spiral\Http\Pipeline; |
|
27
|
|
|
use Spiral\Http\RrDispacher; |
|
28
|
|
|
use Spiral\Http\SapiDispatcher; |
|
29
|
|
|
use Spiral\RoadRunner\PSR7Client; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Configures Http dispatcher in SAPI and RoadRunner modes (if available). |
|
33
|
|
|
*/ |
|
34
|
|
|
final class HttpBootloader extends Bootloader implements SingletonInterface |
|
35
|
|
|
{ |
|
36
|
|
|
const DEPENDENCIES = [ |
|
37
|
|
|
ServerBootloader::class |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
const SINGLETONS = [ |
|
41
|
|
|
Http::class => [self::class, 'httpCore'], |
|
42
|
|
|
EmitterInterface::class => SapiEmitter::class, |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** @var ConfiguratorInterface */ |
|
46
|
|
|
private $config; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param ConfiguratorInterface $config |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(ConfiguratorInterface $config) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->config = $config; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param KernelInterface $kernel |
|
58
|
|
|
* @param FactoryInterface $factory |
|
59
|
|
|
*/ |
|
60
|
|
|
public function boot(KernelInterface $kernel, FactoryInterface $factory) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->config->setDefaults('http', [ |
|
63
|
|
|
'basePath' => '/', |
|
64
|
|
|
'headers' => [ |
|
65
|
|
|
'Content-Type' => 'text/html; charset=UTF-8' |
|
66
|
|
|
], |
|
67
|
|
|
'middleware' => [], |
|
68
|
|
|
]); |
|
69
|
|
|
|
|
70
|
|
|
$kernel->addDispatcher($factory->make(SapiDispatcher::class)); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
if (class_exists(PSR7Client::class)) { |
|
73
|
|
|
$kernel->addDispatcher($factory->make(RrDispacher::class)); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param HttpConfig $config |
|
79
|
|
|
* @param Pipeline $pipeline |
|
80
|
|
|
* @param RequestHandlerInterface $handler |
|
81
|
|
|
* @param ResponseFactoryInterface $responseFactory |
|
82
|
|
|
* @param ContainerInterface $container |
|
83
|
|
|
* @return Http |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function httpCore( |
|
86
|
|
|
HttpConfig $config, |
|
87
|
|
|
Pipeline $pipeline, |
|
88
|
|
|
RequestHandlerInterface $handler, |
|
89
|
|
|
ResponseFactoryInterface $responseFactory, |
|
90
|
|
|
ContainerInterface $container |
|
91
|
|
|
): Http { |
|
92
|
|
|
$core = new Http($config, $pipeline, $responseFactory, $container); |
|
93
|
|
|
$core->setHandler($handler); |
|
94
|
|
|
|
|
95
|
|
|
return $core; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Register new http middleware. |
|
100
|
|
|
* |
|
101
|
|
|
* @param mixed $middleware |
|
102
|
|
|
*/ |
|
103
|
|
|
public function addMiddleware($middleware) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->config->modify('http', new Append('middleware', null, $middleware)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|