|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* PHP version 7.4 |
|
5
|
|
|
* src/Utility/Utils.php |
|
6
|
|
|
*/ |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
namespace MiW\DemoDoctrine\Utility; |
|
9
|
|
|
|
|
10
|
|
|
use Doctrine\ORM\EntityManager; |
|
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
12
|
|
|
use Doctrine\ORM\Tools\Setup; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Trait Utils |
|
16
|
|
|
*/ |
|
|
|
|
|
|
17
|
|
|
trait Utils |
|
18
|
|
|
{ |
|
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Generate the Entity Manager |
|
21
|
|
|
* |
|
22
|
|
|
* @return EntityManagerInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function getEntityManager(): EntityManagerInterface |
|
|
|
|
|
|
25
|
|
|
{ |
|
|
|
|
|
|
26
|
|
|
if (!isset( |
|
|
|
|
|
|
27
|
|
|
$_ENV['DATABASE_NAME'], |
|
|
|
|
|
|
28
|
|
|
$_ENV['DATABASE_USER'], |
|
|
|
|
|
|
29
|
|
|
$_ENV['DATABASE_PASSWD'], |
|
|
|
|
|
|
30
|
|
|
$_ENV['ENTITY_DIR'] |
|
|
|
|
|
|
31
|
|
|
)) { |
|
|
|
|
|
|
32
|
|
|
fwrite(STDERR, 'Faltan variables de entorno por definir' . PHP_EOL); |
|
|
|
|
|
|
33
|
|
|
exit(1); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
// Cargar configuración de la conexión |
|
|
|
|
|
|
37
|
|
|
$dbParams = [ |
|
|
|
|
|
|
38
|
|
|
'host' => $_ENV['DATABASE_HOST'] ?? '127.0.0.1', |
|
|
|
|
|
|
39
|
|
|
'port' => $_ENV['DATABASE_PORT'] ?? 3306, |
|
|
|
|
|
|
40
|
|
|
'dbname' => $_ENV['DATABASE_NAME'], |
|
|
|
|
|
|
41
|
|
|
'user' => $_ENV['DATABASE_USER'], |
|
|
|
|
|
|
42
|
|
|
'password' => $_ENV['DATABASE_PASSWD'], |
|
|
|
|
|
|
43
|
|
|
'driver' => $_ENV['DATABASE_DRIVER'] ?? 'pdo_mysql', |
|
|
|
|
|
|
44
|
|
|
'charset' => $_ENV['DATABASE_CHARSET'] ?? 'UTF8', |
|
|
|
|
|
|
45
|
|
|
]; |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$debug = $_ENV['DEBUG'] ?? false; |
|
|
|
|
|
|
48
|
|
|
$entityDir = dirname(__DIR__, 2) . $_ENV['ENTITY_DIR']; |
|
|
|
|
|
|
49
|
|
|
$config = Setup::createAnnotationMetadataConfiguration( |
|
|
|
|
|
|
50
|
|
|
[ $entityDir ], // paths to mapped entities |
|
|
|
|
|
|
51
|
|
|
$debug, // developper mode |
|
|
|
|
|
|
52
|
|
|
ini_get('sys_temp_dir'), // Proxy dir |
|
|
|
|
|
|
53
|
|
|
null, // Cache implementation |
|
|
|
|
|
|
54
|
|
|
false // use Simple Annotation Reader |
|
|
|
|
|
|
55
|
|
|
); |
|
56
|
|
|
$config->setAutoGenerateProxyClasses(true); |
|
|
|
|
|
|
57
|
|
|
if ($debug) { |
|
58
|
|
|
$config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger()); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
$entityManager = EntityManager::create($dbParams, $config); |
|
63
|
|
|
} catch (\Throwable $e) { |
|
64
|
|
|
$msg = sprintf('ERROR (%d): %s', $e->getCode(), $e->getMessage()); |
|
65
|
|
|
fwrite(STDERR, $msg . PHP_EOL); |
|
|
|
|
|
|
66
|
|
|
exit(1); |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $entityManager; |
|
70
|
|
|
} |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Load the environment/configuration variables |
|
74
|
|
|
* defined in .env file + (.env.docker || .env.local) |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $dir project root directory |
|
|
|
|
|
|
77
|
|
|
*/ |
|
|
|
|
|
|
78
|
|
|
public static function loadEnv(string $dir): void |
|
79
|
|
|
{ |
|
80
|
|
|
require_once $dir . '/vendor/autoload.php'; |
|
81
|
|
|
|
|
82
|
|
|
if (!class_exists(\Dotenv\Dotenv::class)) { |
|
83
|
|
|
fwrite(STDERR, 'ERROR: No se ha cargado la clase Dotenv' . PHP_EOL); |
|
84
|
|
|
exit(1); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Load environment variables from .env file |
|
88
|
|
|
if (file_exists($dir . '/.env')) { |
|
89
|
|
|
$dotenv = \Dotenv\Dotenv::createMutable($dir, '.env'); |
|
90
|
|
|
$dotenv->load(); |
|
91
|
|
|
} else { |
|
92
|
|
|
fwrite(STDERR, 'ERROR: no existe el fichero .env' . PHP_EOL); |
|
93
|
|
|
exit(1); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Overload (if they exist) with .env.docker or .env.local |
|
97
|
|
|
if (isset($_SERVER['DOCKER']) && file_exists($dir . '/.env.docker')) { |
|
98
|
|
|
$dotenv = \Dotenv\Dotenv::createMutable($dir, '.env.docker'); |
|
99
|
|
|
$dotenv->load(); |
|
100
|
|
|
} elseif (file_exists($dir . '/.env.local')) { |
|
101
|
|
|
$dotenv = \Dotenv\Dotenv::createMutable($dir, '.env.local'); |
|
102
|
|
|
$dotenv->load(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
|
|
|
|
|
105
|
|
|
} |
|
|
|
|
|
|
106
|
|
|
|