Skip to content

Commit

Permalink
Add injects into migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
jankonas authored and f3l1x committed Feb 4, 2022
1 parent 6188b72 commit da00c34
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 13 deletions.
32 changes: 20 additions & 12 deletions src/DI/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
use Doctrine\Migrations\Configuration\Migration\ExistingConfiguration;
use Doctrine\Migrations\DependencyFactory as MigrationsDependencyFactory;
use Doctrine\Migrations\Version\MigrationFactory;
use Doctrine\ORM\EntityManagerInterface;
use Nette\DI\ServiceCreationException;
use Nettrine\Migrations\Version\DbalMigrationFactory;
use Psr\Log\LoggerInterface;

class DependencyFactory
Expand All @@ -27,31 +29,37 @@ class DependencyFactory
/** @var LoggerInterface|null */
private $logger;

public function __construct(Configuration $configuration, ?Connection $connection = null, ?EntityManagerInterface $entityManager = null, ?LoggerInterface $logger = null)
/** @var DbalMigrationFactory */
private $migrationFactory;

public function __construct(Configuration $configuration, DbalMigrationFactory $migrationFactory, ?Connection $connection = null, ?EntityManagerInterface $entityManager = null, ?LoggerInterface $logger = null)
{
$this->configuration = $configuration;
$this->connection = $connection;
$this->entityManager = $entityManager;
$this->logger = $logger;
$this->migrationFactory = $migrationFactory;
}

public function createDependencyFactory(): MigrationsDependencyFactory
{
if ($this->entityManager !== null) {
return MigrationsDependencyFactory::fromEntityManager(new ExistingConfiguration($this->configuration), new ExistingEntityManager($this->entityManager), $this->logger);
$dependencyFactory = MigrationsDependencyFactory::fromEntityManager(new ExistingConfiguration($this->configuration), new ExistingEntityManager($this->entityManager), $this->logger);
} elseif ($this->connection !== null) {
$dependencyFactory = MigrationsDependencyFactory::fromConnection(new ExistingConfiguration($this->configuration), new ExistingConnection($this->connection), $this->logger);
} else {
throw new ServiceCreationException(
sprintf(
'Either service of type %s or %s needs to be registered for Doctrine migrations to work properly',
Connection::class,
EntityManagerInterface::class
)
);
}

if ($this->connection !== null) {
return MigrationsDependencyFactory::fromConnection(new ExistingConfiguration($this->configuration), new ExistingConnection($this->connection), $this->logger);
}
$dependencyFactory->setService(MigrationFactory::class, $this->migrationFactory);

throw new ServiceCreationException(
sprintf(
'Either service of type %s or %s needs to be registered for Doctrine migrations to work properly',
Connection::class,
EntityManagerInterface::class
)
);
return $dependencyFactory;
}

}
4 changes: 4 additions & 0 deletions src/DI/MigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Nette\DI\CompilerExtension;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nettrine\Migrations\Version\DbalMigrationFactory;
use stdClass;

/**
Expand Down Expand Up @@ -64,6 +65,9 @@ public function loadConfiguration(): void
$configuration->addSetup('setMigrationsAreOrganizedByYearAndMonth');
}

$builder->addDefinition($this->prefix('migrationFactory'))
->setFactory(DbalMigrationFactory::class);

$builder->addDefinition($this->prefix('nettrineDependencyFactory'))
->setFactory(DependencyFactory::class)
->setAutowired(false);
Expand Down
47 changes: 47 additions & 0 deletions src/Version/DbalMigrationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types = 1);

namespace Nettrine\Migrations\Version;

use Doctrine\DBAL\Connection;
use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\Version\MigrationFactory;
use Nette\DI\Container;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class DbalMigrationFactory implements MigrationFactory
{

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

/** @var Connection */
private $connection;

/** @var LoggerInterface */
private $logger;

public function __construct(Container $container, Connection $connection, ?LoggerInterface $logger = null)
{
$this->container = $container;
$this->connection = $connection;
if ($logger === null) {
$this->logger = new NullLogger();
} else {
$this->logger = $logger;
}
}

public function createVersion(string $migrationClassName): AbstractMigration
{
$migration = new $migrationClassName(
$this->connection,
$this->logger
);

$this->container->callInjects($migration);

return $migration;
}

}
6 changes: 5 additions & 1 deletion tests/cases/Unit/DI/MigrationsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nettrine\Migrations\DI\MigrationsExtension;
use Nettrine\Migrations\Version\DbalMigrationFactory;
use Symfony\Component\Console\Application;
use Tester\Assert;
use Tester\TestCase;
Expand Down Expand Up @@ -116,7 +117,10 @@ public function testDependencyFactory(): void
/** @var Container $container */
$container = new $class();

Assert::type(DependencyFactory::class, $container->getByType(DependencyFactory::class));
/** @var DependencyFactory $dependencyFactory */
$dependencyFactory = $container->getByType(DependencyFactory::class);
Assert::type(DependencyFactory::class, $dependencyFactory);
Assert::type(DbalMigrationFactory::class, $dependencyFactory->getMigrationFactory());
}

}
Expand Down

0 comments on commit da00c34

Please sign in to comment.