1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the PHP App Foundation 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 Foundation\Bootstrap; |
14
|
|
|
|
15
|
|
|
use Foundation\Config\Config; |
16
|
|
|
use Psr\Container\ContainerInterface; |
17
|
|
|
use Foundation\Di\Container\Factory\ZendServiceManagerFactory; |
18
|
|
|
use Foundation\Di\Container\Factory\AuraDiFactory; |
19
|
|
|
use Foundation\Di\Container\Factory\PimpleFactory; |
20
|
|
|
use Foundation\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
|
|
|
public function __construct(callable $configLoader, callable $diContainerFactory) |
57
|
|
|
{ |
58
|
|
|
$this->configLoader = $configLoader; |
59
|
|
|
$this->diContainerFactory = $diContainerFactory; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function __invoke() |
63
|
|
|
{ |
64
|
|
|
$configLoader = $this->configLoader; |
65
|
|
|
$diContainerFactory = $this->diContainerFactory; |
66
|
|
|
|
67
|
|
|
$this->config = $configLoader(); |
68
|
|
|
|
69
|
|
|
$this->diContainer = $diContainerFactory($this->config); |
70
|
|
|
|
71
|
|
|
$this->setPhpSettings(); |
72
|
|
|
$this->registerErrorHandler(); |
73
|
|
|
|
74
|
|
|
return $this->diContainer; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function setPhpSettings() |
78
|
|
|
{ |
79
|
|
|
$phpSettings = (array) $this->config['php_settings'] ?? []; |
80
|
|
|
|
81
|
|
|
foreach ($phpSettings as $key => $value) { |
82
|
|
|
ini_set($key, $value); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
protected function registerErrorHandler() |
87
|
|
|
{ |
88
|
|
|
if (!$this->diContainer->has(RunnerInterface::class)) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->diContainer->get(RunnerInterface::class)->register(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|