Skip to content

Commit 0983c37

Browse files
committed
Multiple Connections: Correctly inject code generators
Technically we do use tags and interface implementations to do this automatically
1 parent 59848d6 commit 0983c37

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TheCodingMachine\TDBM\Bundle\DependencyInjection;
6+
7+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\DependencyInjection\Reference;
10+
11+
class TdbmCompilerPass implements CompilerPassInterface
12+
{
13+
14+
/**
15+
* @inheritDoc
16+
*/
17+
public function process(ContainerBuilder $container)
18+
{
19+
$generatorListeners = $this->taggedServicesToReferences($container->findTaggedServiceIds(TdbmExtension::TAG_GENERATOR_LISTENER));
20+
$codeGeneratorListeners = $this->taggedServicesToReferences($container->findTaggedServiceIds(TdbmExtension::TAG_CODE_GENERATOR_LISTENER));
21+
22+
$tdbmConfigurations = array_keys($container->findTaggedServiceIds(TdbmExtension::TAG_TDBM_CONFIGURATION));
23+
24+
foreach ($tdbmConfigurations as $tdbmConfiguration) {
25+
$configuration = $container->getDefinition($tdbmConfiguration);
26+
$configuration->setArgument('$generatorListeners', $generatorListeners);
27+
$configuration->setArgument('$codeGeneratorListeners', $codeGeneratorListeners);
28+
}
29+
}
30+
31+
/**
32+
* @param array<string, array> $taggedServices Keys are services ids, this is the output of `ContainerBuilder::findTaggedServiceIds`
33+
* @return array<Reference>
34+
*/
35+
private function taggedServicesToReferences(array $taggedServices): array
36+
{
37+
return array_map(static function (string $serviceId) {
38+
return new Reference($serviceId);
39+
}, array_keys($taggedServices));
40+
}
41+
}

DependencyInjection/TdbmExtension.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
use TheCodingMachine\TDBM\Schema\LockFileSchemaManager;
2626
use TheCodingMachine\TDBM\TDBMService;
2727
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;
28+
use TheCodingMachine\TDBM\Utils\CodeGeneratorListenerInterface;
2829
use TheCodingMachine\TDBM\Utils\DefaultNamingStrategy;
30+
use TheCodingMachine\TDBM\Utils\GeneratorListenerInterface;
2931
use TheCodingMachine\TDBM\Utils\NamingStrategyInterface;
3032
use TheCodingMachine\TDBM\SchemaLockFileDumper;
3133
use TheCodingMachine\TDBM\Utils\RootProjectLocator;
@@ -39,6 +41,10 @@
3941

4042
class TdbmExtension extends Extension
4143
{
44+
public const TAG_GENERATOR_LISTENER = 'tdbm.generatorListener';
45+
public const TAG_CODE_GENERATOR_LISTENER = 'tdbm.codeGeneratorListener';
46+
public const TAG_TDBM_CONFIGURATION = 'tdbm.configuration';
47+
4248
private const DEFAULT_CONFIGURATION_ID = TDBMConfiguration::class;
4349
private const DEFAULT_NAMING_STRATEGY_ID = DefaultNamingStrategy::class;
4450

@@ -51,6 +57,9 @@ class TdbmExtension extends Extension
5157
*/
5258
public function load(array $configs, ContainerBuilder $container): void
5359
{
60+
$container->registerForAutoconfiguration(GeneratorListenerInterface::class)->addTag(self::TAG_GENERATOR_LISTENER);
61+
$container->registerForAutoconfiguration(CodeGeneratorListenerInterface::class)->addTag(self::TAG_CODE_GENERATOR_LISTENER);
62+
5463
$configuration = new Configuration();
5564
$processedConfig = $this->processConfiguration($configuration, $configs);
5665

@@ -138,8 +147,8 @@ private function getConfigurationDefinition(ConnectionConfiguration $config, str
138147
$configuration->setArgument(1, $config->getDaoNamespace());
139148
$configuration->setArgument('$connection', new Reference($config->getConnection()));
140149
$configuration->setArgument('$namingStrategy', new Reference($namingStrategyServiceId));
141-
$configuration->setArgument('$codeGeneratorListeners', [new Reference(SymfonyCodeGeneratorListener::class)]);
142150
$configuration->setArgument('$cache', new Reference('tdbm.cache'));
151+
$configuration->addTag(self::TAG_TDBM_CONFIGURATION);
143152

144153
// Let's name the tdbm lock file after the name of the DBAL connection.
145154

TdbmBundle.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
<?php
22

3+
declare(strict_types=1);
34

45
namespace TheCodingMachine\TDBM\Bundle;
56

6-
7+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
79
use Symfony\Component\HttpKernel\Bundle\Bundle;
10+
use TheCodingMachine\TDBM\Bundle\DependencyInjection\TdbmCompilerPass;
811

912
class TdbmBundle extends Bundle
1013
{
14+
public function build(ContainerBuilder $container)
15+
{
16+
parent::build($container);
1117

12-
}
18+
$container->addCompilerPass(new TdbmCompilerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 1);
19+
}
20+
}

0 commit comments

Comments
 (0)