1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the prooph/php-ddd-cargo-sample. |
4
|
|
|
* (c) Alexander Miertsch <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* Date: 8/9/15 - 10:07 PM |
10
|
|
|
*/ |
11
|
|
|
declare(strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace Codeliner\CargoBackend\Infrastructure\Container\Infrastructure; |
14
|
|
|
|
15
|
|
|
use Codeliner\CargoBackend\Infrastructure\Persistence\Doctrine\Type\LegsDoctrineType; |
16
|
|
|
use Codeliner\CargoBackend\Infrastructure\Persistence\Doctrine\Type\TrackingIdDoctrineType; |
17
|
|
|
use Doctrine\DBAL\Types\Type; |
18
|
|
|
use Doctrine\ORM\EntityManager; |
19
|
|
|
use Interop\Container\ContainerInterface; |
20
|
|
|
|
21
|
|
|
final class DoctrineEntityManagerFactory |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Create service |
25
|
|
|
* |
26
|
|
|
* @param ContainerInterface $container |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
|
|
public function __invoke(ContainerInterface $container): EntityManager |
30
|
|
|
{ |
31
|
|
|
$appConfig = $container->get('config'); |
32
|
|
|
|
33
|
|
|
if (! isset($appConfig['doctrine']['connection']['orm_default'])) { |
34
|
|
|
throw new \RuntimeException("Missing doctrine connection config for orm_default driver"); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$config = new \Doctrine\ORM\Configuration(); |
38
|
|
|
|
39
|
|
|
$config->setAutoGenerateProxyClasses(true); |
40
|
|
|
$config->setProxyDir('data/cache'); |
41
|
|
|
$config->setProxyNamespace('Codeliner\CargoBackend\Doctrine\Entities'); |
42
|
|
|
$config->setMetadataDriverImpl( |
43
|
|
|
new \Doctrine\ORM\Mapping\Driver\XmlDriver( |
44
|
|
|
array( |
45
|
|
|
__DIR__ . '/../../Persistence/Doctrine/ORM' |
46
|
|
|
) |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$config->setNamingStrategy(new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy()); |
51
|
|
|
|
52
|
|
|
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); |
53
|
|
|
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); |
54
|
|
|
|
55
|
|
|
$entityManager = \Doctrine\ORM\EntityManager::create($appConfig['doctrine']['connection']['orm_default'], $config); |
56
|
|
|
|
57
|
|
|
//Add custom DDD types to map ValueObjects correctly |
58
|
|
|
if (!Type::hasType('cargo_itinerary_legs')) { |
59
|
|
|
Type::addType('cargo_itinerary_legs', LegsDoctrineType::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!Type::hasType('cargo_tracking_id')) { |
63
|
|
|
Type::addType(TrackingIdDoctrineType::NAME, TrackingIdDoctrineType::class); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $entityManager; |
67
|
|
|
} |
68
|
|
|
} |