shlinkio /
shlink-common
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Shlinkio\Shlink\Common\Doctrine; |
||
| 6 | |||
| 7 | use Doctrine\Common\Cache\ArrayCache; |
||
| 8 | use Doctrine\Common\Cache\Cache; |
||
| 9 | use Doctrine\DBAL\DBALException; |
||
| 10 | use Doctrine\DBAL\Types\Type; |
||
| 11 | use Doctrine\ORM\EntityManager; |
||
| 12 | use Doctrine\ORM\ORMException; |
||
| 13 | use Doctrine\ORM\Tools\Setup; |
||
| 14 | use Psr\Container\ContainerInterface; |
||
| 15 | use Shlinkio\Shlink\Common\Doctrine\Mapping\EnhancedPHPDriver; |
||
| 16 | |||
| 17 | class EntityManagerFactory |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @throws ORMException |
||
| 21 | * @throws DBALException |
||
| 22 | */ |
||
| 23 | 5 | public function __invoke(ContainerInterface $container): EntityManager |
|
| 24 | { |
||
| 25 | 5 | $globalConfig = $container->get('config'); |
|
| 26 | 5 | $isDevMode = (bool) ($globalConfig['debug'] ?? false); |
|
| 27 | 5 | $cache = $container->has(Cache::class) ? $container->get(Cache::class) : new ArrayCache(); |
|
| 28 | 5 | $emConfig = $globalConfig['entity_manager'] ?? []; |
|
| 29 | 5 | $connectionConfig = $emConfig['connection'] ?? []; |
|
| 30 | 5 | $ormConfig = $emConfig['orm'] ?? []; |
|
| 31 | 5 | $funcStyle = $ormConfig['load_mappings_using_functional_style'] ?? false; |
|
| 32 | |||
| 33 | 5 | $this->registerTypes($ormConfig); |
|
| 34 | |||
| 35 | 5 | $config = Setup::createConfiguration($isDevMode, $ormConfig['proxies_dir'] ?? null, $cache); |
|
| 36 | 5 | $config->setMetadataDriverImpl( |
|
| 37 | 5 | new EnhancedPHPDriver($ormConfig['entities_mappings'] ?? [], $emConfig, $funcStyle), |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 38 | ); |
||
| 39 | |||
| 40 | 5 | return EntityManager::create($connectionConfig, $config); |
|
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @throws DBALException |
||
| 45 | */ |
||
| 46 | 5 | private function registerTypes(array $ormConfig): void |
|
| 47 | { |
||
| 48 | 5 | $types = $ormConfig['types'] ?? []; |
|
| 49 | |||
| 50 | 5 | foreach ($types as $name => $className) { |
|
| 51 | 5 | if (! Type::hasType($name)) { |
|
| 52 | 5 | Type::addType($name, $className); |
|
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | } |
||
| 57 |