File size: 3,600 Bytes
cf14c12 | 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | <?php
declare(strict_types=1);
namespace Shared\Infrastructure\Bootstrappers;
use Application;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMSetup;
use Easy\Container\Attributes\Inject;
use ErrorException;
use Exception;
use Ramsey\Uuid\Doctrine\UuidBinaryType;
use RuntimeException;
use Shared\Infrastructure\BootstrapperInterface;
class DoctrineBootstrapper implements BootstrapperInterface
{
/**
* @param Application $app
* @param string $srcDir
* @param string $proxyDir
* @return void
*/
public function __construct(
private Application $app,
#[Inject('config.dirs.src')]
private string $srcDir,
#[Inject('config.dirs.cache')]
private string $proxyDir
) {
}
/** @inheritDoc */
public function bootstrap(): void
{
$params = $this->getConnectionParams();
if ($params) {
Type::addType('uuid_binary', UuidBinaryType::class);
$em = $this->getEntityManager($params);
$em->getConnection()->getDatabasePlatform()
->registerDoctrineTypeMapping('uuid_binary', 'binary');
$this->app
->set(EntityManagerInterface::class, $em);
}
}
/**
* @param array $params
* @return EntityManagerInterface
* @throws RuntimeException
* @throws ErrorException
* @throws DBALException
*/
private function getEntityManager(array $params): EntityManagerInterface
{
$config = ORMSetup::createAttributeMetadataConfiguration(
paths: [$this->srcDir],
isDevMode: true,
proxyDir: $this->proxyDir
);
$connection = DriverManager::getConnection($params, $config);
return new EntityManager($connection, $config);
}
/**
* @return null|array
* @throws Exception
*/
private function getConnectionParams(): ?array
{
$driver = env('DB_DRIVER');
if (!$driver) {
return null;
}
switch ($driver) {
case 'mysql':
$connection = $this->getMysqlConnection();
break;
case 'sqlite':
$connection = $this->getSqliteConnection();
break;
default:
throw new Exception('Value of the DB_DRIVER env var is not valid.');
}
return $connection;
}
/**
* Get connection config to create MySQL connection
*
* @return array
*/
private function getMysqlConnection(): array
{
$connection = [
'driver' => 'pdo_mysql',
'user' => env('DB_USER'),
'password' => env('DB_PASSWORD'),
'dbname' => env('DB_NAME'),
'charset' => env('DB_CHARSET')
];
if (env('DB_UNIX_SOCKET')) {
$connection['unix_socket'] = env('DB_UNIX_SOCKET');
return $connection;
}
$connection['host'] = env('DB_HOST');
$connection['port'] = env('DB_PORT');
return $connection;
}
/**
* Get connection config to create SQLite connection
*
* @return array
*/
private function getSqliteConnection(): array
{
return [
'driver' => 'pdo_sqlite',
'user' => env('DB_USER'),
'password' => env('DB_PASSWORD'),
'path' => env('SQLITE_PATH')
];
}
}
|