From b00848e897c2cc72da75d51ba233cbd294090837 Mon Sep 17 00:00:00 2001 From: tg666 Date: Thu, 10 Dec 2020 05:16:50 +0100 Subject: [PATCH] Added argument `$context` for custom DBAL types --- src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php | 6 +++++- src/DI/DatabaseType.php | 7 ++++++- src/Type/ContainerAwareTypeInterface.php | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php b/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php index bc0baee..1d10f0e 100644 --- a/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php +++ b/src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php @@ -61,6 +61,7 @@ private function processDatabaseTypeProviders(): void $connectionFactory = $builder->getDefinition($dbalExtensionName . '.connectionFactory'); $factory = $connectionFactory->getFactory(); [$types, $typesMapping] = $factory->arguments; + $contexts = []; /** @var \SixtyEightPublishers\DoctrineBridge\DI\DatabaseTypeProviderInterface $extension */ foreach ($this->compiler->getExtensions(DatabaseTypeProviderInterface::class) as $extension) { @@ -73,6 +74,8 @@ private function processDatabaseTypeProviders(): void if (NULL !== $databaseType->mappingType) { $typesMapping[$databaseType->name] = $databaseType->mappingType; } + + $contexts[$databaseType->name] = $databaseType->context; } } @@ -89,10 +92,11 @@ private function processDatabaseTypeProviders(): void continue; } - $connection->addSetup('?::getType(?)->setContainer(?)', [ + $connection->addSetup('?::getType(?)->setContainer(?, ?)', [ new PhpLiteral(Type::class), $typeName, new Reference($builder::THIS_CONTAINER), + $contexts[$typeName] ?? [], ]); } } diff --git a/src/DI/DatabaseType.php b/src/DI/DatabaseType.php index 05fa3e9..b30671b 100644 --- a/src/DI/DatabaseType.php +++ b/src/DI/DatabaseType.php @@ -15,15 +15,20 @@ final class DatabaseType /** @var string|NULL */ public $mappingType; + /** @var array */ + public $context; + /** * @param string $name * @param string $class * @param string|NULL $mappingType + * @param array $context */ - public function __construct(string $name, string $class, ?string $mappingType = NULL) + public function __construct(string $name, string $class, ?string $mappingType = NULL, array $context = []) { $this->name = $name; $this->class = $class; $this->mappingType = $mappingType; + $this->context = $context; } } diff --git a/src/Type/ContainerAwareTypeInterface.php b/src/Type/ContainerAwareTypeInterface.php index ee1497d..00e8d42 100644 --- a/src/Type/ContainerAwareTypeInterface.php +++ b/src/Type/ContainerAwareTypeInterface.php @@ -10,8 +10,9 @@ interface ContainerAwareTypeInterface { /** * @param \Nette\DI\Container $container + * @param array $context * * @return void */ - public function setContainer(Container $container): void; + public function setContainer(Container $container, array $context = []): void; }