Skip to content

Commit

Permalink
Add service migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Jan 21, 2024
1 parent 1dd4290 commit ea5caf6
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 3 deletions.
24 changes: 24 additions & 0 deletions AbstractServiceMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle;

use Doctrine\DBAL\Connection;
use Doctrine\Migrations\AbstractMigration;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

abstract class AbstractServiceMigration extends AbstractMigration implements ServiceSubscriberInterface
{
/** @var ContainerInterface */
protected $container;

final public function __construct(Connection $connection, LoggerInterface $logger, ContainerInterface $container)
{
parent::__construct($connection, $logger);

Check warning on line 20 in AbstractServiceMigration.php

View check run for this annotation

Codecov / codecov/patch

AbstractServiceMigration.php#L18-L20

Added lines #L18 - L20 were not covered by tests

$this->container = $container;
}

Check warning on line 23 in AbstractServiceMigration.php

View check run for this annotation

Codecov / codecov/patch

AbstractServiceMigration.php#L22-L23

Added lines #L22 - L23 were not covered by tests
}
44 changes: 44 additions & 0 deletions DependencyInjection/CompilerPass/RegisterMigrationsPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection\CompilerPass;

use Doctrine\Bundle\MigrationsBundle\AbstractServiceMigration;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\TypedReference;

use function is_subclass_of;

class RegisterMigrationsPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
$migrationRefs = [];

foreach ($container->findTaggedServiceIds('doctrine_migrations.migration', true) as $id => $attributes) {
$class = $container->getDefinition($id)->getClass();
if (is_subclass_of($class, AbstractServiceMigration::class)) {
$definition = new ChildDefinition('doctrine_migrations.abstract_migration');
$definition->setClass($class);
$definition->addTag('container.service_subscriber');

Check warning on line 27 in DependencyInjection/CompilerPass/RegisterMigrationsPass.php

View check run for this annotation

Codecov / codecov/patch

DependencyInjection/CompilerPass/RegisterMigrationsPass.php#L23-L27

Added lines #L23 - L27 were not covered by tests

$container->setDefinition($id, $definition);

Check warning on line 29 in DependencyInjection/CompilerPass/RegisterMigrationsPass.php

View check run for this annotation

Codecov / codecov/patch

DependencyInjection/CompilerPass/RegisterMigrationsPass.php#L29

Added line #L29 was not covered by tests

$migrationRefs[$id] = new TypedReference($id, $class);

Check warning on line 31 in DependencyInjection/CompilerPass/RegisterMigrationsPass.php

View check run for this annotation

Codecov / codecov/patch

DependencyInjection/CompilerPass/RegisterMigrationsPass.php#L31

Added line #L31 was not covered by tests
} else {
$container->removeDefinition($id);

Check warning on line 33 in DependencyInjection/CompilerPass/RegisterMigrationsPass.php

View check run for this annotation

Codecov / codecov/patch

DependencyInjection/CompilerPass/RegisterMigrationsPass.php#L33

Added line #L33 was not covered by tests
}
}

if ($migrationRefs !== []) {
$container->getDefinition('doctrine.migrations.service_migrations_factory')
->replaceArgument(1, new ServiceLocatorArgument($migrationRefs));

Check warning on line 39 in DependencyInjection/CompilerPass/RegisterMigrationsPass.php

View check run for this annotation

Codecov / codecov/patch

DependencyInjection/CompilerPass/RegisterMigrationsPass.php#L38-L39

Added lines #L38 - L39 were not covered by tests
} else {
$container->removeDefinition('doctrine.migrations.service_migrations_factory');
}
}
}
4 changes: 4 additions & 0 deletions DependencyInjection/DoctrineMigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Bundle\MigrationsBundle\Collector\MigrationsCollector;
use Doctrine\Bundle\MigrationsBundle\Collector\MigrationsFlattener;
use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\Version\MigrationFactory;
Expand Down Expand Up @@ -52,6 +53,9 @@ public function load(array $configs, ContainerBuilder $container): void

$loader->load('services.xml');

$container->registerForAutoconfiguration(AbstractMigration::class)
->addTag('doctrine_migrations.migration');

$configurationDefinition = $container->getDefinition('doctrine.migrations.configuration');

foreach ($config['migrations_paths'] as $ns => $path) {
Expand Down
9 changes: 6 additions & 3 deletions DoctrineMigrationsBundle.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle;

use Doctrine\Bundle\MigrationsBundle\DependencyInjection\CompilerPass\ConfigureDependencyFactoryPass;
use Doctrine\Bundle\MigrationsBundle\DependencyInjection\CompilerPass\RegisterMigrationsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -14,9 +17,9 @@
*/
class DoctrineMigrationsBundle extends Bundle
{
/** @return void */
public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new ConfigureDependencyFactoryPass());
$container->addCompilerPass(new ConfigureDependencyFactoryPass());
$container->addCompilerPass(new RegisterMigrationsPass());
}
}
33 changes: 33 additions & 0 deletions MigrationsFactory/ServiceMigrationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\MigrationsFactory;

use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\Version\MigrationFactory;
use Psr\Container\ContainerInterface;

class ServiceMigrationFactory implements MigrationFactory
{
/** @var MigrationFactory */
private $migrationFactory;

/** @var ContainerInterface */
private $container;

public function __construct(MigrationFactory $migrationFactory, ContainerInterface $container)
{
$this->migrationFactory = $migrationFactory;
$this->container = $container;
}

Check warning on line 23 in MigrationsFactory/ServiceMigrationFactory.php

View check run for this annotation

Codecov / codecov/patch

MigrationsFactory/ServiceMigrationFactory.php#L19-L23

Added lines #L19 - L23 were not covered by tests

public function createVersion(string $migrationClassName): AbstractMigration
{
if ($this->container->has($migrationClassName)) {
return $this->container->get($migrationClassName);
}

Check warning on line 29 in MigrationsFactory/ServiceMigrationFactory.php

View check run for this annotation

Codecov / codecov/patch

MigrationsFactory/ServiceMigrationFactory.php#L25-L29

Added lines #L25 - L29 were not covered by tests

return $this->migrationFactory->createVersion($migrationClassName);
}

Check warning on line 32 in MigrationsFactory/ServiceMigrationFactory.php

View check run for this annotation

Codecov / codecov/patch

MigrationsFactory/ServiceMigrationFactory.php#L31-L32

Added lines #L31 - L32 were not covered by tests
}
24 changes: 24 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<factory service="doctrine.migrations.dependency_factory" method="getMigrationFactory"/>
</service>

<service id="doctrine.migrations.service_migrations_factory"
class="Doctrine\Bundle\MigrationsBundle\MigrationsFactory\ServiceMigrationFactory"
decorates="doctrine.migrations.migrations_factory" decoration-priority="-1"
>
<argument id="doctrine.migrations.service_migrations_factory.inner" type="service" />
<argument type="abstract">migrations locator</argument>
</service>

<service id="doctrine.migrations.container_aware_migrations_factory"
class="Doctrine\Bundle\MigrationsBundle\MigrationsFactory\ContainerAwareMigrationFactory"
decorates="doctrine.migrations.migrations_factory"
Expand Down Expand Up @@ -146,6 +154,22 @@
<tag name="console.command" command="doctrine:migrations:version" />
</service>

<service id="doctrine_migrations.abstract_migration" class="Doctrine\Bundle\MigrationsBundle\AbstractServiceMigration" abstract="true">

<argument type="service">
<service class="Doctrine\DBAL\Connection">
<factory service="doctrine.migrations.dependency_factory" method="getConnection" />
</service>
</argument>
<argument type="service">
<service class="Psr\Log\LoggerInterface">
<factory service="doctrine.migrations.dependency_factory" method="getLogger" />
</service>
</argument>
<argument></argument>

</service>

</services>

</container>

0 comments on commit ea5caf6

Please sign in to comment.