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