-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DependencyFactory: refactoring, add more features
- Loading branch information
Showing
13 changed files
with
279 additions
and
111 deletions.
There are no files selected for viewing
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,22 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nettrine\Migrations\DI\Helpers; | ||
|
||
use Nette\DI\Definitions\Statement; | ||
use Nettrine\Migrations\Exceptions\LogicalException; | ||
|
||
final class SmartStatement | ||
{ | ||
|
||
public static function from(mixed $service): Statement | ||
{ | ||
if (is_string($service)) { | ||
return new Statement($service); | ||
} elseif ($service instanceof Statement) { | ||
return $service; | ||
} else { | ||
throw new LogicalException('Unsupported type of service'); | ||
} | ||
} | ||
|
||
} |
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 was deleted.
Oops, something went wrong.
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,76 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nettrine\Migrations; | ||
|
||
use Doctrine\Migrations\AbstractMigration; | ||
use Doctrine\Migrations\Configuration\Configuration; | ||
use Doctrine\Migrations\Configuration\Connection\ConnectionRegistryConnection; | ||
use Doctrine\Migrations\Configuration\EntityManager\ManagerRegistryEntityManager; | ||
use Doctrine\Migrations\Configuration\Migration\ExistingConfiguration; | ||
use Doctrine\Migrations\DependencyFactory; | ||
use Doctrine\Migrations\Version\MigrationFactory; | ||
use Doctrine\Persistence\ConnectionRegistry; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use Nette\DI\Container; | ||
use Nettrine\Migrations\Exceptions\LogicalException; | ||
use Nettrine\Migrations\Migration\MigrationFactoryDecorator; | ||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
|
||
final class DependencyFactoryCreator | ||
{ | ||
|
||
public static function create( | ||
Container $container, | ||
Configuration $configuration, | ||
?ConnectionRegistry $connectionRegistry = null, | ||
?ManagerRegistry $managerRegistry = null, | ||
?LoggerInterface $logger = null | ||
): DependencyFactory | ||
{ | ||
$logger ??= new NullLogger(); | ||
|
||
if ($managerRegistry !== null) { | ||
$dependencyFactory = DependencyFactory::fromEntityManager( | ||
new ExistingConfiguration($configuration), | ||
ManagerRegistryEntityManager::withSimpleDefault($managerRegistry), | ||
$logger | ||
); | ||
} elseif ($connectionRegistry !== null) { | ||
$dependencyFactory = DependencyFactory::fromConnection( | ||
new ExistingConfiguration($configuration), | ||
ConnectionRegistryConnection::withSimpleDefault($connectionRegistry), | ||
$logger | ||
); | ||
} else { | ||
throw new LogicalException('You must provide either ManagerRegistry or ConnectionRegistry.'); | ||
} | ||
|
||
$migrationFactory = new class ($dependencyFactory) implements MigrationFactory { | ||
|
||
public function __construct( | ||
private DependencyFactory $dependencyFactory | ||
) | ||
{ | ||
} | ||
|
||
public function createVersion(string $migrationClassName): AbstractMigration | ||
{ | ||
$migration = new $migrationClassName( | ||
$this->dependencyFactory->getConnection(), | ||
$this->dependencyFactory->getLogger() | ||
); | ||
|
||
assert($migration instanceof AbstractMigration); | ||
|
||
return $migration; | ||
} | ||
|
||
}; | ||
|
||
$dependencyFactory->setService(MigrationFactory::class, new MigrationFactoryDecorator($container, $migrationFactory)); | ||
|
||
return $dependencyFactory; | ||
} | ||
|
||
} |
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,8 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nettrine\Migrations\Exceptions; | ||
|
||
final class LogicalException extends \LogicException | ||
{ | ||
|
||
} |
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,8 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nettrine\Migrations\Exceptions; | ||
|
||
final class RuntimeException extends \RuntimeException | ||
{ | ||
|
||
} |
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,12 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nettrine\Migrations\Migration; | ||
|
||
use Nette\DI\Container; | ||
|
||
interface ContainerAwareInterface | ||
{ | ||
|
||
public function setContainer(Container $container): void; | ||
|
||
} |
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,34 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Nettrine\Migrations\Migration; | ||
|
||
use Doctrine\Migrations\AbstractMigration; | ||
use Doctrine\Migrations\Version\MigrationFactory; | ||
use Nette\DI\Container; | ||
|
||
class MigrationFactoryDecorator implements MigrationFactory | ||
{ | ||
|
||
public function __construct( | ||
private Container $container, | ||
private MigrationFactory $migrationFactory, | ||
) | ||
{ | ||
} | ||
|
||
public function createVersion(string $migrationClassName): AbstractMigration | ||
{ | ||
$instance = $this->migrationFactory->createVersion($migrationClassName); | ||
|
||
// Call setContainer | ||
if ($instance instanceof ContainerAwareInterface) { | ||
$instance->setContainer($this->container); | ||
} | ||
|
||
// Allow to use inject<> | ||
$this->container->callInjects($instance); | ||
|
||
return $instance; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.