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