From f74d50c449a50634eaa09afd0f37e0c98582d789 Mon Sep 17 00:00:00 2001 From: tg666 Date: Fri, 4 Mar 2022 21:11:11 +0100 Subject: [PATCH] Update & Migrations support - added support for `nettrine/migrations` - added extension interface `MigrationsDirectoriesProviderInterface` and DI DTO `MigrationsDirectory` - updated `nettrine` packages to `^0.8` - updated php-cs-fixer and added new configuration --- .php_cs.dist => .php-cs-fixer.dist.php | 6 +-- composer.json | 9 ++-- .../Nettrine/DI/DoctrineBridgeExtension.php | 46 +++++++++++++++++-- src/DI/DatabaseType.php | 12 ++--- src/DI/EntityMapping.php | 9 ++-- ...MigrationsDirectoriesProviderInterface.php | 13 ++++++ src/DI/MigrationsDirectory.php | 22 +++++++++ src/DI/TargetEntity.php | 9 ++-- vendor-bin/nettrine/composer.json | 3 +- 9 files changed, 98 insertions(+), 31 deletions(-) rename .php_cs.dist => .php-cs-fixer.dist.php (88%) create mode 100644 src/DI/MigrationsDirectoriesProviderInterface.php create mode 100644 src/DI/MigrationsDirectory.php diff --git a/.php_cs.dist b/.php-cs-fixer.dist.php similarity index 88% rename from .php_cs.dist rename to .php-cs-fixer.dist.php index 3baba0e..d5e3b33 100644 --- a/.php_cs.dist +++ b/.php-cs-fixer.dist.php @@ -3,16 +3,16 @@ $finder = PhpCsFixer\Finder::create() ->in(__DIR__ . '/src') ->in(__DIR__ . '/tests') - ->exclude('temp') + ->exclude(__DIR__ . '/tests/temp') ; -return PhpCsFixer\Config::create() +return (new PhpCsFixer\Config) ->setUsingCache(FALSE) ->setIndent("\t") ->setRules([ '@PSR2' => TRUE, 'array_syntax' => ['syntax' => 'short'], - 'trailing_comma_in_multiline_array' => true, + 'trailing_comma_in_multiline' => TRUE, 'constant_case' => [ 'case' => 'upper', ], diff --git a/composer.json b/composer.json index 5e97ae3..34cb60f 100644 --- a/composer.json +++ b/composer.json @@ -10,18 +10,19 @@ } ], "require": { - "php": "^7.3", + "php": "^7.4", "nette/di": "^3.0.3" }, "require-dev": { "bamarni/composer-bin-plugin": "^1.4", - "friendsofphp/php-cs-fixer": "^2.0", + "friendsofphp/php-cs-fixer": "^3.0", "nette/bootstrap": "^3.0", "nette/tester": "^2.3.4", - "roave/security-advisories": "dev-master" + "roave/security-advisories": "dev-latest" }, "suggest": { - "nettrine/orm": "For integration with nettrine/orm" + "nettrine/orm": "For integration with nettrine/orm", + "nettrine/migrations": "For integration with nettrine/migrations" }, "autoload": { "psr-4": { diff --git a/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php b/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php index 1d10f0e..9b0c27e 100644 --- a/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php +++ b/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php @@ -10,12 +10,16 @@ use Nette\DI\Definitions\Reference; use Nettrine\DBAL\DI\DbalExtension; use Nettrine\ORM\DI\Helpers\MappingHelper; +use Nette\DI\Definitions\ServiceDefinition; +use Nettrine\Migrations\DI\MigrationsExtension; use Doctrine\ORM\Tools\ResolveTargetEntityListener; +use Doctrine\Migrations\Configuration\Configuration; use SixtyEightPublishers\DoctrineBridge\DI\EntityMapping; use SixtyEightPublishers\DoctrineBridge\DI\DatabaseTypeProviderInterface; use SixtyEightPublishers\DoctrineBridge\DI\TargetEntityProviderInterface; use SixtyEightPublishers\DoctrineBridge\Type\ContainerAwareTypeInterface; use SixtyEightPublishers\DoctrineBridge\DI\EntityMappingProviderInterface; +use SixtyEightPublishers\DoctrineBridge\DI\MigrationsDirectoriesProviderInterface; final class DoctrineBridgeExtension extends CompilerExtension { @@ -24,7 +28,7 @@ final class DoctrineBridgeExtension extends CompilerExtension */ public function loadConfiguration(): void { - if (0 >= count($this->compiler->getExtensions(TargetEntityProviderInterface::class))) { + if (!$this->isExtensionLoaded(TargetEntityProviderInterface::class)) { return; } @@ -41,6 +45,7 @@ public function beforeCompile(): void $this->processDatabaseTypeProviders(); $this->processEntityMappings(); $this->processTargetEntities(); + $this->processMigrationsDirectories(); } /** @@ -57,8 +62,10 @@ private function processDatabaseTypeProviders(): void $dbalExtensionName = key($dbalExtensions); $builder = $this->getContainerBuilder(); - /** @var \Nette\DI\Definitions\ServiceDefinition $connectionFactory */ $connectionFactory = $builder->getDefinition($dbalExtensionName . '.connectionFactory'); + + assert($connectionFactory instanceof ServiceDefinition); + $factory = $connectionFactory->getFactory(); [$types, $typesMapping] = $factory->arguments; $contexts = []; @@ -82,9 +89,10 @@ private function processDatabaseTypeProviders(): void $factory->arguments[0] = $types; $factory->arguments[1] = $typesMapping; - /** @var \Nette\DI\Definitions\ServiceDefinition $connection */ $connection = $builder->getDefinition($dbalExtensionName . '.connection'); + assert($connection instanceof ServiceDefinition); + foreach ($types as $typeName => $typeOptions) { $typeClassName = $typeOptions['class']; @@ -149,4 +157,36 @@ private function processTargetEntities(): void } } } + + /** + * @return void + */ + private function processMigrationsDirectories(): void + { + if (!$this->isExtensionLoaded(MigrationsExtension::class)) { + return; + } + + $builder = $this->getContainerBuilder(); + $configuration = $builder->getDefinitionByType(Configuration::class); + + assert($configuration instanceof ServiceDefinition); + + /** @var \SixtyEightPublishers\DoctrineBridge\DI\MigrationsDirectoriesProviderInterface $extension */ + foreach ($this->compiler->getExtensions(MigrationsDirectoriesProviderInterface::class) as $extension) { + foreach ($extension->getMigrationsDirectories() as $migrationsDirectory) { + $configuration->addSetup('addMigrationsDirectory', [$migrationsDirectory->namespace, $migrationsDirectory->directory]); + } + } + } + + /** + * @param string $classname + * + * @return bool + */ + private function isExtensionLoaded(string $classname): bool + { + return 0 < count($this->compiler->getExtensions($classname)); + } } diff --git a/src/DI/DatabaseType.php b/src/DI/DatabaseType.php index b30671b..6361c48 100644 --- a/src/DI/DatabaseType.php +++ b/src/DI/DatabaseType.php @@ -6,17 +6,13 @@ final class DatabaseType { - /** @var string */ - public $name; + public string $name; - /** @var string */ - public $class; + public string $class; - /** @var string|NULL */ - public $mappingType; + public ?string $mappingType; - /** @var array */ - public $context; + public array $context; /** * @param string $name diff --git a/src/DI/EntityMapping.php b/src/DI/EntityMapping.php index 9911ffc..0709189 100644 --- a/src/DI/EntityMapping.php +++ b/src/DI/EntityMapping.php @@ -10,14 +10,11 @@ final class EntityMapping public const DRIVER_YAML = 'yaml'; public const DRIVER_XML = 'xml'; - /** @var string */ - public $driver; + public string $driver; - /** @var string */ - public $namespace; + public string $namespace; - /** @var string */ - public $path; + public string $path; /** * @param string $driver diff --git a/src/DI/MigrationsDirectoriesProviderInterface.php b/src/DI/MigrationsDirectoriesProviderInterface.php new file mode 100644 index 0000000..a806f78 --- /dev/null +++ b/src/DI/MigrationsDirectoriesProviderInterface.php @@ -0,0 +1,13 @@ +namespace = $namespace; + $this->directory = $directory; + } +} diff --git a/src/DI/TargetEntity.php b/src/DI/TargetEntity.php index 1f8259f..5f82750 100644 --- a/src/DI/TargetEntity.php +++ b/src/DI/TargetEntity.php @@ -6,14 +6,11 @@ final class TargetEntity { - /** @var string */ - public $originalEntity; + public string $originalEntity; - /** @var string */ - public $newEntity; + public string $newEntity; - /** @var array */ - public $mapping; + public array$mapping; /** * @param string $originalEntity diff --git a/vendor-bin/nettrine/composer.json b/vendor-bin/nettrine/composer.json index b3e61b5..5f3b71b 100644 --- a/vendor-bin/nettrine/composer.json +++ b/vendor-bin/nettrine/composer.json @@ -1,7 +1,8 @@ { "require": { "theofidry/composer-inheritance-plugin": "^1.0", - "nettrine/orm": "^0.7" + "nettrine/orm": "^0.8", + "nettrine/migrations": "^0.8" }, "config": { "bin-dir": "bin",