1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Micro framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Micro\Plugin\Doctrine\Configuration; |
15
|
|
|
|
16
|
|
|
use Doctrine\DBAL\DriverManager; |
17
|
|
|
use Micro\Framework\Kernel\Configuration\PluginRoutingKeyConfiguration; |
18
|
|
|
use Micro\Plugin\Doctrine\Configuration\Driver\DriverConfigurationInterface; |
19
|
|
|
use Micro\Plugin\Doctrine\Configuration\Driver\PdoMySqlConfiguration; |
20
|
|
|
use Micro\Plugin\Doctrine\Configuration\Driver\PdoPgSqlConfiguration; |
21
|
|
|
use Micro\Plugin\Doctrine\Configuration\Driver\PdoSqliteDriverConfiguration; |
22
|
|
|
|
23
|
|
|
class EntityManagerConfiguration extends PluginRoutingKeyConfiguration implements EntityManagerConfigurationInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Driver name. |
27
|
|
|
* |
28
|
|
|
* Example `ORM_DEFAULT_DRIVER=pdo_mysql` |
29
|
|
|
* |
30
|
|
|
* @api |
31
|
|
|
*/ |
32
|
|
|
public const CFG_DRIVER_NAME = 'ORM_%s_DRIVER'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Driver name. |
36
|
|
|
* |
37
|
|
|
* Example `ORM_DEFAULT_PROXY_DIR=${BASE_PATH}/var/cache/orm/proxy` |
38
|
|
|
* |
39
|
|
|
* @api |
40
|
|
|
*/ |
41
|
|
|
public const CFG_PROXY_DIR = 'ORM_%s_PROXY_DIR'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string|null |
45
|
|
|
*/ |
46
|
1 |
|
public function getProxyDir(): ?string |
47
|
|
|
{ |
48
|
1 |
|
return $this->get(self::CFG_PROXY_DIR); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
public function getDriverName(): string |
52
|
|
|
{ |
53
|
3 |
|
return $this->get(self::CFG_DRIVER_NAME, null, false); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function getDriverConfiguration(): DriverConfigurationInterface |
57
|
|
|
{ |
58
|
2 |
|
$driverName = mb_strtolower($this->getDriverName()); |
59
|
|
|
|
60
|
2 |
|
if (!\in_array($driverName, $this->getAvailableDrivers())) { |
61
|
1 |
|
throw new \InvalidArgumentException(sprintf('ORM: Driver `%s` is not supported.', $driverName)); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
return match ($driverName) { |
65
|
1 |
|
PdoMySqlConfiguration::name() => new PdoMySqlConfiguration($this->configuration, $this->configRoutingKey), |
66
|
1 |
|
PdoPgSqlConfiguration::name() => new PdoPgSqlConfiguration($this->configuration, $this->configRoutingKey), |
67
|
1 |
|
PdoSqliteDriverConfiguration::name() => new PdoSqliteDriverConfiguration($this->configuration, $this->configRoutingKey), |
68
|
1 |
|
default => throw new \InvalidArgumentException(sprintf('Driver `%s` is available, but not supported in the current version.', $driverName)), |
69
|
1 |
|
}; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string[] |
74
|
|
|
*/ |
75
|
2 |
|
public function getAvailableDrivers(): array |
76
|
|
|
{ |
77
|
2 |
|
return DriverManager::getAvailableDrivers(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|