1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Simplex\Quickstart\Shared\Factory; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
7
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
use Doctrine\ORM\Configuration; |
10
|
|
|
use Doctrine\ORM\EntityManager; |
11
|
|
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
12
|
|
|
use Ramsey\Uuid\Doctrine\UuidType; |
13
|
|
|
use Symfony\Component\Finder\Finder; |
14
|
|
|
|
15
|
|
|
class DoctrineOrmFactory |
16
|
|
|
{ |
17
|
|
|
const DOCTRINE_CACHE_DIRECTORY = 'doctrine'; |
18
|
|
|
|
19
|
|
|
const PROXY_DIRECTORY = self::DOCTRINE_CACHE_DIRECTORY . DIRECTORY_SEPARATOR . 'proxy'; |
20
|
|
|
|
21
|
|
|
const PROXY_NAMESPACE = 'DoctrineProxies'; |
22
|
|
|
|
23
|
|
|
const MODULES_DIRECTORY_PATH = __DIR__ . '/../../Module'; |
24
|
|
|
|
25
|
|
|
const ENTITY_DIRECTORY_NAME = 'Model'; |
26
|
|
|
|
27
|
|
|
const UUID_TYPE_NAME = 'uuid'; |
28
|
|
|
|
29
|
|
|
public function create( |
30
|
|
|
string $host, |
31
|
|
|
string $port, |
32
|
|
|
string $name, |
33
|
|
|
string $user, |
34
|
|
|
string $pass, |
35
|
|
|
string $cacheDir, |
36
|
|
|
bool $enableCache) : EntityManager |
37
|
|
|
{ |
38
|
|
|
$connectionOptions = array( |
39
|
|
|
'driver' => 'pdo_mysql', |
40
|
|
|
'host' => $host, |
41
|
|
|
'port' => $port, |
42
|
|
|
'dbname' => $name, |
43
|
|
|
'user' => $user, |
44
|
|
|
'password' => $pass, |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
$config = $this->createConfig($cacheDir, $enableCache); |
48
|
|
|
|
49
|
|
|
$this->addCustomTypes(); |
50
|
|
|
|
51
|
|
|
return EntityManager::create($connectionOptions, $config); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function createConfig(string $cacheDir, bool $enableCache): Configuration |
55
|
|
|
{ |
56
|
|
|
$cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR); |
57
|
|
|
|
58
|
|
|
if (!$enableCache) { |
59
|
|
|
$cache = new ArrayCache(); |
60
|
|
|
} else { |
61
|
|
|
$cache = new FilesystemCache($cacheDir . DIRECTORY_SEPARATOR . self::DOCTRINE_CACHE_DIRECTORY); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$config = new Configuration; |
65
|
|
|
|
66
|
|
|
$driverImpl = new AnnotationDriver(new AnnotationReader(), $this->getEntityDirPaths()); |
67
|
|
|
|
68
|
|
|
$config->setMetadataDriverImpl($driverImpl); |
69
|
|
|
$config->setMetadataCacheImpl($cache); |
70
|
|
|
$config->setQueryCacheImpl($cache); |
71
|
|
|
$config->setProxyDir($cacheDir . DIRECTORY_SEPARATOR . self::PROXY_DIRECTORY); |
72
|
|
|
$config->setProxyNamespace(self::PROXY_NAMESPACE); |
73
|
|
|
|
74
|
|
|
if ($enableCache) { |
75
|
|
|
$config->setAutoGenerateProxyClasses(true); |
76
|
|
|
} else { |
77
|
|
|
$config->setAutoGenerateProxyClasses(false); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $config; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function getEntityDirPaths(): array |
84
|
|
|
{ |
85
|
|
|
$finder = new Finder(); |
86
|
|
|
$finder->directories()->depth(0)->in(self::MODULES_DIRECTORY_PATH); |
87
|
|
|
|
88
|
|
|
$paths = []; |
89
|
|
|
foreach ($finder as $dir) { |
90
|
|
|
$modelDir = $dir->getPathname() . DIRECTORY_SEPARATOR . self::ENTITY_DIRECTORY_NAME; |
91
|
|
|
if (is_readable($modelDir)) { |
92
|
|
|
$paths[] = $modelDir; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $paths; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function addCustomTypes() |
100
|
|
|
{ |
101
|
|
|
Type::addType(self::UUID_TYPE_NAME, UuidType::class); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|