diff --git a/migrations/Version20221114193325.php b/migrations/Version20221114193325.php index 9766ccf3..bc2a97fa 100644 --- a/migrations/Version20221114193325.php +++ b/migrations/Version20221114193325.php @@ -4,18 +4,15 @@ declare(strict_types=1); namespace DoctrineMigrations; +use App\Doctrine\Migration\ContainerAwareMigrationInterface; use App\Migration\AbstractMultiPlatformMigration; use App\Migration\WithPermPresetsTrait; use App\Services\UserSystem\PermissionPresetsHelper; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Schema\Schema; use Psr\Log\LoggerInterface; -use Symfony\Component\DependencyInjection\ContainerAwareInterface; -/** - * Auto-generated Migration: Please modify to your needs! - */ -final class Version20221114193325 extends AbstractMultiPlatformMigration implements ContainerAwareInterface +final class Version20221114193325 extends AbstractMultiPlatformMigration implements ContainerAwareMigrationInterface { use WithPermPresetsTrait; diff --git a/src/Doctrine/Migration/ContainerAwareMigrationFactory.php b/src/Doctrine/Migration/ContainerAwareMigrationFactory.php new file mode 100644 index 00000000..9ce8e077 --- /dev/null +++ b/src/Doctrine/Migration/ContainerAwareMigrationFactory.php @@ -0,0 +1,55 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Doctrine\Migration; + +use App\Services\UserSystem\PermissionPresetsHelper; +use Doctrine\Migrations\AbstractMigration; +use Doctrine\Migrations\Version\MigrationFactory; +use Symfony\Component\DependencyInjection\Attribute\AsDecorator; +use Symfony\Component\DependencyInjection\Attribute\AutowireLocator; +use Symfony\Component\DependencyInjection\ContainerInterface; + +#[AsDecorator("doctrine.migrations.migrations_factory")] +class ContainerAwareMigrationFactory implements MigrationFactory +{ + public function __construct(private readonly MigrationFactory $decorated, + //List all services that should be available in migrations here + #[AutowireLocator([ + PermissionPresetsHelper::class + ])] + private readonly ContainerInterface $container) + { + } + + public function createVersion(string $migrationClassName): AbstractMigration + { + $migration = $this->decorated->createVersion($migrationClassName); + + if ($migration instanceof ContainerAwareMigrationInterface) { + $migration->setContainer($this->container); + } + + return $migration; + } +} \ No newline at end of file diff --git a/src/Doctrine/Migration/ContainerAwareMigrationInterface.php b/src/Doctrine/Migration/ContainerAwareMigrationInterface.php new file mode 100644 index 00000000..5fd914ba --- /dev/null +++ b/src/Doctrine/Migration/ContainerAwareMigrationInterface.php @@ -0,0 +1,31 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Doctrine\Migration; + +use Symfony\Component\DependencyInjection\ContainerInterface; + +interface ContainerAwareMigrationInterface +{ + public function setContainer(?ContainerInterface $container = null): void; +} \ No newline at end of file