Skip to content

Commit 7376b1c

Browse files
committed
Allow custom services to be lazy
1 parent 0e1800a commit 7376b1c

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

DependencyInjection/DoctrineMigrationsExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use InvalidArgumentException;
1010
use RuntimeException;
1111
use Symfony\Component\Config\FileLocator;
12+
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1213
use Symfony\Component\DependencyInjection\ContainerBuilder;
1314
use Symfony\Component\DependencyInjection\Definition;
1415
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -67,7 +68,7 @@ public function load(array $configs, ContainerBuilder $container) : void
6768
$diDefinition = $container->getDefinition('doctrine.migrations.dependency_factory');
6869

6970
foreach ($config['services'] as $doctrineId => $symfonyId) {
70-
$diDefinition->addMethodCall('setService', [$doctrineId, new Reference($symfonyId)]);
71+
$diDefinition->addMethodCall('setDefinition', [$doctrineId, new ServiceClosureArgument(new Reference($symfonyId))]);
7172
}
7273

7374
if (! isset($config['services'][MetadataStorage::class])) {

Tests/DependencyInjection/DoctrineMigrationsExtensionTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\Migrations\Version\Comparator;
1717
use Doctrine\Migrations\Version\Version;
1818
use Doctrine\ORM\EntityManager;
19+
use Exception;
1920
use InvalidArgumentException;
2021
use PHPUnit\Framework\TestCase;
2122
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -25,6 +26,7 @@
2526
use Symfony\Component\DependencyInjection\Definition;
2627
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2728
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
29+
use Symfony\Component\DependencyInjection\Reference;
2830
use function assert;
2931
use function method_exists;
3032
use function sys_get_temp_dir;
@@ -175,6 +177,34 @@ public function compare(Version $a, Version $b) : int
175177
self::assertSame($sorter, $di->getVersionComparator());
176178
}
177179

180+
public function testServicesAreLazy() : void
181+
{
182+
$config = [
183+
'services' => [Comparator::class => 'my_sorter'],
184+
];
185+
$container = $this->getContainer($config);
186+
187+
$conn = $this->createMock(Connection::class);
188+
$container->set('doctrine.dbal.default_connection', $conn);
189+
190+
$sorterFactory = new class() {
191+
public function __invoke() : void
192+
{
193+
throw new Exception('This method should not be invoked.');
194+
}
195+
};
196+
$container->set('my_sorter_factory', $sorterFactory);
197+
198+
$sorterDefinition = new Definition(Comparator::class);
199+
$sorterDefinition->setFactory(new Reference('my_sorter_factory'));
200+
$container->setDefinition('my_sorter', $sorterDefinition);
201+
202+
$container->compile();
203+
204+
$di = $container->get('doctrine.migrations.dependency_factory');
205+
self::assertInstanceOf(DependencyFactory::class, $di);
206+
}
207+
178208
public function testCustomConnection() : void
179209
{
180210
$config = [

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
"autoload-dev": {
4444
"psr-4": { "Doctrine\\Bundle\\MigrationsBundle\\Tests\\": "Tests" }
4545
},
46+
"config": {
47+
"platform": {
48+
"php": "7.2.5"
49+
}
50+
},
4651
"extra": {
4752
"branch-alias": {
4853
"dev-master": "3.0.x-dev"

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)