|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace a9f\Fractor\Configuration; |
| 6 | + |
| 7 | +use a9f\Fractor\Application\Contract\ConfigurableFractorRule; |
| 8 | +use a9f\Fractor\Application\Contract\FractorRule; |
| 9 | +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 10 | +use Webmozart\Assert\Assert; |
| 11 | + |
| 12 | +final class FractorConfigurationBuilder |
| 13 | +{ |
| 14 | + /** |
| 15 | + * @var string[] |
| 16 | + */ |
| 17 | + private array $sets = []; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var string[] |
| 21 | + */ |
| 22 | + private array $paths = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var array<mixed> |
| 26 | + */ |
| 27 | + private array $skip = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var array<class-string<FractorRule>> |
| 31 | + */ |
| 32 | + private array $rules = []; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var array<class-string<ConfigurableFractorRule>, array<int|string, mixed>> |
| 36 | + */ |
| 37 | + private array $rulesWithConfigurations = []; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param string[] $paths |
| 41 | + */ |
| 42 | + public function withPaths(array $paths): self |
| 43 | + { |
| 44 | + $this->paths = $paths; |
| 45 | + |
| 46 | + return $this; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @param array<mixed> $skip |
| 51 | + */ |
| 52 | + public function withSkip(array $skip): self |
| 53 | + { |
| 54 | + $this->skip = array_merge($this->skip, $skip); |
| 55 | + |
| 56 | + return $this; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param string[] $sets |
| 61 | + */ |
| 62 | + public function withSets(array $sets): self |
| 63 | + { |
| 64 | + $this->sets = array_merge($this->sets, $sets); |
| 65 | + |
| 66 | + return $this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param class-string<ConfigurableFractorRule> $fractorClass |
| 71 | + * @param array<int|string, mixed> $configuration |
| 72 | + */ |
| 73 | + public function withConfiguredRule(string $fractorClass, array $configuration): self |
| 74 | + { |
| 75 | + $this->rulesWithConfigurations[$fractorClass][] = $configuration; |
| 76 | + |
| 77 | + return $this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @param array<class-string<FractorRule>> $rules |
| 82 | + */ |
| 83 | + public function withRules(array $rules): self |
| 84 | + { |
| 85 | + $this->rules = array_merge($this->rules, $rules); |
| 86 | + |
| 87 | + return $this; |
| 88 | + } |
| 89 | + |
| 90 | + public function __invoke(ContainerConfigurator $containerConfigurator): void |
| 91 | + { |
| 92 | + Assert::allString($this->paths); |
| 93 | + |
| 94 | + $parameters = $containerConfigurator->parameters(); |
| 95 | + $parameters->set(Option::PATHS, $this->paths); |
| 96 | + $parameters->set(Option::SKIP, $this->skip); |
| 97 | + |
| 98 | + $services = $containerConfigurator->services(); |
| 99 | + |
| 100 | + foreach ($this->rules as $rule) { |
| 101 | + Assert::classExists($rule); |
| 102 | + Assert::isAOf($rule, FractorRule::class); |
| 103 | + $services->set($rule) |
| 104 | + ->autoconfigure() |
| 105 | + ->autowire(); |
| 106 | + } |
| 107 | + |
| 108 | + Assert::allString($this->sets); |
| 109 | + foreach ($this->sets as $set) { |
| 110 | + Assert::fileExists($set); |
| 111 | + $containerConfigurator->import($set); |
| 112 | + } |
| 113 | + |
| 114 | + foreach ($this->rulesWithConfigurations as $configuredRule => $configuration) { |
| 115 | + Assert::classExists($configuredRule); |
| 116 | + Assert::isAOf($configuredRule, ConfigurableFractorRule::class); |
| 117 | + |
| 118 | + $services->set($configuredRule) |
| 119 | + ->call('configure', $configuration) |
| 120 | + ->autoconfigure() |
| 121 | + ->autowire(); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments