Passed
Push — master ( 5e186e...dbb2e0 )
by Anton
02:56
created

HttpBootloader::whitelistCookie()   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\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));
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

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