Skip to content

Commit

Permalink
add service factories
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed May 11, 2020
1 parent 7376b1c commit 5435638
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
15 changes: 15 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ public function getConfigTreeBuilder() : TreeBuilder
->prototype('scalar')->end()
->end()

->arrayNode('factories')
->info('A set of callables to pass to the underlying doctrine/migrations library as services, allowing to change its behaviour.')
->useAttributeAsKey('factory')
->defaultValue([])
->validate()
->ifTrue(static function ($v) {
return count(array_filter(array_keys($v), static function (string $doctrineService) : bool {
return strpos($doctrineService, 'Doctrine\Migrations\\') !==0;
}));
})
->thenInvalid('Valid callables for the DoctrineMigrationsBundle must be in the "Doctrine\Migrations" namespace.')
->end()
->prototype('scalar')->end()
->end()

->arrayNode('storage')
->addDefaultsIfNotSet()
->info('Storage to use for migration status metadata.')
Expand Down
4 changes: 4 additions & 0 deletions DependencyInjection/DoctrineMigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function load(array $configs, ContainerBuilder $container) : void
$diDefinition->addMethodCall('setDefinition', [$doctrineId, new ServiceClosureArgument(new Reference($symfonyId))]);
}

foreach ($config['factories'] as $doctrineId => $symfonyId) {
$diDefinition->addMethodCall('setDefinition', [$doctrineId, new Reference($symfonyId)]);
}

if (! isset($config['services'][MetadataStorage::class])) {
$storageConfiguration = $config['storage']['table_storage'];

Expand Down
15 changes: 15 additions & 0 deletions Resources/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ application:
# Custom migration classes factory
'Doctrine\Migrations\Version\MigrationFactory': ~
factories:
# Custom migration sorting service id via callables (MyCallableFactory must be a callable)
'Doctrine\Migrations\Version\Comparator': 'MyCallableFactory'
- The ``services`` node allows you to provide custom services to the underlying ``DependencyFactory`` part
of ``doctrine/migrations``.

- The node ``factories`` is similar to ``services``, with the difference that it accepts only callables.
The provided callable must return the service to be passed to the ``DependencyFactory``.
The callable will receive as first argument the ``DependencyFactory`` itself,
allowing you to fetch other dependencies from the factory while instantiating your custom dependencies.

Usage
-----

Expand Down
35 changes: 35 additions & 0 deletions Tests/DependencyInjection/DoctrineMigrationsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,41 @@ public function __invoke() : void
self::assertInstanceOf(DependencyFactory::class, $di);
}

public function testServiceFactory() : void
{
$mockComparator = $this->createMock(Comparator::class);
$config = [
'factories' => [Comparator::class => 'my_sorter'],
];

$container = $this->getContainer($config);

$conn = $this->createMock(Connection::class);
$container->set('doctrine.dbal.default_connection', $conn);

$sorterFactory = new class($mockComparator) {
/** @var Comparator */
private $comparator;

public function __construct(Comparator $comparator)
{
$this->comparator = $comparator;
}

public function __invoke(DependencyFactory $di) : Comparator
{
return $this->comparator;
}
};
$container->set('my_sorter', $sorterFactory);

$container->compile();

$di = $container->get('doctrine.migrations.dependency_factory');
self::assertInstanceOf(DependencyFactory::class, $di);
self::assertSame($mockComparator, $di->getVersionComparator());
}

public function testCustomConnection() : void
{
$config = [
Expand Down

0 comments on commit 5435638

Please sign in to comment.