/
DoctrineEntityManagerFactory.php
79 lines (63 loc) 路 2.73 KB
/
DoctrineEntityManagerFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types = 1);
namespace CodelyTv\Shared\Infrastructure\Doctrine;
use CodelyTv\Shared\Infrastructure\Doctrine\Dbal\DbalCustomTypesRegistrar;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\MySqlSchemaManager;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\Driver\SimplifiedXmlDriver;
use Doctrine\ORM\Tools\Setup;
use RuntimeException;
use function Lambdish\Phunctional\dissoc;
final class DoctrineEntityManagerFactory
{
private static $sharedPrefixes = [
__DIR__ . '/../../../Shared/Infrastructure/Persistence/Mappings' => 'CodelyTv\Shared\Domain',
];
public static function create(
array $parameters,
array $contextPrefixes,
bool $isDevMode,
string $schemaFile,
array $dbalCustomTypesClasses
): EntityManagerInterface {
if ($isDevMode) {
static::generateDatabaseIfNotExists($parameters, $schemaFile);
}
DbalCustomTypesRegistrar::register($dbalCustomTypesClasses);
return EntityManager::create($parameters, self::createConfiguration($contextPrefixes, $isDevMode));
}
private static function generateDatabaseIfNotExists(array $parameters, string $schemaFile): void
{
self::ensureSchemaFileExists($schemaFile);
$databaseName = $parameters['dbname'];
$parametersWithoutDatabaseName = dissoc($parameters, 'dbname');
$connection = DriverManager::getConnection($parametersWithoutDatabaseName);
$schemaManager = new MySqlSchemaManager($connection);
if (!self::databaseExists($databaseName, $schemaManager)) {
$schemaManager->createDatabase($databaseName);
$connection->exec(sprintf('USE %s', $databaseName));
$connection->exec(file_get_contents(realpath($schemaFile)));
}
$connection->close();
}
private static function databaseExists($databaseName, MySqlSchemaManager $schemaManager): bool
{
return in_array($databaseName, $schemaManager->listDatabases(), true);
}
private static function ensureSchemaFileExists(string $schemaFile): void
{
if (!file_exists($schemaFile)) {
throw new RuntimeException(sprintf('The file <%s> does not exist', $schemaFile));
}
}
private static function createConfiguration(array $contextPrefixes, bool $isDevMode): Configuration
{
$config = Setup::createConfiguration($isDevMode, null, new ArrayCache());
$config->setMetadataDriverImpl(new SimplifiedXmlDriver(array_merge(self::$sharedPrefixes, $contextPrefixes)));
return $config;
}
}