|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kiboko\Component\Satellite\Action\Custom\Factory; |
| 6 | + |
| 7 | +use Kiboko\Component\Packaging; |
| 8 | +use Kiboko\Component\Satellite\Action\Custom; |
| 9 | +use Kiboko\Component\Satellite\Action\Custom\Configuration; |
| 10 | +use Kiboko\Component\Satellite\DependencyInjection\SatelliteDependencyInjection; |
| 11 | +use Kiboko\Component\Satellite\ExpressionLanguage as Satellite; |
| 12 | +use Kiboko\Contract\Configurator; |
| 13 | +use Symfony\Component\Config\Definition\ConfigurationInterface; |
| 14 | +use Symfony\Component\Config\Definition\Exception as Symfony; |
| 15 | +use Symfony\Component\Config\Definition\Processor; |
| 16 | +use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
| 17 | +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
| 18 | +use Symfony\Component\String\ByteString; |
| 19 | + |
| 20 | +use function Kiboko\Component\SatelliteToolbox\Configuration\compileValueWhenExpression; |
| 21 | + |
| 22 | +class Action implements Configurator\FactoryInterface |
| 23 | +{ |
| 24 | + private readonly Processor $processor; |
| 25 | + private readonly ConfigurationInterface $configuration; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private readonly ExpressionLanguage $interpreter = new Satellite\ExpressionLanguage(), |
| 29 | + private readonly array $providers = [], |
| 30 | + ) { |
| 31 | + $this->processor = new Processor(); |
| 32 | + $this->configuration = new Configuration(); |
| 33 | + } |
| 34 | + |
| 35 | + public function configuration(): ConfigurationInterface |
| 36 | + { |
| 37 | + return $this->configuration; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @throws Configurator\ConfigurationExceptionInterface |
| 42 | + */ |
| 43 | + public function normalize(array $config): array |
| 44 | + { |
| 45 | + try { |
| 46 | + return $this->processor->processConfiguration($this->configuration, $config); |
| 47 | + } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException $exception) { |
| 48 | + throw new Configurator\InvalidConfigurationException($exception->getMessage(), 0, $exception); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public function validate(array $config): bool |
| 53 | + { |
| 54 | + try { |
| 55 | + $this->processor->processConfiguration($this->configuration, $config); |
| 56 | + |
| 57 | + return true; |
| 58 | + } catch (Symfony\InvalidTypeException|Symfony\InvalidConfigurationException) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @throws Configurator\ConfigurationExceptionInterface |
| 65 | + */ |
| 66 | + public function compile(array $config): Repository\Action |
| 67 | + { |
| 68 | + $containerName = sprintf('ProjectServiceContainer%s', ByteString::fromRandom(8)->toString()); |
| 69 | + |
| 70 | + $builder = new Custom\Builder\Action( |
| 71 | + compileValueWhenExpression($this->interpreter, $config['use']), |
| 72 | + sprintf('GyroscopsGenerated\\%s', $containerName), |
| 73 | + ); |
| 74 | + |
| 75 | + $container = (new SatelliteDependencyInjection(...$this->providers))($config); |
| 76 | + |
| 77 | + $repository = new Repository\Action($builder); |
| 78 | + |
| 79 | + $dumper = new PhpDumper($container); |
| 80 | + $repository->addFiles( |
| 81 | + new Packaging\File( |
| 82 | + sprintf('%s.php', $containerName), |
| 83 | + new Packaging\Asset\InMemory( |
| 84 | + $dumper->dump(['class' => $containerName, 'namespace' => 'GyroscopsGenerated']) |
| 85 | + ) |
| 86 | + ), |
| 87 | + ); |
| 88 | + |
| 89 | + return $repository; |
| 90 | + } |
| 91 | +} |
0 commit comments