1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aoe\Asdis\System\Factory; |
4
|
|
|
|
5
|
|
|
use Aoe\Asdis\System\Configuration\Provider; |
6
|
|
|
use Aoe\Asdis\System\Factory\Exception\DeclarationNotFound; |
7
|
|
|
use Aoe\Asdis\System\Factory\Exception\InvalidDeclaration; |
8
|
|
|
use Aoe\Asdis\System\Factory\Exception\MissingImplementation; |
9
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Abstract factory class for factories that create their products from array based declarations. |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractDeclarationBasedFactory |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public const DECLARATION_KEY = 'key'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public const DECLARATION_CLASS = 'class'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public const DECLARATION_FILE = 'file'; |
30
|
|
|
|
31
|
|
|
protected ?Provider $configurationProvider = null; |
32
|
|
|
|
33
|
|
|
private ?string $fallbackKey = null; |
34
|
|
|
|
35
|
|
|
private array $declarations = []; |
36
|
|
|
|
37
|
|
|
private array $classImplements = []; |
38
|
|
|
|
39
|
2 |
|
public function injectConfigurationProvider(Provider $configurationProvider): void |
40
|
|
|
{ |
41
|
2 |
|
$this->configurationProvider = $configurationProvider; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
3 |
|
protected function setClassImplements(array $classImplements): void |
45
|
|
|
{ |
46
|
3 |
|
$this->classImplements = $classImplements; |
47
|
3 |
|
} |
48
|
|
|
|
49
|
3 |
|
protected function setDeclarations(array $declarations): void |
50
|
|
|
{ |
51
|
3 |
|
$this->declarations = []; |
52
|
3 |
|
foreach ($declarations as $declaration) { |
53
|
2 |
|
$this->addDeclaration($declaration); |
54
|
|
|
} |
55
|
3 |
|
} |
56
|
|
|
|
57
|
1 |
|
protected function setFallbackKey(string $fallbackKey): void |
58
|
|
|
{ |
59
|
1 |
|
$this->fallbackKey = $fallbackKey; |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return object |
64
|
|
|
* @throws DeclarationNotFound |
65
|
|
|
* @throws MissingImplementation |
66
|
|
|
*/ |
67
|
1 |
|
protected function buildObjectFromKey(string $key) |
68
|
|
|
{ |
69
|
1 |
|
$declaration = null; |
|
|
|
|
70
|
|
|
try { |
71
|
1 |
|
$declaration = $this->getDeclarationByKey($key); |
72
|
|
|
} catch (DeclarationNotFound $declarationNotFound) { |
73
|
|
|
if (!isset($this->fallbackKey)) { |
74
|
|
|
throw $declarationNotFound; |
75
|
|
|
} |
76
|
|
|
$declaration = $this->getDeclarationByKey($this->fallbackKey); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
if (!class_exists($declaration[self::DECLARATION_CLASS])) { |
80
|
|
|
require_once $declaration[self::DECLARATION_FILE]; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
$object = GeneralUtility::makeInstance($declaration[self::DECLARATION_CLASS]); |
84
|
1 |
|
$implemented = class_implements($object); |
85
|
1 |
|
foreach ($this->classImplements as $classImplement) { |
86
|
1 |
|
if (!in_array($classImplement, $implemented)) { |
87
|
|
|
throw new MissingImplementation( |
88
|
|
|
$declaration[self::DECLARATION_CLASS], |
89
|
|
|
$classImplement, |
90
|
|
|
static::class, |
91
|
|
|
1_372_770_673_456 |
|
|
|
|
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
} |
95
|
1 |
|
return $object; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @throws InvalidDeclaration |
100
|
|
|
*/ |
101
|
2 |
|
private function addDeclaration(array $declaration): void |
102
|
|
|
{ |
103
|
|
|
if ( |
104
|
2 |
|
!array_key_exists(self::DECLARATION_KEY, $declaration) || |
105
|
2 |
|
!array_key_exists(self::DECLARATION_CLASS, $declaration) || |
106
|
2 |
|
!array_key_exists(self::DECLARATION_FILE, $declaration) |
107
|
|
|
) { |
108
|
|
|
throw new InvalidDeclaration( |
109
|
|
|
'Missing declaration element.', |
110
|
|
|
1_372_422_185_108 |
|
|
|
|
111
|
|
|
); |
112
|
|
|
} |
113
|
2 |
|
$this->declarations[] = $declaration; |
114
|
2 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return mixed |
118
|
|
|
* @throws DeclarationNotFound |
119
|
|
|
*/ |
120
|
1 |
|
private function getDeclarationByKey(string $key) |
121
|
|
|
{ |
122
|
1 |
|
foreach ($this->declarations as $declaration) { |
123
|
1 |
|
if (strcmp($declaration[self::DECLARATION_KEY], $key) === 0) { |
124
|
1 |
|
return $declaration; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
throw new DeclarationNotFound($key, 1_372_422_430_920); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|