Completed
Push — master ( 45dd31...d94b70 )
by Nikola
02:35
created

Bootstrap::buildDiContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Phoundation package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Phoundation\Bootstrap;
14
15
use Phoundation\Config\Config;
16
use Psr\Container\ContainerInterface;
17
use Phoundation\Config\ConfigFactory;
18
use Phoundation\ErrorHandling\RunnerInterface;
19
20
/**
21
 * @author Nikola Posa <[email protected]>
22
 */
23
class Bootstrap
24
{
25
    /**
26
     * @var Options
27
     */
28
    protected $options;
29
30
    /**
31
     * @var Config
32
     */
33
    protected $config;
34
35
    /**
36
     * @var ContainerInterface
37
     */
38
    protected $diContainer;
39
40 8
    public function __construct(array $options)
41
    {
42 8
        $this->options = Options::fromArray($options);
43 7
    }
44
45 7
    public function __invoke()
46
    {
47 7
        $this->buildConfig();
48 7
        $this->buildDiContainer();
49
50 7
        $this->setPhpSettings();
51 7
        $this->registerErrorHandler();
52
53 7
        return $this->diContainer;
54
    }
55
56 7
    protected function buildConfig()
57
    {
58 7
        $config = Config::fromArray($this->options->getConfig());
59
60 7
        foreach ($this->options->getConfigPaths() as $path) {
61 1
            $config->merge(ConfigFactory::createFromPath($path));
62
        }
63
64 7
        foreach ($this->options->getConfigGlobPaths() as $globPath) {
65 1
            $config->merge(ConfigFactory::createFromGlobPath($globPath));
66
        }
67
68 7
        $this->config = $config;
69 7
    }
70
71 7
    protected function buildDiContainer()
72
    {
73 7
        $diContainerFactory = $this->options->getDiContainerFactory();
74
75 7
        $this->diContainer = $diContainerFactory($this->config, $this->options->getDiConfigKey(), $this->options->getConfigServiceName());
76 7
    }
77
78 7
    protected function setPhpSettings()
79
    {
80 7
        $phpSettings = (array) ($this->config['php_settings'] ?? []);
81
82 7
        foreach ($phpSettings as $key => $value) {
83 1
            ini_set($key, $value);
84
        }
85 7
    }
86
87 7
    protected function registerErrorHandler()
88
    {
89 7
        if (!$this->diContainer->has(RunnerInterface::class)) {
90 6
            return;
91
        }
92
93 1
        $this->diContainer->get(RunnerInterface::class)->register();
94 1
    }
95
}
96