Completed
Push — master ( f4be61...d5c601 )
by Nikola
08:05
created

Bootstrap   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 70
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 14 1
A setPhpSettings() 0 8 2
A registerErrorHandler() 0 8 2
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\Di\Container\Factory\ZendServiceManagerFactory;
18
use Phoundation\Di\Container\Factory\AuraDiFactory;
19
use Phoundation\Di\Container\Factory\PimpleFactory;
20
use Phoundation\ErrorHandling\RunnerInterface;
21
22
/**
23
 * @author Nikola Posa <[email protected]>
24
 */
25
class Bootstrap
26
{
27
    /**
28
     * @var callable
29
     */
30
    protected $configLoader;
31
32
    /**
33
     * @var callable
34
     */
35
    protected $diContainerFactory;
36
37
    /**
38
     * @var Config
39
     */
40
    protected $config;
41
42
    /**
43
     * @var ContainerInterface
44
     */
45
    protected $diContainer;
46
47
    /**
48
     * @var array
49
     */
50
    protected static $diContainerFactories = [
51
        'zend-service-manager' => ZendServiceManagerFactory::class,
52
        'aura-di' => AuraDiFactory::class,
53
        'pimple' => PimpleFactory::class,
54
    ];
55
56 1
    public function __construct(callable $configLoader, callable $diContainerFactory)
57
    {
58 1
        $this->configLoader = $configLoader;
59 1
        $this->diContainerFactory = $diContainerFactory;
60 1
    }
61
62 1
    public function __invoke()
63
    {
64 1
        $configLoader = $this->configLoader;
65 1
        $diContainerFactory = $this->diContainerFactory;
66
67 1
        $this->config = $configLoader();
68
69 1
        $this->diContainer = $diContainerFactory($this->config);
70
71 1
        $this->setPhpSettings();
72 1
        $this->registerErrorHandler();
73
74 1
        return $this->diContainer;
75
    }
76
77 1
    protected function setPhpSettings()
78
    {
79 1
        $phpSettings = (array) ($this->config['php_settings'] ?? []);
80
81 1
        foreach ($phpSettings as $key => $value) {
82
            ini_set($key, $value);
83
        }
84 1
    }
85
86 1
    protected function registerErrorHandler()
87
    {
88 1
        if (!$this->diContainer->has(RunnerInterface::class)) {
89 1
            return;
90
        }
91
92
        $this->diContainer->get(RunnerInterface::class)->register();
93
    }
94
}
95