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
|
|
|
|