|
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\Di\Container\Factory; |
|
14
|
|
|
|
|
15
|
|
|
use Phoundation\Config\Config; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Nikola Posa <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class AbstractFactory implements DiContainerFactoryInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $options; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Config |
|
29
|
|
|
*/ |
|
30
|
|
|
private $config; |
|
31
|
|
|
|
|
32
|
22 |
|
public function __construct(array $options = []) |
|
33
|
|
|
{ |
|
34
|
22 |
|
$this->options = array_merge([ |
|
35
|
22 |
|
'di_config_key' => self::DEFAULT_DI_CONFIG_KEY, |
|
36
|
22 |
|
'config_service_name' => self::DEFAULT_CONFIG_SERVICE_NAME, |
|
37
|
|
|
], $options); |
|
38
|
22 |
|
} |
|
39
|
|
|
|
|
40
|
22 |
|
public function __invoke(Config $config) |
|
41
|
|
|
{ |
|
42
|
22 |
|
$this->config = $config; |
|
43
|
|
|
|
|
44
|
22 |
|
$container = $this->createContainer(); |
|
45
|
|
|
|
|
46
|
22 |
|
$this->configure($container); |
|
47
|
|
|
|
|
48
|
22 |
|
return $container; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
abstract protected function createContainer(); |
|
52
|
|
|
|
|
53
|
|
|
abstract protected function configure($container); |
|
54
|
|
|
|
|
55
|
22 |
|
final protected function getConfig() |
|
56
|
|
|
{ |
|
57
|
22 |
|
return $this->config; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
22 |
|
final protected function getConfigServiceName() |
|
61
|
|
|
{ |
|
62
|
22 |
|
return $this->options['config_service_name']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
22 |
|
final protected function getDiConfig() : array |
|
66
|
|
|
{ |
|
67
|
22 |
|
$diConfigKey = $this->options['di_config_key']; |
|
68
|
|
|
|
|
69
|
22 |
|
if (empty($this->config[$diConfigKey]) || !is_array($this->config[$diConfigKey])) { |
|
70
|
9 |
|
return []; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
13 |
|
return $this->config[$diConfigKey]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
12 |
|
final protected function getDiConfigGroup(string $group) : array |
|
77
|
|
|
{ |
|
78
|
12 |
|
$diConfig = $this->getDiConfig(); |
|
79
|
|
|
|
|
80
|
12 |
|
if (empty($diConfig[$group]) || !is_array($diConfig[$group])) { |
|
81
|
12 |
|
return []; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
8 |
|
return $diConfig[$group]; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|