From 11cfcf8f12cfed5c376dbb894cfd826e507bb7f5 Mon Sep 17 00:00:00 2001 From: tg666 Date: Sun, 29 Nov 2020 20:33:16 +0100 Subject: [PATCH] Container aware types - added interface `ContainerAwareTypeInterface` - added processing of container aware types in the Nettrine bridge - updated README --- README.md | 23 ++++++++++++++++++ .../Nettrine/DI/DoctrineBridgeExtension.php | 24 ++++++++++++++++++- src/Type/ContainerAwareTypeInterface.php | 17 +++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 src/Type/ContainerAwareTypeInterface.php diff --git a/README.md b/README.md index 4b0c8f1..a5b33d7 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,29 @@ class FooExtension extends CompilerExtension implements DatabaseTypeProviderInte } ``` +#### Services in Doctrine Types + +Doctrine DBAL types don't have access to services by default. +With this extension, you can receive DI Container in your custom types when the Connection is created. + +```php +service = $container->getByType(MyService::class); + } +} +``` + ### Entity Mapping Provider ```php diff --git a/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php b/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php index c1b3733..bc0baee 100644 --- a/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php +++ b/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php @@ -4,13 +4,17 @@ namespace SixtyEightPublishers\DoctrineBridge\Bridge\Nettrine\DI; +use Doctrine\DBAL\Types\Type; use Nette\DI\CompilerExtension; +use Nette\PhpGenerator\PhpLiteral; +use Nette\DI\Definitions\Reference; use Nettrine\DBAL\DI\DbalExtension; use Nettrine\ORM\DI\Helpers\MappingHelper; use Doctrine\ORM\Tools\ResolveTargetEntityListener; 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; final class DoctrineBridgeExtension extends CompilerExtension @@ -51,9 +55,10 @@ private function processDatabaseTypeProviders(): void } $dbalExtensionName = key($dbalExtensions); + $builder = $this->getContainerBuilder(); /** @var \Nette\DI\Definitions\ServiceDefinition $connectionFactory */ - $connectionFactory = $this->getContainerBuilder()->getDefinition($dbalExtensionName . '.connectionFactory'); + $connectionFactory = $builder->getDefinition($dbalExtensionName . '.connectionFactory'); $factory = $connectionFactory->getFactory(); [$types, $typesMapping] = $factory->arguments; @@ -73,6 +78,23 @@ private function processDatabaseTypeProviders(): void $factory->arguments[0] = $types; $factory->arguments[1] = $typesMapping; + + /** @var \Nette\DI\Definitions\ServiceDefinition $connection */ + $connection = $builder->getDefinition($dbalExtensionName . '.connection'); + + foreach ($types as $typeName => $typeOptions) { + $typeClassName = $typeOptions['class']; + + if (!is_subclass_of($typeClassName, ContainerAwareTypeInterface::class, TRUE)) { + continue; + } + + $connection->addSetup('?::getType(?)->setContainer(?)', [ + new PhpLiteral(Type::class), + $typeName, + new Reference($builder::THIS_CONTAINER), + ]); + } } /** diff --git a/src/Type/ContainerAwareTypeInterface.php b/src/Type/ContainerAwareTypeInterface.php new file mode 100644 index 0000000..ee1497d --- /dev/null +++ b/src/Type/ContainerAwareTypeInterface.php @@ -0,0 +1,17 @@ +