-
Notifications
You must be signed in to change notification settings - Fork 4
/
ContainerBuilder.php
594 lines (512 loc) 路 17.6 KB
/
ContainerBuilder.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<?php
/**
* ClanCats Container
*
* @link https://github.com/ClanCats/Container/
* @copyright Copyright (c) 2016-2023 Mario D枚ring
* @license https://github.com/ClanCats/Container/blob/master/LICENSE (MIT License)
*/
namespace ClanCats\Container;
use ClanCats\Container\{
Container,
Exceptions\ContainerBuilderException
};
class ContainerBuilder
{
/**
* The full container name with namespace
*
* @var string
*/
protected string $containerName;
/**
* The class name without namespace
*
* @var string
*/
protected string $containerClassName;
/**
* Just the namespace
*
* @var string|null
*/
protected ?string $containerNamespace = null;
/**
* Should we override the debug function in the generated container
* So that when the container is var_dump't we do not end in an
* infinite recrusion?
*/
protected bool $overrideDebugInfo = true;
/**
* An array of paramters to be builded directly
* as propterty.
*
* @var array<string, mixed>
*/
protected array $parameters = [];
/**
* An array of service aliases to be defined.
*
* @var array<string, string>
*/
protected array $aliases = [];
/**
* An array of binded services
*
* @var array<string, ServiceDefinitionInterface>
*/
protected array $services = [];
/**
* An array of service names that should be shared in the builded container
*
* @var array<string>
*/
protected array $shared = [];
/**
* An array of converted service names
* The normalized service names is camel cased and should be usable as method name.
*
* @var array<string>
*/
private array $normalizedServiceNames = [];
/**
* Constrcut a container builder instance
*
* @param string $containerName
* @return void
*/
public function __construct(string $containerName)
{
$this->setContainerName($containerName);
}
/**
* Sets the container name
* This will also update the "containerClassName" and "containerNamespace"
*
* @param string $containerName
* @return void
*/
public function setContainerName(string $containerName)
{
if (empty($containerName) || !(preg_match('/^[a-zA-Z0-9\\\\_]*$/', $containerName)) || is_numeric($containerName[0]))
{
throw new ContainerBuilderException('The container name cannot be empty, start with a number or contain sepcial characters except "\\".');
}
if ($containerName[0] === "\\")
{
$containerName = substr($containerName, 1);
}
$this->containerClassName = $this->containerName = $containerName;
// check if we need to generate a namespace
if (($pos = strrpos($containerName, "\\")) !== false)
{
$this->containerNamespace = substr($containerName, 0, $pos);
$this->containerClassName = substr($containerName, $pos + 1);
}
}
/**
* Sets the override debug info flag
* When set to false the generated container will not override the __debugInfo method.
*
* @param bool $overrideDebugInfo
* @return void
*/
public function setOverrideDebugInfo(bool $overrideDebugInfo) : void
{
$this->overrideDebugInfo = $overrideDebugInfo;
}
/**
* Get the current container full name
*
* @return string
*/
public function getContainerName() : string
{
return $this->containerName;
}
/**
* Get the current container class name without namespace
*
* @return string
*/
public function getContainerClassName() : string
{
return $this->containerClassName;
}
/**
* Get the php container namespace not to confuse with the "ContainerNamespace" class.
*
* @return string|null
*/
public function getContainerNamespace() : ?string
{
return $this->containerNamespace;
}
/**
* Get all currently added services
*
* @return array<string, ServiceDefinitionInterface>
*/
public function getServices() : array
{
return $this->services;
}
/**
* Returns all shared service names
*
* @return array<string>
*/
public function getSharedNames() : array
{
return $this->shared;
}
/**
* Add a service by string and arguments array.
*
* @param string $serviceName
* @param class-string $serviceClass
* @param array<mixed> $serviceArguments
* @param bool $isShared
* @return ServiceDefinition
*/
public function add(string $serviceName, string $serviceClass, array $serviceArguments = [], bool $isShared = true) : ServiceDefinition
{
$service = new ServiceDefinition($serviceClass, $serviceArguments);
$this->addService($serviceName, $service, $isShared);
return $service;
}
/**
* Add services by an array
*
* @param array<string, array<mixed>> $servicesArray
* @return void
*/
public function addArray(array $servicesArray) : void
{
foreach($servicesArray as $serviceName => $serviceConfiguration)
{
$this->addService($serviceName, ServiceDefinition::fromArray($serviceConfiguration), $serviceConfiguration['shared'] ?? true);
}
}
/**
* Add a service definition instance to the container builder.
*
* @param string $serviceName
* @param ServiceDefinitionInterface $serviceDefinition
* @return void
*/
public function addService(string $serviceName, ServiceDefinitionInterface $serviceDefinition, bool $isShared = true) : void
{
if ($this->invalidServiceBuilderString($serviceName))
{
throw new ContainerBuilderException('The "'.$serviceName.'" servicename must be a string, cannot be numeric, empty or contain any special characters except "." and "_".');
}
// add the service definition
$this->services[$serviceName] = $serviceDefinition;
// generate the normalized name
$this->generateNormalizedServiceName($serviceName);
// set the shared unshared flag
if ($isShared && (!in_array($serviceName, $this->shared)))
{
$this->shared[] = $serviceName;
}
elseif ((!$isShared) && in_array($serviceName, $this->shared))
{
unset($this->shared[array_search($serviceName, $this->shared)]);
}
}
/**
* Import data from a container namespace
*
* @param ContainerNamespace $namespace
* @return void
*/
public function importNamespace(ContainerNamespace $namespace) : void
{
// import the parameters
$this->parameters = array_merge($this->parameters, $namespace->getParameters());
// import aliases
$this->aliases = array_merge($this->aliases, $namespace->getAliases());
// import the service definitions
foreach($namespace->getServices() as $name => $service)
{
$this->addService($name, $service);
}
}
/**
* Checks if the given string is valid and not numeric &
*
* @param string $value
* @return bool
*/
private function invalidServiceBuilderString(string $value) : bool
{
if (empty($value) || is_numeric($value)) {
return true;
}
// check for trailing / prepending whitespace ect.
if (trim($value) !== $value) {
return true;
}
// check for other special characters
if (preg_match('/[^a-zA-Z0-9._]+/', $value)) {
return true;
}
// also check the first character the string contains with a number
if (is_numeric($value[0]) || $value[0] === '.' || $value[0] === '_') {
return true;
}
$lastCharacter = $value[strlen($value) - 1];
if ($lastCharacter === '.' || $lastCharacter === '_') {
return true;
}
return false;
}
/**
* Generate a camelized service name
*
* @param string $serviceName
* @return string
*/
private function camelizeServiceName(string $serviceName) : string
{
return str_replace(['.', '_'], '', ucwords(str_replace(['.', '_'], '.', $serviceName), '.'));
}
/**
* Generates the "normalizedServiceNames" array.
*
* @param string $serviceName
* @return void
*/
private function generateNormalizedServiceName(string $serviceName)
{
$normalizedServiceName = $this->camelizeServiceName($serviceName);
$duplicateCounter = 0;
$countedNormalizedServiceName = $normalizedServiceName;
while(in_array($countedNormalizedServiceName, $this->normalizedServiceNames))
{
$duplicateCounter++;
$countedNormalizedServiceName = $normalizedServiceName . $duplicateCounter;
}
$this->normalizedServiceNames[$serviceName] = $countedNormalizedServiceName;
}
/**
* Generate the container class code string
*
* @return string
*/
public function generate() : string
{
$buffer = "<?php\n\n";
// add namespace if needed
if (!is_null($this->containerNamespace))
{
$buffer .= "namespace " . $this->containerNamespace . ";\n\n";
}
// add use statement for the super container
$aliasContainerName = 'ClanCatsContainer' . md5($this->containerName);
$buffer .= "use " . Container::class . " as " . $aliasContainerName . ";\n\n";
// generate the the class
$buffer .= "class $this->containerClassName extends $aliasContainerName {\n\n";
$buffer .= $this->generateParameters() . "\n";
$buffer .= $this->generateAliases() . "\n";
$buffer .= $this->generateMetaData() . "\n";
$buffer .= $this->generateResolverTypes() . "\n";
$buffer .= $this->generateResolverMappings() . "\n";
$buffer .= $this->generateResolverMethods() . "\n";
if ($this->overrideDebugInfo) {
$buffer .= $this->generateDebugInfo() . "\n";
}
return $buffer . "\n}";
}
/**
* Generate the service resolver method name for the given service
*
* @param string $serviceName
* @return string
*/
private function getResolverMethodName(string $serviceName) : string
{
if (!isset($this->normalizedServiceNames[$serviceName]))
{
throw new ContainerBuilderException("The '" . $serviceName . "' service has never been definied.");
}
return 'resolve' . $this->normalizedServiceNames[$serviceName];
}
/**
* Generate arguments code
*
* @param ServiceArguments $arguments
* @return string
*/
private function generateArgumentsCode(ServiceArguments $arguments) : string
{
$buffer = [];
foreach($arguments->getAll() as list($argumentValue, $argumentType))
{
if ($argumentType === ServiceArguments::DEPENDENCY)
{
if ($argumentValue === 'container')
{
$buffer[] = "\$this";
}
// if the dependency is defined in the current container builder
// we can be sure that it exists and directly call the resolver method
elseif (isset($this->services[$argumentValue]))
{
$resolverMethodCall = "\$this->" . $this->getResolverMethodName($argumentValue) . '()';
// if is not shared we can just forward the factory method
if (!in_array($argumentValue, $this->shared))
{
$buffer[] = $resolverMethodCall;
}
// otherwise we have to check if the singleton has
// already been resolved.
else
{
$buffer[] = "\$this->resolvedSharedServices['$argumentValue'] ?? \$this->resolvedSharedServices['$argumentValue'] = " . $resolverMethodCall;
}
}
// if the dependency is not defined inside the container builder
// it might be added dynamically later. So we just access the containers `get` method.
else
{
$buffer[] = "\$this->get('$argumentValue')";
}
}
elseif ($argumentType === ServiceArguments::PARAMETER)
{
$buffer[] = "\$this->getParameter('$argumentValue')";
}
elseif ($argumentType === ServiceArguments::RAW)
{
$buffer[] = var_export($argumentValue, true);
}
}
return implode(', ', $buffer);
}
/**
* Generate the containers parameter property
*
* @return string
*/
private function generateParameters() : string
{
return "protected array \$parameters = " . var_export($this->parameters, true) . ";\n";
}
/**
* Generate the containers aliases property
*
* @return string
*/
private function generateAliases() : string
{
return "protected array \$serviceAliases = " . var_export($this->aliases, true) . ";\n";
}
/**
* Generate the containers parameter property
*
* @return string
*/
private function generateMetaData() : string
{
$metaData = [];
$metaDataService = [];
foreach($this->services as $serviceName => $serviceDefinition)
{
foreach($serviceDefinition->getMetaData() as $key => $serviceMetaData)
{
if (!isset($metaData[$key])) {
$metaData[$key] = [];
}
$metaData[$key][$serviceName] = $serviceMetaData;
// mapping for the service centered
if (!isset($metaDataService[$serviceName])) {
$metaDataService[$serviceName] = [];
}
if (!in_array($key, $metaDataService[$serviceName])) {
$metaDataService[$serviceName][] = $key;
}
}
}
return "protected array \$metadata = " . var_export($metaData, true) . ";\nprotected array \$metadataService = " . var_export($metaDataService, true) . ";\n";
}
/**
* Generate the resolver types array
*
* @return string
*/
private function generateResolverTypes() : string
{
$types = [];
foreach($this->services as $serviceName => $serviceDefinition)
{
$types[] = var_export($serviceName, true) . ' => ' . Container::RESOLVE_METHOD;
}
// also add the aliases
foreach($this->aliases as $serviceName => $targetService)
{
$types[] = var_export($serviceName, true) . ' => ' . Container::RESOLVE_ALIAS;
}
return "protected array \$serviceResolverType = [" . implode(', ', $types) . "];\n";
}
/**
* Generate the resolver mappings array
*
* @return string
*/
private function generateResolverMappings() : string
{
$mappings = [];
foreach($this->services as $serviceName => $serviceDefinition)
{
$mappings[] = var_export($serviceName, true) . ' => ' . var_export($this->getResolverMethodName($serviceName), true);
}
return "protected array \$resolverMethods = [" . implode(', ', $mappings) . "];\n";
}
/**
* Generate the resolver methods
*
* @return string
*/
private function generateResolverMethods() : string
{
$buffer = "";
foreach($this->services as $serviceName => $serviceDefinition)
{
$isSharedService = in_array($serviceName, $this->shared);
$serviceClassName = $serviceDefinition->getClassName();
if ($serviceClassName[0] !== "\\") {
$serviceClassName = "\\" . $serviceClassName;
}
$buffer .= "public function " . $this->getResolverMethodName($serviceName) . "() : {$serviceClassName} {\n";
if ($isSharedService) {
$buffer .= "\tif (isset(\$this->resolvedSharedServices[" . var_export($serviceName, true) . "])) return \$this->resolvedSharedServices[" . var_export($serviceName, true) . "];\n";
}
$buffer .= "\t\$instance = new " . $serviceClassName . "(". $this->generateArgumentsCode($serviceDefinition->getArguments()) .");\n";
foreach($serviceDefinition->getMethodCalls() as list($callName, $callArguments))
{
$buffer .= "\t\$instance->" . $callName . '('. $this->generateArgumentsCode($callArguments) .");\n";
}
if ($isSharedService)
{
$buffer .= "\t\$this->resolvedSharedServices[" . var_export($serviceName, true) . "] = \$instance;\n";
}
$buffer .= "\treturn \$instance;\n";
$buffer .= "}\n";
}
return $buffer;
}
private function generateDebugInfo() : string
{
return <<<EOF
/**
* Override the debug info function so that we do not end in an infinite recrusion.
*/
public function __debugInfo() : array
{
return ['services' => \$this->available()];
}
EOF;
}
}