Skip to content
This repository has been archived by the owner on Mar 27, 2022. It is now read-only.

Commit

Permalink
Merge branch 'svycka-hotfix/add-custom-types-before-registering'
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Jun 30, 2017
2 parents 6dd456c + b0b2f8d commit a4833d2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 36 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -16,7 +16,6 @@ matrix:
env:
- EXECUTE_TEST_COVERALLS=true
- php: 7
- php: hhvm

before_install:
- if [[ $EXECUTE_TEST_COVERALLS != 'true' ]]; then phpenv config-rm xdebug.ini || return 0 ; fi
Expand Down
34 changes: 34 additions & 0 deletions src/ConnectionFactory.php
Expand Up @@ -13,17 +13,25 @@
use Doctrine\DBAL\Driver\PDOMySql\Driver as PdoMysqlDriver;
use Doctrine\DBAL\DriverManager;
use Psr\Container\ContainerInterface;
use Doctrine\DBAL\Types\Type;

/**
* @method Connection __invoke(ContainerInterface $container)
*/
class ConnectionFactory extends AbstractFactory
{
/**
* @var bool
*/
private static $areTypesRegistered = false;

/**
* {@inheritdoc}
*/
protected function createWithConfig(ContainerInterface $container, $configKey)
{
$this->registerTypes($container);

$config = $this->retrieveConfig($container, $configKey, 'connection');
$params = $config['params'] + [
'driverClass' => $config['driver_class'],
Expand Down Expand Up @@ -75,4 +83,30 @@ protected function getDefaultConfig($configKey)
'doctrine_commented_types' => [],
];
}

/**
* Registers all declared typed, if not already done.
*
* @param ContainerInterface $container
*/
private function registerTypes(ContainerInterface $container)
{
if (self::$areTypesRegistered) {
return;
}

$applicationConfig = $container->has('config') ? $container->get('config') : [];
$doctrineConfig = array_key_exists('doctrine', $applicationConfig) ? $applicationConfig['doctrine'] : [];
$typesConfig = array_key_exists('types', $doctrineConfig) ? $doctrineConfig['types'] : [];
self::$areTypesRegistered = true;

foreach ($typesConfig as $name => $className) {
if (Type::hasType($name)) {
Type::overrideType($name, $className);
continue;
}

Type::addType($name, $className);
}
}
}
35 changes: 0 additions & 35 deletions src/EntityManagerFactory.php
Expand Up @@ -9,7 +9,6 @@

namespace ContainerInteropDoctrine;

use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManager;
use Psr\Container\ContainerInterface;

Expand All @@ -18,18 +17,11 @@
*/
class EntityManagerFactory extends AbstractFactory
{
/**
* @var bool
*/
private static $areTypesRegistered = false;

/**
* {@inheritdoc}
*/
protected function createWithConfig(ContainerInterface $container, $configKey)
{
$this->registerTypes($container);

$config = $this->retrieveConfig($container, $configKey, 'entity_manager');

return EntityManager::create(
Expand Down Expand Up @@ -58,31 +50,4 @@ protected function getDefaultConfig($configKey)
'configuration' => $configKey,
];
}

/**
* Registers all declared typed, if not already done.
*
* @param ContainerInterface $container
*/
private function registerTypes(ContainerInterface $container)
{
if (self::$areTypesRegistered) {
return;
}

$applicationConfig = $container->has('config') ? $container->get('config') : [];
$doctrineConfig = array_key_exists('doctrine', $applicationConfig) ? $applicationConfig['doctrine'] : [];
$typesConfig = array_key_exists('types', $doctrineConfig) ? $doctrineConfig['types'] : [];

foreach ($typesConfig as $name => $className) {
if (Type::hasType($name)) {
Type::overrideType($name, $className);
continue;
}

Type::addType($name, $className);
}

self::$areTypesRegistered = true;
}
}
17 changes: 17 additions & 0 deletions test/ConnectionFactoryTest.php
Expand Up @@ -13,6 +13,7 @@
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver;
use Doctrine\DBAL\Exception\ConnectionException;
use Doctrine\DBAL\Types\BooleanType;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Configuration;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -144,6 +145,19 @@ public function testDoctrineCommentedTypesInjection()
$this->assertTrue($connection->getDatabasePlatform()->isCommentedDoctrineType($type));
}

public function testCustomTypeDoctrineMappingTypesInjection()
{
$factory = new ConnectionFactory();
$property = (new \ReflectionObject($factory))->getProperty('areTypesRegistered');
$property->setAccessible(true);
$property->setValue($factory, false);

$connection = $factory($this->buildContainer('orm_default', 'orm_default', 'orm_default', [
'doctrine_mapping_types' => ['foo' => 'custom_type'],
])->reveal());

$this->assertTrue($connection->getDatabasePlatform()->hasDoctrineTypeMappingFor('foo'));
}
/**
* @param string $ownKey
* @param string $configurationKey
Expand All @@ -166,6 +180,9 @@ private function buildContainer(
'driver_class' => PDOSqliteDriver::class,
],
],
'types' => [
'custom_type' => BooleanType::class
]
],
]);

Expand Down

0 comments on commit a4833d2

Please sign in to comment.