-
Notifications
You must be signed in to change notification settings - Fork 13
/
FactoryTrait.php
248 lines (217 loc) · 8.19 KB
/
FactoryTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
namespace atk4\core;
trait FactoryTrait
{
/**
* Check this property to see if trait is present in the object.
*
* @var bool
*/
public $_factoryTrait = true;
/**
* Given two seeds (or more) will merge them, prioritizing the first argument.
* If object is passed on either of arguments, then it will setDefaults() remaining
* arguments, respecting their positioning.
*
* See full documentation.
*
* @param array|object|mixed $seed
* @param array|object|mixed $seed2
* @param array $more_seeds
*
* @return object|array if at least one seed is an object, will return object
*/
public function mergeSeeds($seed, $seed2, ...$more_seeds)
{
// recursively merge extra seeds
if ($more_seeds) {
$seed2 = $this->mergeSeeds($seed2, ...$more_seeds);
}
if (is_object($seed)) {
if (is_array($seed2)) {
// set defaults but don't override existing properties
$arguments = array_filter($seed2, 'is_numeric', ARRAY_FILTER_USE_KEY); // with numeric keys
$injection = array_diff_key($seed2, $arguments); // with string keys
if ($injection) {
if (isset($seed->_DIContainerTrait)) {
$seed->setDefaults($injection, true);
} else {
throw new Exception([
'factory() requested to passively inject some properties into existing object that does not use \atk4\core\DIContainerTrait',
'object' => $seed,
'injection' => $injection,
]);
}
}
}
return $seed;
}
if (is_object($seed2)) {
// seed is not object, and setDefaults will complain if it's not array
if (is_array($seed)) {
$arguments = array_filter($seed, 'is_numeric', ARRAY_FILTER_USE_KEY); // with numeric keys
$injection = array_diff_key($seed, $arguments); // with string keys
if ($injection) {
if (isset($seed2->_DIContainerTrait)) {
$seed2->setDefaults($injection);
} else {
throw new Exception([
'factory() requested to inject some properties into existing object that does not use \atk4\core\DIContainerTrait',
'object' => $seed2,
'injection' => $seed,
]);
}
}
}
return $seed2;
}
if (!is_array($seed)) {
$seed = [$seed];
}
if (!is_array($seed2)) {
$seed2 = [$seed2];
}
// merge seeds but prefer seed over seed2
// move numerical keys to the beginning and sort them
$res = [];
$res2 = [];
foreach ($seed as $k => $v) {
if (is_numeric($k)) {
$res[$k] = $v;
} elseif ($v !== null) {
$res2[$k] = $v;
}
}
foreach ($seed2 as $k => $v) {
if (is_numeric($k)) {
if (!isset($res[$k])) {
$res[$k] = $v;
}
} elseif ($v !== null) {
if (!isset($res2[$k])) {
$res2[$k] = $v;
}
}
}
ksort($res, SORT_NUMERIC);
$res = $res + $res2;
return $res;
}
/**
* Given a Seed (see doc) as a first argument, will create object of a corresponding
* class, call constructor with numerical arguments of a seed and inject key/value
* arguments.
*
* Argument $defaults has the same effect as the seed, but will not contain the class.
* Class is always determined by seed, except if you pass object into defaults.
*
* To learn more about mechanics of factory trait, see documentation
*
* @param mixed $seed
* @param array $defaults
* @param string $prefix Optional prefix for class name
*/
public function factory($seed, $defaults = [], string $prefix = null): object
{
if ($defaults === null) {
$defaults = [];
}
if (!$seed) {
$seed = [];
}
if (!is_array($seed)) {
$seed = [$seed];
}
if (is_array($defaults)) {
array_unshift($defaults, null); // insert argument 0
} elseif (!is_object($defaults)) {
$defaults = [null, $defaults];
}
$seed = $this->mergeSeeds($seed, $defaults);
if (is_object($seed)) {
// setDefaults already called
return $seed;
}
$arguments = array_filter($seed, 'is_numeric', ARRAY_FILTER_USE_KEY); // with numeric keys
$injection = array_diff_key($seed, $arguments); // with string keys
$object = array_shift($arguments); // first numeric key argument is object
if (!is_object($object)) {
$class = is_string($object) ? $this->normalizeClassName($object, $prefix) : '';
if (!$class) {
throw new Exception([
'Class name was not specified by the seed',
'seed' => $seed,
]);
}
$object = new $class(...$arguments);
}
if ($injection) {
if (isset($object->_DIContainerTrait)) {
$object->setDefaults($injection);
} else {
throw new Exception([
'factory() could not inject properties into new object. It does not use \atk4\core\DIContainerTrait',
'object' => $object,
'seed' => $seed,
'injection' => $injection,
]);
}
}
return $object;
}
/**
* First normalize class name, then add specified prefix to
* class name. Finally if $app is defined, and has method
* `normalizeClassNameApp` it will also get a chance to
* add prefix.
*
* Rules observed, in order:
* - If class starts with "." then prefixing is always done.
* - If class contains "\" prefixing is never done.
* - If class (with prefix) exists, do prefix.
* - don't prefix otherwise.
*
* Example: normalizeClassName('User', 'Model') == 'Model\User';
* Example: normalizeClassName(Test\User::class, 'Model') == 'Test\User'; # or as per "use"
* Example: normalizeClassName('Test/User', 'Model') == 'Model\Test\User';
* Example: normalizeClassName('./User', 'Model') == 'Model\User';
* Example: normalizeClassName('User', 'Model') == 'Model\User'; # if exists, 'User' otherwise
*
* # If used without namespace:
* Example: normalizeClassName(User::class, 'Model') == 'Model\User'; # if exists, 'User' otherwise
*
* @param string $name Name of class
* @param string $prefix Optional prefix for class name
*
* @return string Full, normalized class name
*/
public function normalizeClassName(string $name, string $prefix = null): string
{
// If App has "normalizeClassName" (obsolete now), use it instead
if (
isset($this->_appScopeTrait, $this->app)
&& method_exists($this->app, 'normalizeClassNameApp')
) {
$result = $this->app->normalizeClassNameApp($name, $prefix);
if ($result !== null) {
return $result;
}
}
// Rule 1: if starts with "." always prefix
if ($name && $name[0] === '.' && $prefix) {
$name = $prefix . '\\' . substr($name, 1);
$name = str_replace('/', '\\', $name);
return $name;
}
// Rule 2: if "\" is present, don't prefix
if (strpos($name, '\\') !== false) {
$name = str_replace('/', '\\', $name);
return $name;
}
if ($name && $name[0] !== '/' && $prefix) {
$name = $prefix . '\\' . $name;
}
$name = str_replace('/', '\\', $name);
return $name;
}
}