Skip to content

Commit

Permalink
Added argument $context for custom DBAL types
Browse files Browse the repository at this point in the history
  • Loading branch information
tg666 committed Dec 10, 2020
1 parent 11cfcf8 commit b00848e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -73,6 +74,8 @@ private function processDatabaseTypeProviders(): void
if (NULL !== $databaseType->mappingType) {
$typesMapping[$databaseType->name] = $databaseType->mappingType;
}

$contexts[$databaseType->name] = $databaseType->context;
}
}

Expand All @@ -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] ?? [],
]);
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/DI/DatabaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion src/Type/ContainerAwareTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit b00848e

Please sign in to comment.