|
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\FactoryInterface; |
|
18
|
|
|
use Phoundation\Config\Loader\GlobConfigLoader; |
|
19
|
|
|
use Phoundation\Di\Container\Factory\ZendServiceManagerFactory; |
|
20
|
|
|
use Phoundation\Di\Container\Factory\AuraDiFactory; |
|
21
|
|
|
use Phoundation\Di\Container\Factory\PimpleFactory; |
|
22
|
|
|
use Phoundation\ErrorHandling\RunnerInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Nikola Posa <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class Bootstrap |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var callable |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $configLoader; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var callable |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $diContainerFactory; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $diConfigKey; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $configServiceName; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var Config |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $config; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var ContainerInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $diContainer; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var array |
|
61
|
|
|
*/ |
|
62
|
|
|
protected static $configLoaders = [ |
|
63
|
|
|
'glob' => GlobConfigLoader::class, |
|
64
|
|
|
]; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var array |
|
68
|
|
|
*/ |
|
69
|
|
|
protected static $diContainerFactories = [ |
|
70
|
|
|
'zend-service-manager' => ZendServiceManagerFactory::class, |
|
71
|
|
|
'aura-di' => AuraDiFactory::class, |
|
72
|
|
|
'pimple' => PimpleFactory::class, |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param callable|string $configLoader |
|
77
|
|
|
* @param callable|string $diContainerFactory |
|
78
|
|
|
* @param string $diConfigKey |
|
79
|
|
|
* @param string $configServiceName |
|
80
|
|
|
*/ |
|
81
|
1 |
|
protected function __construct($configLoader, $diContainerFactory, string $diConfigKey, string $configServiceName) |
|
82
|
|
|
{ |
|
83
|
1 |
|
if (is_string($configLoader)) { |
|
84
|
1 |
|
if (array_key_exists($configLoader, static::$configLoaders)) { |
|
85
|
1 |
|
$configLoader = static::$configLoaders[$configLoader]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
$configLoader = new $configLoader(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
1 |
|
if (!is_callable($configLoader)) { |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
if (is_string($diContainerFactory)) { |
|
95
|
1 |
|
if (array_key_exists($diContainerFactory, static::$diContainerFactories)) { |
|
96
|
1 |
|
$diContainerFactory = static::$diContainerFactories[$diContainerFactory]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
$diContainerFactory = new $diContainerFactory(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
if (!is_callable($diContainerFactory)) { |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
$this->configLoader = $configLoader; |
|
106
|
1 |
|
$this->diContainerFactory = $diContainerFactory; |
|
107
|
1 |
|
$this->diConfigKey = $diConfigKey; |
|
108
|
1 |
|
$this->configServiceName = $configServiceName; |
|
109
|
1 |
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
public static function init(array $options) |
|
112
|
|
|
{ |
|
113
|
1 |
|
return new self( |
|
114
|
1 |
|
$options['config_loader'], |
|
115
|
1 |
|
$options['di_container_factory'], |
|
116
|
1 |
|
$options['di_config_key'] ?? FactoryInterface::DEFAULT_DI_CONFIG_KEY, |
|
117
|
1 |
|
$options['config_service_name'] ?? FactoryInterface::DEFAULT_CONFIG_SERVICE_NAME |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
1 |
|
public function run(array $configPaths) |
|
122
|
|
|
{ |
|
123
|
1 |
|
$configLoader = $this->configLoader; |
|
124
|
1 |
|
$diContainerFactory = $this->diContainerFactory; |
|
125
|
|
|
|
|
126
|
1 |
|
$this->config = $configLoader($configPaths); |
|
127
|
|
|
|
|
128
|
1 |
|
$this->diContainer = $diContainerFactory($this->config); |
|
129
|
|
|
|
|
130
|
1 |
|
$this->setPhpSettings(); |
|
131
|
1 |
|
$this->registerErrorHandler(); |
|
132
|
|
|
|
|
133
|
1 |
|
return $this->diContainer; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
protected function setPhpSettings() |
|
137
|
|
|
{ |
|
138
|
1 |
|
$phpSettings = (array) ($this->config['php_settings'] ?? []); |
|
139
|
|
|
|
|
140
|
1 |
|
foreach ($phpSettings as $key => $value) { |
|
141
|
|
|
ini_set($key, $value); |
|
142
|
|
|
} |
|
143
|
1 |
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
protected function registerErrorHandler() |
|
146
|
|
|
{ |
|
147
|
1 |
|
if (!$this->diContainer->has(RunnerInterface::class)) { |
|
148
|
1 |
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->diContainer->get(RunnerInterface::class)->register(); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|