1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* src/Utility/DoctrineConnector.php |
|
|
|
|
5
|
|
|
* |
6
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
|
|
|
|
7
|
|
|
* @link http://www.etsisi.upm.es/ ETS de Ingeniería de Sistemas Informáticos |
|
|
|
|
8
|
|
|
*/ |
|
|
|
|
9
|
|
|
|
10
|
|
|
namespace MiW\DemoDoctrine\Utility; |
11
|
|
|
|
12
|
|
|
use Doctrine\ORM\EntityManager; |
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
14
|
|
|
use Doctrine\ORM\Tools\Setup; |
15
|
|
|
use Throwable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class DoctrineConnector |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
final class DoctrineConnector |
21
|
|
|
{ |
|
|
|
|
22
|
|
|
private static ?EntityManager $instance = null; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Generate the Entity Manager |
26
|
|
|
* |
27
|
|
|
* @return EntityManagerInterface |
28
|
|
|
*/ |
29
|
|
|
public static function getEntityManager(): EntityManagerInterface |
|
|
|
|
30
|
|
|
{ |
|
|
|
|
31
|
|
|
if (null !== self::$instance) { |
|
|
|
|
32
|
|
|
return self::$instance; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ( |
|
|
|
|
36
|
|
|
!isset( |
|
|
|
|
37
|
|
|
$_ENV['DATABASE_NAME'], |
|
|
|
|
38
|
|
|
$_ENV['DATABASE_USER'], |
|
|
|
|
39
|
|
|
$_ENV['DATABASE_PASSWD'], |
|
|
|
|
40
|
|
|
$_ENV['ENTITY_DIR'] |
|
|
|
|
41
|
|
|
) |
|
|
|
|
42
|
|
|
) { |
43
|
|
|
fwrite(STDERR, 'Faltan variables de entorno por definir' . PHP_EOL); |
|
|
|
|
44
|
|
|
exit(1); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Cargar configuración de la conexión |
|
|
|
|
48
|
|
|
$dbParams = [ |
|
|
|
|
49
|
|
|
'host' => $_ENV['DATABASE_HOST'] ?? '127.0.0.1', |
|
|
|
|
50
|
|
|
'port' => $_ENV['DATABASE_PORT'] ?? 3306, |
|
|
|
|
51
|
|
|
'dbname' => $_ENV['DATABASE_NAME'], |
|
|
|
|
52
|
|
|
'user' => $_ENV['DATABASE_USER'], |
|
|
|
|
53
|
|
|
'password' => $_ENV['DATABASE_PASSWD'], |
|
|
|
|
54
|
|
|
'driver' => $_ENV['DATABASE_DRIVER'] ?? 'pdo_mysql', |
|
|
|
|
55
|
|
|
'charset' => $_ENV['DATABASE_CHARSET'] ?? 'UTF8', |
|
|
|
|
56
|
|
|
]; |
|
|
|
|
57
|
|
|
|
58
|
|
|
$entityDir = dirname(__DIR__, 2) . '/' . $_ENV['ENTITY_DIR']; |
|
|
|
|
59
|
|
|
$debug = $_ENV['DEBUG'] ?? false; |
|
|
|
|
60
|
|
|
$config = Setup::createAnnotationMetadataConfiguration( |
|
|
|
|
61
|
|
|
[ $entityDir ], // paths to mapped entities |
|
|
|
|
62
|
|
|
$debug, // developper mode |
|
|
|
|
63
|
|
|
ini_get('sys_temp_dir'), // Proxy dir |
|
|
|
|
64
|
|
|
null, // Cache implementation |
|
|
|
|
65
|
|
|
false // use Simple Annotation Reader |
|
|
|
|
66
|
|
|
); |
67
|
|
|
$config->setAutoGenerateProxyClasses(true); |
|
|
|
|
68
|
|
|
if ($debug) { |
69
|
|
|
$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
$entityManager = EntityManager::create($dbParams, $config); |
74
|
|
|
} catch (Throwable $e) { |
75
|
|
|
$msg = sprintf('ERROR (%d): %s', $e->getCode(), $e->getMessage()); |
76
|
|
|
fwrite(STDERR, $msg . PHP_EOL); |
|
|
|
|
77
|
|
|
exit(1); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $entityManager; |
81
|
|
|
} |
|
|
|
|
82
|
|
|
|
83
|
|
|
protected function __construct() |
|
|
|
|
84
|
|
|
{ |
|
|
|
|
85
|
|
|
} |
|
|
|
|
86
|
|
|
|
87
|
|
|
private function __clone() |
|
|
|
|
88
|
|
|
{ |
|
|
|
|
89
|
|
|
} |
|
|
|
|
90
|
|
|
|
91
|
|
|
// private function __wakeup() |
|
|
|
|
92
|
|
|
// { |
|
|
|
|
93
|
|
|
// } |
|
|
|
|
94
|
|
|
} |
|
|
|
|
95
|
|
|
|