Lines
88.88%
8 / 9
Functions and Methods
80.00%
4 / 5
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| SyntheticServices | 88.88% | 8 / 9 | 80.00% | 4 / 5 | 8.09 | 0.00% | 0 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| withService | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| withOverrides | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| declareDefinitionsOn | 75.00% | 3 / 4 | 0.00% | 0 / 1 | 3.14 | |||||
| setInstancesOn | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Kernel; | |
| 6 | ||
| 7 | use Symfony\Component\DependencyInjection\ContainerBuilder; | |
| 8 | use Symfony\Component\DependencyInjection\Definition; | |
| 9 | ||
| 10 | final readonly class SyntheticServices | |
| 11 | { | |
| 12 | /** @param array<string, object> $services */ | |
| 13 | public function __construct(private array $services = []) {} | |
| 14 | ||
| 15 | public function withService(string $id, object $service): self | |
| 16 | { | |
| 17 | return new self([...$this->services, $id => $service]); | |
| 18 | } | |
| 19 | ||
| 20 | public function withOverrides(self $overrides): self | |
| 21 | { | |
| 22 | return new self([...$this->services, ...$overrides->services]); | |
| 23 | } | |
| 24 | ||
| 25 | public function declareDefinitionsOn(ContainerBuilder $builder): void | |
| 26 | { | |
| 27 | foreach ($this->services as $id => $service) { | |
| 28 | if ($builder->hasAlias($id)) { | |
| 29 | $builder->removeAlias($id); | |
| 30 | } | |
| 31 | ||
| 32 | // Public so an unused one survives compilation and can still be set. | |
| 33 | $builder->setDefinition($id, new Definition($service::class)->setSynthetic(true)->setPublic(true)); | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | // Call after compile: setting earlier drops the definition, and factories using it fail. | |
| 38 | public function setInstancesOn(ContainerBuilder $builder): void | |
| 39 | { | |
| 40 | foreach ($this->services as $id => $service) { | |
| 41 | $builder->set($id, $service); | |
| 42 | } | |
| 43 | } | |
| 44 | } |