Skip to content

Commit 358b02d

Browse files
committed
Use AbstractBundle
1 parent ff0536b commit 358b02d

4 files changed

Lines changed: 197 additions & 222 deletions

File tree

config/definition.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Config\Definition\Configurator;
6+
7+
use Bizkit\VersioningBundle\VCS\TaggingMode;
8+
use Bizkit\VersioningBundle\VCS\VCSHandlerInterface;
9+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
10+
11+
return static function (DefinitionConfigurator $definition): void {
12+
$definition->rootNode()
13+
->children()
14+
->scalarNode('parameter_prefix')
15+
->info('The prefix added to the version parameters.')
16+
->example('my_app')
17+
->cannotBeEmpty()
18+
->defaultValue('application')
19+
->end()
20+
->scalarNode('strategy')
21+
->info('The versioning strategy used.')
22+
->cannotBeEmpty()
23+
->defaultValue('incrementing')
24+
->end()
25+
26+
->scalarNode('filename')
27+
->info('The name of the file containing the version information.')
28+
->cannotBeEmpty()
29+
->defaultValue('version')
30+
->end()
31+
->scalarNode('filepath')
32+
->info('The path to the file containing the version information.')
33+
->cannotBeEmpty()
34+
->defaultValue('%kernel.project_dir%/config')
35+
->end()
36+
37+
->enumNode('format')
38+
->info('The format used for the version file.')
39+
->values(['yaml'])
40+
->cannotBeEmpty()
41+
->defaultValue('yaml')
42+
->end()
43+
44+
->arrayNode('vcs')
45+
->info("Configuration for the VCS integration,\nset to false to disable the integration.")
46+
->addDefaultsIfNotSet()
47+
->beforeNormalization()
48+
->ifTrue(static fn ($v): bool => \is_bool($v))
49+
->then(static fn (bool $v): array => $v ? [] : ['handler' => null])
50+
->end()
51+
->children()
52+
->scalarNode('handler')
53+
->info("The handler used for the VCS integration,\nset to null to disable the integration.")
54+
->defaultValue('git')
55+
->end()
56+
57+
->scalarNode('commit_message')
58+
->info('The message to use for the VCS commit.')
59+
->cannotBeEmpty()
60+
->defaultValue(VCSHandlerInterface::DEFAULT_MESSAGE)
61+
->end()
62+
->enumNode('tagging_mode')
63+
->beforeNormalization()
64+
->ifString()
65+
->then(static fn ($v) => TaggingMode::tryFrom($v) ?? throw new InvalidConfigurationException(\sprintf(
66+
'Invalid tagging mode provided: expected one of "%s", got "%s".',
67+
implode('", "', array_map(static fn (TaggingMode $case) => $case->value, TaggingMode::cases())),
68+
$v,
69+
)))
70+
->end()
71+
->values(TaggingMode::cases())
72+
->info("The mode for applying tags to version commits:\n- 'always': automatically add a tag without prompting\n- 'never': do not add a tag\n- 'ask': prompt before tagging when incrementing versions")
73+
->cannotBeEmpty()
74+
->defaultValue(TaggingMode::Ask)
75+
->end()
76+
->scalarNode('tag_message')
77+
->info('The message to use for the VCS tag.')
78+
->cannotBeEmpty()
79+
->defaultValue(VCSHandlerInterface::DEFAULT_MESSAGE)
80+
->end()
81+
82+
->scalarNode('name')
83+
->info("The name used for the VCS commit information,\nset to null to use the default VCS configuration.")
84+
->defaultNull()
85+
->end()
86+
->scalarNode('email')
87+
->info("The email used for the VCS commit information,\nset to null to use the default VCS configuration.")
88+
->defaultNull()
89+
->end()
90+
91+
->scalarNode('path_to_executable')
92+
->info("The path to the VCS executable,\nset to null for autodiscovery.")
93+
->defaultNull()
94+
->end()
95+
->end()
96+
->end()
97+
->end()
98+
;
99+
};

src/BizkitVersioningBundle.php

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,106 @@
44

55
namespace Bizkit\VersioningBundle;
66

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;
818

9-
final class BizkitVersioningBundle extends Bundle
19+
final class BizkitVersioningBundle extends AbstractBundle implements CompilerPassInterface
1020
{
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
1271
{
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+
));
14108
}
15109
}

src/DependencyInjection/BizkitVersioningExtension.php

Lines changed: 0 additions & 108 deletions
This file was deleted.

0 commit comments

Comments
 (0)