Passed
Push — master ( 8926af...beb5fc )
by Anton
02:16
created

HttpBootloader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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));
0 ignored issues
show
Bug introduced by
It seems like $factory->make(Spiral\Http\SapiDispatcher::class) can also be of type null; however, parameter $dispatcher of Spiral\Boot\KernelInterface::addDispatcher() does only seem to accept Spiral\Boot\DispatcherInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
        $kernel->addDispatcher(/** @scrutinizer ignore-type */ $factory->make(SapiDispatcher::class));
Loading history...
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