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 FactoryInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Config |
24
|
|
|
*/ |
25
|
|
|
private $config; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $diConfigKey; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $configServiceName; |
36
|
|
|
|
37
|
13 |
|
public function __invoke(Config $config, string $diConfigKey = self::DEFAULT_DI_CONFIG_KEY, string $configServiceName = self::DEFAULT_CONFIG_SERVICE_NAME) |
38
|
|
|
{ |
39
|
13 |
|
$this->config = $config; |
40
|
13 |
|
$this->diConfigKey = $diConfigKey; |
41
|
13 |
|
$this->configServiceName = $configServiceName; |
42
|
|
|
|
43
|
13 |
|
$container = $this->createContainer(); |
44
|
|
|
|
45
|
13 |
|
$this->configure($container); |
46
|
|
|
|
47
|
13 |
|
return $container; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
abstract protected function createContainer(); |
51
|
|
|
|
52
|
|
|
abstract protected function configure($container); |
53
|
|
|
|
54
|
13 |
|
final protected function getConfig() |
55
|
|
|
{ |
56
|
13 |
|
return $this->config; |
57
|
|
|
} |
58
|
|
|
|
59
|
13 |
|
final protected function getConfigServiceName() |
60
|
|
|
{ |
61
|
13 |
|
return $this->configServiceName; |
62
|
|
|
} |
63
|
|
|
|
64
|
13 |
|
final protected function getDiConfig() : array |
65
|
|
|
{ |
66
|
13 |
|
if (empty($this->config[$this->diConfigKey]) || !is_array($this->config[$this->diConfigKey])) { |
67
|
7 |
|
return []; |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
return $this->config[$this->diConfigKey]; |
71
|
|
|
} |
72
|
|
|
|
73
|
8 |
|
final protected function getDiConfigGroup(string $group) : array |
74
|
|
|
{ |
75
|
8 |
|
$diConfig = $this->getDiConfig(); |
76
|
|
|
|
77
|
8 |
|
if (empty($diConfig[$group]) || !is_array($diConfig[$group])) { |
78
|
8 |
|
return []; |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
return $diConfig[$group]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|