-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Definition.php
809 lines (689 loc) · 19.9 KB
/
Definition.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\Argument\BoundArgument;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
/**
* Definition represents a service definition.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Definition
{
private const DEFAULT_DEPRECATION_TEMPLATE = 'The "%service_id%" service is deprecated. You should stop using it, as it will be removed in the future.';
private ?string $class = null;
private ?string $file = null;
private string|array|null $factory = null;
private bool $shared = true;
private array $deprecation = [];
private array $properties = [];
private array $calls = [];
private array $instanceof = [];
private bool $autoconfigured = false;
private string|array|null $configurator = null;
private array $tags = [];
private bool $public = false;
private bool $synthetic = false;
private bool $abstract = false;
private bool $lazy = false;
private ?array $decoratedService = null;
private bool $autowired = false;
private array $changes = [];
private array $bindings = [];
private array $errors = [];
protected array $arguments = [];
/**
* @internal
*
* Used to store the name of the inner id when using service decoration together with autowiring
*/
public ?string $innerServiceId = null;
/**
* @internal
*
* Used to store the behavior to follow when using service decoration and the decorated service is invalid
*/
public ?int $decorationOnInvalid = null;
public function __construct(?string $class = null, array $arguments = [])
{
if (null !== $class) {
$this->setClass($class);
}
$this->arguments = $arguments;
}
/**
* Returns all changes tracked for the Definition object.
*/
public function getChanges(): array
{
return $this->changes;
}
/**
* Sets the tracked changes for the Definition object.
*
* @param array $changes An array of changes for this Definition
*
* @return $this
*/
public function setChanges(array $changes): static
{
$this->changes = $changes;
return $this;
}
/**
* Sets a factory.
*
* @param string|array|Reference|null $factory A PHP function, reference or an array containing a class/Reference and a method to call
*
* @return $this
*/
public function setFactory(string|array|Reference|null $factory): static
{
$this->changes['factory'] = true;
if (\is_string($factory) && str_contains($factory, '::')) {
$factory = explode('::', $factory, 2);
} elseif ($factory instanceof Reference) {
$factory = [$factory, '__invoke'];
}
$this->factory = $factory;
return $this;
}
/**
* Gets the factory.
*
* @return string|array|null The PHP function or an array containing a class/Reference and a method to call
*/
public function getFactory(): string|array|null
{
return $this->factory;
}
/**
* Sets the service that this service is decorating.
*
* @param string|null $id The decorated service id, use null to remove decoration
* @param string|null $renamedId The new decorated service id
*
* @return $this
*
* @throws InvalidArgumentException in case the decorated service id and the new decorated service id are equals
*/
public function setDecoratedService(?string $id, ?string $renamedId = null, int $priority = 0, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE): static
{
if ($renamedId && $id === $renamedId) {
throw new InvalidArgumentException(\sprintf('The decorated service inner name for "%s" must be different than the service name itself.', $id));
}
$this->changes['decorated_service'] = true;
if (null === $id) {
$this->decoratedService = null;
} else {
$this->decoratedService = [$id, $renamedId, $priority];
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
$this->decoratedService[] = $invalidBehavior;
}
}
return $this;
}
/**
* Gets the service that this service is decorating.
*
* @return array|null An array composed of the decorated service id, the new id for it and the priority of decoration, null if no service is decorated
*/
public function getDecoratedService(): ?array
{
return $this->decoratedService;
}
/**
* Sets the service class.
*
* @return $this
*/
public function setClass(?string $class): static
{
$this->changes['class'] = true;
$this->class = $class;
return $this;
}
/**
* Gets the service class.
*/
public function getClass(): ?string
{
return $this->class;
}
/**
* Sets the arguments to pass to the service constructor/factory method.
*
* @return $this
*/
public function setArguments(array $arguments): static
{
$this->arguments = $arguments;
return $this;
}
/**
* Sets the properties to define when creating the service.
*
* @return $this
*/
public function setProperties(array $properties): static
{
$this->properties = $properties;
return $this;
}
/**
* Gets the properties to define when creating the service.
*/
public function getProperties(): array
{
return $this->properties;
}
/**
* Sets a specific property.
*
* @return $this
*/
public function setProperty(string $name, mixed $value): static
{
$this->properties[$name] = $value;
return $this;
}
/**
* Adds an argument to pass to the service constructor/factory method.
*
* @return $this
*/
public function addArgument(mixed $argument): static
{
$this->arguments[] = $argument;
return $this;
}
/**
* Replaces a specific argument.
*
* @return $this
*
* @throws OutOfBoundsException When the replaced argument does not exist
*/
public function replaceArgument(int|string $index, mixed $argument): static
{
if (0 === \count($this->arguments)) {
throw new OutOfBoundsException(\sprintf('Cannot replace arguments for class "%s" if none have been configured yet.', $this->class));
}
if (!\array_key_exists($index, $this->arguments)) {
throw new OutOfBoundsException(\sprintf('The argument "%s" doesn\'t exist in class "%s".', $index, $this->class));
}
$this->arguments[$index] = $argument;
return $this;
}
/**
* Sets a specific argument.
*
* @return $this
*/
public function setArgument(int|string $key, mixed $value): static
{
$this->arguments[$key] = $value;
return $this;
}
/**
* Gets the arguments to pass to the service constructor/factory method.
*/
public function getArguments(): array
{
return $this->arguments;
}
/**
* Gets an argument to pass to the service constructor/factory method.
*
* @throws OutOfBoundsException When the argument does not exist
*/
public function getArgument(int|string $index): mixed
{
if (!\array_key_exists($index, $this->arguments)) {
throw new OutOfBoundsException(\sprintf('The argument "%s" doesn\'t exist in class "%s".', $index, $this->class));
}
return $this->arguments[$index];
}
/**
* Sets the methods to call after service initialization.
*
* @return $this
*/
public function setMethodCalls(array $calls = []): static
{
$this->calls = [];
foreach ($calls as $call) {
$this->addMethodCall($call[0], $call[1], $call[2] ?? false);
}
return $this;
}
/**
* Adds a method to call after service initialization.
*
* @param string $method The method name to call
* @param array $arguments An array of arguments to pass to the method call
* @param bool $returnsClone Whether the call returns the service instance or not
*
* @return $this
*
* @throws InvalidArgumentException on empty $method param
*/
public function addMethodCall(string $method, array $arguments = [], bool $returnsClone = false): static
{
if (!$method) {
throw new InvalidArgumentException('Method name cannot be empty.');
}
$this->calls[] = $returnsClone ? [$method, $arguments, true] : [$method, $arguments];
return $this;
}
/**
* Removes a method to call after service initialization.
*
* @return $this
*/
public function removeMethodCall(string $method): static
{
foreach ($this->calls as $i => $call) {
if ($call[0] === $method) {
unset($this->calls[$i]);
}
}
return $this;
}
/**
* Check if the current definition has a given method to call after service initialization.
*/
public function hasMethodCall(string $method): bool
{
foreach ($this->calls as $call) {
if ($call[0] === $method) {
return true;
}
}
return false;
}
/**
* Gets the methods to call after service initialization.
*/
public function getMethodCalls(): array
{
return $this->calls;
}
/**
* Sets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
*
* @param ChildDefinition[] $instanceof
*
* @return $this
*/
public function setInstanceofConditionals(array $instanceof): static
{
$this->instanceof = $instanceof;
return $this;
}
/**
* Gets the definition templates to conditionally apply on the current definition, keyed by parent interface/class.
*
* @return ChildDefinition[]
*/
public function getInstanceofConditionals(): array
{
return $this->instanceof;
}
/**
* Sets whether or not instanceof conditionals should be prepended with a global set.
*
* @return $this
*/
public function setAutoconfigured(bool $autoconfigured): static
{
$this->changes['autoconfigured'] = true;
$this->autoconfigured = $autoconfigured;
return $this;
}
public function isAutoconfigured(): bool
{
return $this->autoconfigured;
}
/**
* Sets tags for this definition.
*
* @return $this
*/
public function setTags(array $tags): static
{
$this->tags = $tags;
return $this;
}
/**
* Returns all tags.
*/
public function getTags(): array
{
return $this->tags;
}
/**
* Gets a tag by name.
*/
public function getTag(string $name): array
{
return $this->tags[$name] ?? [];
}
/**
* Adds a tag for this definition.
*
* @return $this
*/
public function addTag(string $name, array $attributes = []): static
{
$this->tags[$name][] = $attributes;
return $this;
}
/**
* Whether this definition has a tag with the given name.
*/
public function hasTag(string $name): bool
{
return isset($this->tags[$name]);
}
/**
* Clears all tags for a given name.
*
* @return $this
*/
public function clearTag(string $name): static
{
unset($this->tags[$name]);
return $this;
}
/**
* Clears the tags for this definition.
*
* @return $this
*/
public function clearTags(): static
{
$this->tags = [];
return $this;
}
/**
* Sets a file to require before creating the service.
*
* @return $this
*/
public function setFile(?string $file): static
{
$this->changes['file'] = true;
$this->file = $file;
return $this;
}
/**
* Gets the file to require before creating the service.
*/
public function getFile(): ?string
{
return $this->file;
}
/**
* Sets if the service must be shared or not.
*
* @return $this
*/
public function setShared(bool $shared): static
{
$this->changes['shared'] = true;
$this->shared = $shared;
return $this;
}
/**
* Whether this service is shared.
*/
public function isShared(): bool
{
return $this->shared;
}
/**
* Sets the visibility of this service.
*
* @return $this
*/
public function setPublic(bool $boolean): static
{
$this->changes['public'] = true;
$this->public = $boolean;
return $this;
}
/**
* Whether this service is public facing.
*/
public function isPublic(): bool
{
return $this->public;
}
/**
* Whether this service is private.
*/
public function isPrivate(): bool
{
return !$this->public;
}
/**
* Sets the lazy flag of this service.
*
* @return $this
*/
public function setLazy(bool $lazy): static
{
$this->changes['lazy'] = true;
$this->lazy = $lazy;
return $this;
}
/**
* Whether this service is lazy.
*/
public function isLazy(): bool
{
return $this->lazy;
}
/**
* Sets whether this definition is synthetic, that is not constructed by the
* container, but dynamically injected.
*
* @return $this
*/
public function setSynthetic(bool $boolean): static
{
$this->synthetic = $boolean;
if (!isset($this->changes['public'])) {
$this->setPublic(true);
}
return $this;
}
/**
* Whether this definition is synthetic, that is not constructed by the
* container, but dynamically injected.
*/
public function isSynthetic(): bool
{
return $this->synthetic;
}
/**
* Whether this definition is abstract, that means it merely serves as a
* template for other definitions.
*
* @return $this
*/
public function setAbstract(bool $boolean): static
{
$this->abstract = $boolean;
return $this;
}
/**
* Whether this definition is abstract, that means it merely serves as a
* template for other definitions.
*/
public function isAbstract(): bool
{
return $this->abstract;
}
/**
* Whether this definition is deprecated, that means it should not be called
* anymore.
*
* @param string $package The name of the composer package that is triggering the deprecation
* @param string $version The version of the package that introduced the deprecation
* @param string $message The deprecation message to use
*
* @return $this
*
* @throws InvalidArgumentException when the message template is invalid
*/
public function setDeprecated(string $package, string $version, string $message): static
{
if ('' !== $message) {
if (preg_match('#[\r\n]|\*/#', $message)) {
throw new InvalidArgumentException('Invalid characters found in deprecation template.');
}
if (!str_contains($message, '%service_id%')) {
throw new InvalidArgumentException('The deprecation template must contain the "%service_id%" placeholder.');
}
}
$this->changes['deprecated'] = true;
$this->deprecation = ['package' => $package, 'version' => $version, 'message' => $message ?: self::DEFAULT_DEPRECATION_TEMPLATE];
return $this;
}
/**
* Whether this definition is deprecated, that means it should not be called
* anymore.
*/
public function isDeprecated(): bool
{
return (bool) $this->deprecation;
}
/**
* @param string $id Service id relying on this definition
*/
public function getDeprecation(string $id): array
{
return [
'package' => $this->deprecation['package'],
'version' => $this->deprecation['version'],
'message' => str_replace('%service_id%', $id, $this->deprecation['message']),
];
}
/**
* Sets a configurator to call after the service is fully initialized.
*
* @param string|array|Reference|null $configurator A PHP function, reference or an array containing a class/Reference and a method to call
*
* @return $this
*/
public function setConfigurator(string|array|Reference|null $configurator): static
{
$this->changes['configurator'] = true;
if (\is_string($configurator) && str_contains($configurator, '::')) {
$configurator = explode('::', $configurator, 2);
} elseif ($configurator instanceof Reference) {
$configurator = [$configurator, '__invoke'];
}
$this->configurator = $configurator;
return $this;
}
/**
* Gets the configurator to call after the service is fully initialized.
*/
public function getConfigurator(): string|array|null
{
return $this->configurator;
}
/**
* Is the definition autowired?
*/
public function isAutowired(): bool
{
return $this->autowired;
}
/**
* Enables/disables autowiring.
*
* @return $this
*/
public function setAutowired(bool $autowired): static
{
$this->changes['autowired'] = true;
$this->autowired = $autowired;
return $this;
}
/**
* Gets bindings.
*
* @return BoundArgument[]
*/
public function getBindings(): array
{
return $this->bindings;
}
/**
* Sets bindings.
*
* Bindings map $named or FQCN arguments to values that should be
* injected in the matching parameters (of the constructor, of methods
* called and of controller actions).
*
* @return $this
*/
public function setBindings(array $bindings): static
{
foreach ($bindings as $key => $binding) {
if (0 < strpos($key, '$') && $key !== $k = preg_replace('/[ \t]*\$/', ' $', $key)) {
unset($bindings[$key]);
$bindings[$key = $k] = $binding;
}
if (!$binding instanceof BoundArgument) {
$bindings[$key] = new BoundArgument($binding);
}
}
$this->bindings = $bindings;
return $this;
}
/**
* Add an error that occurred when building this Definition.
*
* @return $this
*/
public function addError(string|\Closure|self $error): static
{
if ($error instanceof self) {
$this->errors = array_merge($this->errors, $error->errors);
} else {
$this->errors[] = $error;
}
return $this;
}
/**
* Returns any errors that occurred while building this Definition.
*/
public function getErrors(): array
{
foreach ($this->errors as $i => $error) {
if ($error instanceof \Closure) {
$this->errors[$i] = (string) $error();
} elseif (!\is_string($error)) {
$this->errors[$i] = (string) $error;
}
}
return $this->errors;
}
public function hasErrors(): bool
{
return (bool) $this->errors;
}
}