Skip to content

Commit 4450c0f

Browse files
author
Julien Neuhart
authored
Merge pull request #21 from homersimpsons/fix/multiple-connections-code-generator-di
Multiple Connections: Correctly inject code generators
2 parents 59848d6 + 38d0cc7 commit 4450c0f

File tree

5 files changed

+169
-63
lines changed

5 files changed

+169
-63
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
name: "Continuous Integration"
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- master
8+
9+
jobs:
10+
phpunit:
11+
name: "PHPUnit (with MySQL)"
12+
runs-on: "ubuntu-latest"
13+
14+
strategy:
15+
matrix:
16+
php-version:
17+
- "7.2"
18+
- "7.3"
19+
- "7.4"
20+
21+
services:
22+
mysql:
23+
image: "mysql:5.7"
24+
env:
25+
MYSQL_ALLOW_EMPTY_PASSWORD: true
26+
MYSQL_ROOT_PASSWORD:
27+
ports:
28+
- "3306:3306"
29+
30+
steps:
31+
- name: "Checkout"
32+
uses: "actions/checkout@v2"
33+
with:
34+
fetch-depth: 2
35+
36+
- name: "Install PHP"
37+
uses: "shivammathur/setup-php@v2"
38+
with:
39+
php-version: "${{ matrix.php-version }}"
40+
extensions: ""
41+
coverage: "pcov"
42+
tools: "cs2pr"
43+
44+
- name: "Cache dependencies installed with composer"
45+
uses: "actions/cache@v1"
46+
with:
47+
path: "~/.composer/cache"
48+
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
49+
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"
50+
51+
- name: "Install dependencies with composer"
52+
run: "composer install --no-interaction --no-progress --no-suggest"
53+
54+
- name: "Run PHPUnit"
55+
run: "vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover=coverage.xml"
56+
57+
- name: "Upload Code Coverage"
58+
if: ${{ matrix.php-version == '7.4' }}
59+
uses: "codecov/codecov-action@v1"
60+
61+
- name: "Run a static analysis with phpstan/phpstan"
62+
run: "composer phpstan -- --error-format=checkstyle | cs2pr"
63+
64+
phpunit-prefer-lowest:
65+
name: "PHPUnit with prefer-lowest"
66+
runs-on: "ubuntu-latest"
67+
68+
strategy:
69+
matrix:
70+
php-version:
71+
- "7.2"
72+
- "7.3"
73+
- "7.4"
74+
75+
services:
76+
mysql:
77+
image: "mysql:8"
78+
env:
79+
MYSQL_ALLOW_EMPTY_PASSWORD: true
80+
MYSQL_ROOT_PASSWORD:
81+
ports:
82+
- "3306:3306"
83+
84+
steps:
85+
- name: "Checkout"
86+
uses: "actions/checkout@v2"
87+
with:
88+
fetch-depth: 2
89+
90+
- name: "Install PHP"
91+
uses: "shivammathur/setup-php@v2"
92+
with:
93+
php-version: "${{ matrix.php-version }}"
94+
extensions: ""
95+
coverage: "pcov"
96+
97+
- name: "Cache dependencies installed with composer"
98+
uses: "actions/cache@v1"
99+
with:
100+
path: "~/.composer/cache"
101+
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
102+
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"
103+
104+
- name: "Install dependencies with composer"
105+
run: "composer update --no-interaction --no-progress --no-suggest --prefer-lowest"
106+
107+
- name: "Run PHPUnit"
108+
run: "vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover=coverage.xml"

.travis.yml

Lines changed: 0 additions & 60 deletions
This file was deleted.
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)