|
4 | 4 |
|
5 | 5 | namespace Bizkit\VersioningBundle; |
6 | 6 |
|
7 | | -use Symfony\Component\HttpKernel\Bundle\Bundle; |
| 7 | +use Bizkit\VersioningBundle\Reader\ReaderInterface; |
| 8 | +use Bizkit\VersioningBundle\Strategy\StrategyInterface; |
| 9 | +use Bizkit\VersioningBundle\VCS\VCSHandlerInterface; |
| 10 | +use Bizkit\VersioningBundle\Writer\WriterInterface; |
| 11 | +use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator; |
| 12 | +use Symfony\Component\DependencyInjection\Alias; |
| 13 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 14 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 15 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 16 | +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; |
| 17 | +use Symfony\Component\HttpKernel\Bundle\AbstractBundle; |
8 | 18 |
|
9 | | -final class BizkitVersioningBundle extends Bundle |
| 19 | +final class BizkitVersioningBundle extends AbstractBundle implements CompilerPassInterface |
10 | 20 | { |
11 | | - public function getPath(): string |
| 21 | + private string $configuredFormat; |
| 22 | + private string $configuredStrategy; |
| 23 | + private ?string $configuredVCSHandler; |
| 24 | + |
| 25 | + public function build(ContainerBuilder $container): void |
| 26 | + { |
| 27 | + $container->addCompilerPass($this); // todo |
| 28 | + } |
| 29 | + |
| 30 | + public function configure(DefinitionConfigurator $definition): void |
| 31 | + { |
| 32 | + $definition->import(\dirname(__DIR__).'/config/definition.php'); |
| 33 | + } |
| 34 | + |
| 35 | + public function loadExtension(array $config, ContainerConfigurator $configurator, ContainerBuilder $container): void |
| 36 | + { |
| 37 | + $this->configuredFormat = $config['format']; |
| 38 | + $this->configuredStrategy = $config['strategy']; |
| 39 | + $this->configuredVCSHandler = $config['vcs']['handler']; |
| 40 | + |
| 41 | + $configurator->import(\dirname(__DIR__).'/config/services.php'); |
| 42 | + |
| 43 | + $filepath = $container->getParameterBag()->resolveValue($config['filepath']); |
| 44 | + $file = $filepath.\DIRECTORY_SEPARATOR.$config['filename'].'.'.$config['format']; |
| 45 | + |
| 46 | + $container->fileExists($file); // todo |
| 47 | + |
| 48 | + $configurator->import($file, $config['format'], 'not_found'); |
| 49 | + |
| 50 | + $container->registerForAutoconfiguration(StrategyInterface::class) |
| 51 | + ->addTag('bizkit_versioning.strategy'); |
| 52 | + |
| 53 | + $container->registerForAutoconfiguration(VCSHandlerInterface::class) |
| 54 | + ->addTag('bizkit_versioning.vcs_handler'); |
| 55 | + |
| 56 | + $container->setParameter('.bizkit_versioning.parameter_prefix', $config['parameter_prefix']); |
| 57 | + $container->setParameter('.bizkit_versioning.file', $file); |
| 58 | + |
| 59 | + $container->setParameter('.bizkit_versioning.vcs_commit_message', $config['vcs']['commit_message']); |
| 60 | + $container->setParameter('.bizkit_versioning.vcs_tag_message', $config['vcs']['tag_message']); |
| 61 | + $container->setParameter('.bizkit_versioning.vcs_tagging_mode', $config['vcs']['tagging_mode']); |
| 62 | + $container->setParameter('.bizkit_versioning.vcs_name', $config['vcs']['name']); |
| 63 | + $container->setParameter('.bizkit_versioning.vcs_email', $config['vcs']['email']); |
| 64 | + $container->setParameter('.bizkit_versioning.path_to_vcs_executable', $config['vcs']['path_to_executable']); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Needs to happen after {@see ResolveInstanceofConditionalsPass} & {@see ResolveClassPass}. |
| 69 | + */ |
| 70 | + public function process(ContainerBuilder $container): void |
12 | 71 | { |
13 | | - return \dirname(__DIR__); |
| 72 | + $this->registerServiceAlias($container, 'bizkit_versioning.reader', 'format', $this->configuredFormat, ReaderInterface::class); |
| 73 | + $this->registerServiceAlias($container, 'bizkit_versioning.writer', 'format', $this->configuredFormat, WriterInterface::class); |
| 74 | + |
| 75 | + $this->registerServiceAlias($container, 'bizkit_versioning.strategy', 'alias', $this->configuredStrategy, StrategyInterface::class, true); |
| 76 | + |
| 77 | + if (null !== $this->configuredVCSHandler) { |
| 78 | + $this->registerServiceAlias($container, 'bizkit_versioning.vcs_handler', 'alias', $this->configuredVCSHandler, VCSHandlerInterface::class, true); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private function registerServiceAlias( |
| 83 | + ContainerBuilder $container, |
| 84 | + string $tag, |
| 85 | + string $attribute, |
| 86 | + string $configuredValue, |
| 87 | + string $alias, |
| 88 | + bool $fallbackToServiceId = false, |
| 89 | + ): void { |
| 90 | + $taggedServices = $container->findTaggedServiceIds($tag); |
| 91 | + |
| 92 | + foreach ($taggedServices as $id => $tags) { |
| 93 | + $value = $tags[0][$attribute] ?? ($fallbackToServiceId ? $id : null); |
| 94 | + |
| 95 | + if ($configuredValue === $value) { |
| 96 | + $container->setAlias($alias, new Alias($id, false)); |
| 97 | + |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + throw new InvalidArgumentException(\sprintf( |
| 103 | + 'Unknown configuration value "%s", there is no service with the tag "%s" and attribute "%s" with that value registered.', |
| 104 | + $configuredValue, |
| 105 | + $tag, |
| 106 | + $attribute, |
| 107 | + )); |
14 | 108 | } |
15 | 109 | } |
0 commit comments