Skip to content

Commit 21bc326

Browse files
authored
Merge pull request #5 from autorusltd/release/v1.11.0
Register custom types
2 parents 9d2dd40 + e013170 commit 21bc326

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

src/ManagerRegistry.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function __construct(Container $container)
4747
$configuration = $container->get('doctrine.configuration');
4848
$connections = [];
4949
$managers = [];
50+
$types = [];
5051

5152
foreach ($configuration as $name => $params) {
5253
$connections[$name] = sprintf('doctrine.connection.%s', $name);
@@ -59,10 +60,15 @@ public function __construct(Container $container)
5960
$container->set($managers[$name], factory(function ($params) {
6061
return $this->createManager($params);
6162
})->parameter('params', $params));
63+
64+
if (!empty($params['types'])) {
65+
$types += $params['types'];
66+
}
6267
}
6368

6469
parent::__construct('ORM', $connections, $managers, key($connections), key($managers), Proxy::class);
6570

71+
$this->registerUserTypes($types);
6672
$this->registerUuidType();
6773

6874
$this->container = $container;
@@ -194,6 +200,20 @@ private function createManager(array $params) : EntityManagerInterface
194200
return EntityManager::create($params['connection'], $config);
195201
}
196202

203+
/**
204+
* @param array $types
205+
*
206+
* @return void
207+
*/
208+
private function registerUserTypes(array $types) : void
209+
{
210+
foreach ($types as $name => $class) {
211+
Type::hasType($name) ?
212+
Type::overrideType($name, $class) :
213+
Type::addType($name, $class);
214+
}
215+
}
216+
197217
/**
198218
* @return void
199219
*/

tests/Fixture/ContainerAwareTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ private function getContainer() : Container
4747
__DIR__ . '/Entity',
4848
],
4949
'proxy_auto_generate' => false,
50+
'types' => [
51+
Example1DbalType::NAME => Example1DbalType::class,
52+
],
5053
],
5154
'bar' => [
5255
'connection' => [
@@ -56,6 +59,9 @@ private function getContainer() : Container
5659
__DIR__ . '/Entity',
5760
],
5861
'proxy_auto_generate' => false,
62+
'types' => [
63+
Example2DbalType::NAME => Example2DbalType::class,
64+
],
5965
],
6066
]);
6167

tests/Fixture/Example1DbalType.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Arus\Doctrine\Bridge\Tests\Fixture;
4+
5+
/**
6+
* Import classes
7+
*/
8+
use Doctrine\DBAL\Platforms\AbstractPlatform;
9+
use Doctrine\DBAL\Types\Type;
10+
11+
/**
12+
* Example1DbalType
13+
*/
14+
final class Example1DbalType extends Type
15+
{
16+
17+
/**
18+
* Name of the type
19+
*
20+
* @var string
21+
*/
22+
public const NAME = 'test:example:1';
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function getName()
28+
{
29+
return self::NAME;
30+
}
31+
32+
/**
33+
* {@inheritDoc}
34+
*/
35+
public function getSQLDeclaration(array $field, AbstractPlatform $platform)
36+
{
37+
return $platform->getVarcharTypeDeclarationSQL($field);
38+
}
39+
}

tests/Fixture/Example2DbalType.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Arus\Doctrine\Bridge\Tests\Fixture;
4+
5+
/**
6+
* Import classes
7+
*/
8+
use Doctrine\DBAL\Platforms\AbstractPlatform;
9+
use Doctrine\DBAL\Types\Type;
10+
11+
/**
12+
* Example2DbalType
13+
*/
14+
final class Example2DbalType extends Type
15+
{
16+
17+
/**
18+
* Name of the type
19+
*
20+
* @var string
21+
*/
22+
public const NAME = 'test:example:2';
23+
24+
/**
25+
* {@inheritDoc}
26+
*/
27+
public function getName()
28+
{
29+
return self::NAME;
30+
}
31+
32+
/**
33+
* {@inheritDoc}
34+
*/
35+
public function getSQLDeclaration(array $field, AbstractPlatform $platform)
36+
{
37+
return $platform->getVarcharTypeDeclarationSQL($field);
38+
}
39+
}

tests/ManagerRegistryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
use Arus\Doctrine\Bridge\RepositoryFactory;
99
use Doctrine\DBAL\Connection;
10+
use Doctrine\DBAL\Types\Type;
1011
use Doctrine\ORM\EntityManager as Manager;
1112
use Doctrine\ORM\Events;
1213
use Doctrine\ORM\ORMException;
@@ -215,4 +216,22 @@ public function testHydrator() : void
215216

216217
$doctrine->getHydrator('undefined');
217218
}
219+
220+
/**
221+
* @return void
222+
*/
223+
public function testCustomTypes() : void
224+
{
225+
$container = $this->getContainer();
226+
227+
$doctrine = $container->get('doctrine');
228+
229+
$this->assertTrue(Type::hasType(
230+
Fixture\Example1DbalType::NAME
231+
));
232+
233+
$this->assertTrue(Type::hasType(
234+
Fixture\Example2DbalType::NAME
235+
));
236+
}
218237
}

0 commit comments

Comments
 (0)