1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Shlinkio\Shlink\Common\Factory; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\PHPDriver; |
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 Interop\Container\ContainerInterface; |
15
|
|
|
use Shlinkio\Shlink\Common\Type\ChronosDateTimeType; |
16
|
|
|
use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
17
|
|
|
use Zend\ServiceManager\Exception\ServiceNotFoundException; |
18
|
|
|
use Zend\ServiceManager\Factory\FactoryInterface; |
19
|
|
|
|
20
|
|
|
class EntityManagerFactory implements FactoryInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
24
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when creating a service. |
25
|
|
|
* @throws ORMException |
26
|
|
|
* @throws DBALException |
27
|
|
|
*/ |
28
|
1 |
|
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null) |
29
|
|
|
{ |
30
|
1 |
|
$globalConfig = $container->get('config'); |
31
|
1 |
|
$isDevMode = isset($globalConfig['debug']) ? ((bool) $globalConfig['debug']) : false; |
32
|
1 |
|
$cache = $container->has(Cache::class) ? $container->get(Cache::class) : new ArrayCache(); |
33
|
1 |
|
$emConfig = $globalConfig['entity_manager'] ?? []; |
34
|
1 |
|
$connectionConfig = $emConfig['connection'] ?? []; |
35
|
1 |
|
$ormConfig = $emConfig['orm'] ?? []; |
36
|
|
|
|
37
|
1 |
|
if (! Type::hasType(ChronosDateTimeType::CHRONOS_DATETIME)) { |
38
|
1 |
|
Type::addType(ChronosDateTimeType::CHRONOS_DATETIME, ChronosDateTimeType::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
$config = Setup::createConfiguration($isDevMode, $ormConfig['proxies_dir'] ?? null, $cache); |
42
|
1 |
|
$config->setMetadataDriverImpl(new PHPDriver($ormConfig['entities_mappings'] ?? [])); |
43
|
|
|
|
44
|
1 |
|
return EntityManager::create($connectionConfig, $config); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|