|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Prozorov\DataVerification\Factories; |
|
4
|
|
|
|
|
5
|
|
|
use Prozorov\DataVerification\Exceptions\{ConfigurationException, FactoryException}; |
|
6
|
|
|
use Webmozart\Assert\Assert; |
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
|
|
9
|
|
|
abstract class AbstractFactory |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string $allowedType |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $allowedType; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array $config |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $config = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array $resolved |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $resolved = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool $singletons |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $singletons = true; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ContainerInterface $container |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $container; |
|
35
|
|
|
|
|
36
|
17 |
|
public function __construct(array $config, ContainerInterface $container = null) |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
17 |
|
$this->config = $config; |
|
39
|
17 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* make. |
|
43
|
|
|
* |
|
44
|
|
|
* @access public |
|
45
|
|
|
* @param string $code |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
5 |
|
public function make(string $code) |
|
49
|
|
|
{ |
|
50
|
5 |
|
if (! empty($this->resolved[$code])) { |
|
51
|
1 |
|
return $this->resolved[$code]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
if (empty($this->config)) { |
|
55
|
|
|
throw new ConfigurationException('Фабрика не инициализирована'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
5 |
|
if (empty($this->config[$code])) { |
|
59
|
1 |
|
throw new FactoryException('Отсутствуют инструкции по созданию объекта ' . $code); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
if (is_callable($this->config[$code])) { |
|
63
|
|
|
$resolved = $this->config[$code](); |
|
64
|
4 |
|
} elseif (is_string($code)) { |
|
|
|
|
|
|
65
|
4 |
|
$resolved = $this->getResolvedFromString($code); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
Assert::isInstanceOf($resolved, $this->allowedType); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
4 |
|
if (! $this->singletons) { |
|
71
|
2 |
|
return $resolved; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
$this->resolved[$code] = $resolved; |
|
75
|
|
|
|
|
76
|
2 |
|
return $this->resolved[$code]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* entityExists. |
|
81
|
|
|
* |
|
82
|
|
|
* @access public |
|
83
|
|
|
* @param string $code |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
4 |
|
public function entityExists(string $code): bool |
|
87
|
|
|
{ |
|
88
|
4 |
|
return array_key_exists($code, $this->config); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* getResolvedFromString. |
|
93
|
|
|
* |
|
94
|
|
|
* @access protected |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
4 |
|
protected function getResolvedFromString(string $code) |
|
98
|
|
|
{ |
|
99
|
4 |
|
if (! $this->entityExists($code)) { |
|
100
|
|
|
throw new FactoryException('Фабрика не может сделать такую сущность'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
4 |
|
if (empty($this->container)) { |
|
104
|
4 |
|
return new $this->config[$code]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this->container->get($this->config[$code]); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.