|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (C) 2018 |
|
5
|
|
|
* Nathan Boiron <[email protected]> |
|
6
|
|
|
* Romain Canon <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of the TYPO3 NotiZ project. |
|
9
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
10
|
|
|
* under the terms of the GNU General Public License, either |
|
11
|
|
|
* version 3 of the License, or any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* For the full copyright and license information, see: |
|
14
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace CuyZ\Notiz\Core\Definition\Builder\Component; |
|
18
|
|
|
|
|
19
|
|
|
use CuyZ\Notiz\Core\Definition\Builder\Component\Processor\DefinitionProcessor; |
|
20
|
|
|
use CuyZ\Notiz\Core\Definition\Builder\Component\Source\DefinitionSource; |
|
21
|
|
|
use CuyZ\Notiz\Core\Exception\ClassNotFoundException; |
|
22
|
|
|
use CuyZ\Notiz\Core\Exception\DuplicateEntryException; |
|
23
|
|
|
use CuyZ\Notiz\Core\Exception\EntryNotFoundException; |
|
24
|
|
|
use CuyZ\Notiz\Core\Exception\InvalidClassException; |
|
25
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Components container used in the definition builder. |
|
29
|
|
|
* |
|
30
|
|
|
* You may use it in your own components registration service to add new sources |
|
31
|
|
|
* and processors. |
|
32
|
|
|
* |
|
33
|
|
|
* @see \CuyZ\Notiz\Core\Definition\Builder\DefinitionBuilder |
|
34
|
|
|
*/ |
|
35
|
|
|
class DefinitionComponents |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* @var DefinitionSource[] |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $sources = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var DefinitionProcessor[] |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $processors = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var ObjectManagerInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $objectManager; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param ObjectManagerInterface $objectManager |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(ObjectManagerInterface $objectManager) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->objectManager = $objectManager; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Registers a new source component, that will later be used to fetch a |
|
62
|
|
|
* definition array from any origin. |
|
63
|
|
|
* |
|
64
|
|
|
* The given class name must implement the interface below: |
|
65
|
|
|
* |
|
66
|
|
|
* @see \CuyZ\Notiz\Core\Definition\Builder\Component\Source\DefinitionSource |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $identifier |
|
69
|
|
|
* @param string $className |
|
70
|
|
|
* @return DefinitionSource |
|
71
|
|
|
* |
|
72
|
|
|
* @throws ClassNotFoundException |
|
73
|
|
|
* @throws DuplicateEntryException |
|
74
|
|
|
* @throws InvalidClassException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function addSource($identifier, $className) |
|
77
|
|
|
{ |
|
78
|
|
|
if (!class_exists($className)) { |
|
79
|
|
|
throw ClassNotFoundException::definitionSourceClassNotFound($className); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (!in_array(DefinitionSource::class, class_implements($className))) { |
|
83
|
|
|
throw InvalidClassException::definitionSourceHasMissingInterface($className); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (false === $this->hasSource($identifier)) { |
|
87
|
|
|
$this->sources[$identifier] = $this->objectManager->get($className); |
|
88
|
|
|
} else { |
|
89
|
|
|
$existingEntryClass = get_class($this->sources[$identifier]); |
|
90
|
|
|
|
|
91
|
|
|
if ($className !== $existingEntryClass) { |
|
92
|
|
|
throw DuplicateEntryException::definitionSourceDuplication($identifier, $existingEntryClass); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->sources[$identifier]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $identifier |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public function hasSource($identifier) |
|
104
|
|
|
{ |
|
105
|
|
|
return true === isset($this->sources[$identifier]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string $identifier |
|
110
|
|
|
* @return DefinitionSource |
|
111
|
|
|
* |
|
112
|
|
|
* @throws EntryNotFoundException |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getSource($identifier) |
|
115
|
|
|
{ |
|
116
|
|
|
if (false === $this->hasSource($identifier)) { |
|
117
|
|
|
throw EntryNotFoundException::definitionSourceNotFound($identifier); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->sources[$identifier]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return DefinitionSource[] |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getSources() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->sources; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Registers a new processor component, that will later be used to modify |
|
133
|
|
|
* the definition object after it has been created from sources array. |
|
134
|
|
|
* |
|
135
|
|
|
* The given class name must implement the interface below: |
|
136
|
|
|
* |
|
137
|
|
|
* @see \CuyZ\Notiz\Core\Definition\Builder\Component\Processor\DefinitionProcessor |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $identifier |
|
140
|
|
|
* @param string $className |
|
141
|
|
|
* @return DefinitionProcessor |
|
142
|
|
|
* |
|
143
|
|
|
* @throws ClassNotFoundException |
|
144
|
|
|
* @throws DuplicateEntryException |
|
145
|
|
|
* @throws InvalidClassException |
|
146
|
|
|
*/ |
|
147
|
|
|
public function addProcessor($identifier, $className) |
|
148
|
|
|
{ |
|
149
|
|
|
if (!class_exists($className)) { |
|
150
|
|
|
throw ClassNotFoundException::definitionProcessorClassNotFound($className); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
if (!in_array(DefinitionProcessor::class, class_implements($className))) { |
|
154
|
|
|
throw InvalidClassException::definitionProcessorHasMissingInterface($className); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (false === $this->hasProcessor($identifier)) { |
|
158
|
|
|
$this->processors[$identifier] = $this->objectManager->get($className); |
|
159
|
|
|
} else { |
|
160
|
|
|
$existingEntryClass = get_class($this->processors[$identifier]); |
|
161
|
|
|
|
|
162
|
|
|
if ($className !== $existingEntryClass) { |
|
163
|
|
|
throw DuplicateEntryException::definitionProcessorDuplication($identifier, $existingEntryClass); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $this->processors[$identifier]; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param string $identifier |
|
172
|
|
|
* @return bool |
|
173
|
|
|
*/ |
|
174
|
|
|
public function hasProcessor($identifier) |
|
175
|
|
|
{ |
|
176
|
|
|
return true === isset($this->processors[$identifier]); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $identifier |
|
181
|
|
|
* @return DefinitionProcessor |
|
182
|
|
|
* |
|
183
|
|
|
* @throws EntryNotFoundException |
|
184
|
|
|
*/ |
|
185
|
|
|
public function getProcessor($identifier) |
|
186
|
|
|
{ |
|
187
|
|
|
if (false === $this->hasProcessor($identifier)) { |
|
188
|
|
|
throw EntryNotFoundException::definitionProcessorNotFound($identifier); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $this->processors[$identifier]; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return DefinitionProcessor[] |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getProcessors() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->processors; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|