1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Factory; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Yiisoft\Factory\Definition\DefinitionValidator; |
9
|
|
|
use Yiisoft\Factory\Exception\InvalidConfigException; |
10
|
|
|
use Yiisoft\Factory\Exception\NotFoundException; |
11
|
|
|
use Yiisoft\Factory\Exception\NotInstantiableException; |
12
|
|
|
|
13
|
|
|
class Factory implements FactoryInterface |
14
|
|
|
{ |
15
|
|
|
private DependencyResolver $container; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool $validate Validate definitions when set |
19
|
|
|
*/ |
20
|
|
|
private bool $validate; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Factory constructor. |
24
|
|
|
* |
25
|
|
|
* @psalm-param array<string, mixed> $definitions |
26
|
|
|
* |
27
|
|
|
* @throws InvalidConfigException |
28
|
|
|
*/ |
29
|
39 |
|
public function __construct( |
30
|
|
|
ContainerInterface $container = null, |
31
|
|
|
array $definitions = [], |
32
|
|
|
bool $validate = true |
33
|
|
|
) { |
34
|
39 |
|
$this->container = new DependencyResolver($container); |
35
|
39 |
|
$this->validate = $validate; |
36
|
39 |
|
$this->setDefaultDefinitions(); |
37
|
39 |
|
$this->setMultiple($definitions); |
38
|
39 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param mixed $config |
42
|
|
|
* |
43
|
|
|
* @throws InvalidConfigException |
44
|
|
|
* @throws NotFoundException |
45
|
|
|
* @throws NotInstantiableException |
46
|
|
|
* |
47
|
|
|
* @return mixed|object |
48
|
|
|
*/ |
49
|
38 |
|
public function create($config) |
50
|
|
|
{ |
51
|
38 |
|
if ($this->validate) { |
52
|
36 |
|
DefinitionValidator::validate($config); |
53
|
|
|
} |
54
|
|
|
|
55
|
36 |
|
return $this->container |
56
|
36 |
|
->createDefinition($config) |
57
|
34 |
|
->resolve($this->container); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets a definition to the factory. |
62
|
|
|
* |
63
|
|
|
* @param mixed $definition |
64
|
|
|
* |
65
|
|
|
* @throws InvalidConfigException |
66
|
|
|
*/ |
67
|
39 |
|
public function set(string $id, $definition): void |
68
|
|
|
{ |
69
|
39 |
|
if ($this->validate) { |
70
|
37 |
|
DefinitionValidator::validate($definition, $id); |
71
|
|
|
} |
72
|
|
|
|
73
|
39 |
|
$this->container->setFactoryDefinition($id, $definition); |
74
|
39 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Sets multiple definitions at once. |
78
|
|
|
* |
79
|
|
|
* @param array $definitions definitions indexed by their ids |
80
|
|
|
* |
81
|
|
|
* @psalm-param array<string, mixed> $definitions |
82
|
|
|
* |
83
|
|
|
* @throws InvalidConfigException |
84
|
|
|
*/ |
85
|
39 |
|
public function setMultiple(array $definitions): void |
86
|
|
|
{ |
87
|
|
|
/** @var mixed $definition */ |
88
|
39 |
|
foreach ($definitions as $id => $definition) { |
89
|
16 |
|
$this->set($id, $definition); |
90
|
|
|
} |
91
|
39 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @throws InvalidConfigException |
95
|
|
|
*/ |
96
|
39 |
|
private function setDefaultDefinitions(): void |
97
|
|
|
{ |
98
|
39 |
|
$this->set(ContainerInterface::class, $this->container); |
99
|
39 |
|
} |
100
|
|
|
} |
101
|
|
|
|