-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
238 additions
and
3 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
DependencyInjection/CompilerPass/RegisterMigrationsPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\DBAL\Connection; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\DependencyInjection\Argument\BoundArgument; | ||
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\TypedReference; | ||
|
||
class RegisterMigrationsPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
$migrationRefs = []; | ||
|
||
foreach ($container->findTaggedServiceIds('doctrine_migrations.migration', true) as $id => $attributes) { | ||
$definition = $container->getDefinition($id); | ||
$definition->setBindings([ | ||
Connection::class => new BoundArgument(new Reference('doctrine.migrations.connection')), | ||
LoggerInterface::class => new BoundArgument(new Reference('doctrine.migrations.logger')), | ||
]); | ||
|
||
$migrationRefs[$id] = new TypedReference($id, $definition->getClass()); | ||
} | ||
|
||
if ($migrationRefs !== []) { | ||
$container->getDefinition('doctrine.migrations.filter_service_migration_finder') | ||
->replaceArgument(1, new ServiceLocatorArgument($migrationRefs)); | ||
$container->getDefinition('doctrine.migrations.service_migrations_repository') | ||
->replaceArgument(1, new ServiceLocatorArgument($migrationRefs)); | ||
} else { | ||
$container->removeDefinition('doctrine.migrations.connection'); | ||
$container->removeDefinition('doctrine.migrations.logger'); | ||
$container->removeDefinition('doctrine.migrations.filter_service_migration_finder'); | ||
$container->removeDefinition('doctrine.migrations.service_migrations_repository'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Bundle\MigrationsBundle\MigrationFinder; | ||
|
||
use Doctrine\Migrations\Finder\MigrationFinder; | ||
use Psr\Container\ContainerInterface; | ||
|
||
use function array_values; | ||
|
||
final class FilterServiceMigrationFinder implements MigrationFinder | ||
{ | ||
/** @var MigrationFinder */ | ||
private $migrationFinder; | ||
|
||
/** @var ContainerInterface */ | ||
private $container; | ||
|
||
public function __construct(MigrationFinder $migrationFinder, ContainerInterface $container) | ||
{ | ||
$this->migrationFinder = $migrationFinder; | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function findMigrations(string $directory, ?string $namespace = null): array | ||
{ | ||
$migrations = $this->migrationFinder->findMigrations( | ||
$directory, | ||
$namespace | ||
); | ||
|
||
foreach ($migrations as $i => $migration) { | ||
if (! $this->container->has($migration)) { | ||
continue; | ||
} | ||
|
||
unset($migrations[$i]); | ||
} | ||
|
||
return array_values($migrations); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Bundle\MigrationsBundle\MigrationsRepository; | ||
|
||
use Doctrine\Migrations\AbstractMigration; | ||
use Doctrine\Migrations\Exception\DuplicateMigrationVersion; | ||
use Doctrine\Migrations\Metadata\AvailableMigration; | ||
use Doctrine\Migrations\Metadata\AvailableMigrationsSet; | ||
use Doctrine\Migrations\MigrationsRepository; | ||
use Doctrine\Migrations\Version\Version; | ||
use Symfony\Contracts\Service\ServiceProviderInterface; | ||
|
||
use function assert; | ||
|
||
class ServiceMigrationsRepository implements MigrationsRepository | ||
{ | ||
/** @var MigrationsRepository */ | ||
private $migrationRepository; | ||
|
||
/** @var ServiceProviderInterface */ | ||
private $container; | ||
|
||
/** @var AvailableMigration[] */ | ||
private $migrations = []; | ||
|
||
public function __construct( | ||
MigrationsRepository $migrationRepository, | ||
ServiceProviderInterface $container | ||
) { | ||
$this->migrationRepository = $migrationRepository; | ||
$this->container = $container; | ||
} | ||
|
||
public function hasMigration(string $version): bool | ||
{ | ||
return $this->container->has($version) || $this->migrationRepository->hasMigration($version); | ||
} | ||
|
||
public function getMigration(Version $version): AvailableMigration | ||
{ | ||
if (! isset($this->migrations[(string) $version]) && ! $this->loadMigrationFromContainer($version)) { | ||
return $this->migrationRepository->getMigration($version); | ||
} | ||
|
||
return $this->migrations[(string) $version]; | ||
} | ||
|
||
/** | ||
* Returns a non-sorted set of migrations. | ||
*/ | ||
public function getMigrations(): AvailableMigrationsSet | ||
{ | ||
$this->loadMigrationsFromContainer(); | ||
|
||
$migrations = $this->migrations; | ||
foreach ($this->migrationRepository->getMigrations()->getItems() as $availableMigration) { | ||
$version = $availableMigration->getVersion(); | ||
|
||
if (isset($migrations[(string) $version])) { | ||
throw DuplicateMigrationVersion::new( | ||
(string) $version, | ||
(string) $version | ||
); | ||
} | ||
|
||
$migrations[(string) $version] = $availableMigration; | ||
} | ||
|
||
return new AvailableMigrationsSet($migrations); | ||
} | ||
|
||
private function loadMigrationsFromContainer(): void | ||
{ | ||
foreach ($this->container->getProvidedServices() as $id) { | ||
if (isset($this->migrations[$id])) { | ||
continue; | ||
} | ||
|
||
$this->loadMigrationFromContainer(new Version($id)); | ||
} | ||
} | ||
|
||
private function loadMigrationFromContainer(Version $version): bool | ||
{ | ||
if (! $this->container->has((string) $version)) { | ||
return false; | ||
} | ||
|
||
$migration = $this->container->get((string) $version); | ||
assert($migration instanceof AbstractMigration); | ||
|
||
$this->migrations[(string) $version] = new AvailableMigration($version, $migration); | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters