1 | <?php |
||
2 | /** |
||
3 | * @package EBloodBank |
||
4 | * @since 1.6 |
||
5 | */ |
||
6 | namespace EBloodBank; |
||
7 | |||
8 | use Doctrine; |
||
9 | use Doctrine\ORM; |
||
10 | use Psr\Container\ContainerInterface; |
||
11 | |||
12 | /** |
||
13 | * @since 1.6 |
||
14 | */ |
||
15 | class EntityManagerFactory |
||
16 | { |
||
17 | /** |
||
18 | * @param ContainerInterface $container |
||
19 | * @return \Doctrine\ORM\EntityManager |
||
20 | * @since 1.6 |
||
21 | */ |
||
22 | public function __invoke(ContainerInterface $container) |
||
23 | { |
||
24 | $config = ORM\Tools\Setup::createConfiguration((bool) EBB_DEV_MODE); |
||
25 | |||
26 | $entitiesPaths = [trimTrailingSlash(EBB_DIR) . '/src/EBloodBank/Models/']; |
||
27 | $driverImpl = $config->newDefaultAnnotationDriver($entitiesPaths, true); |
||
28 | $config->addEntityNamespace('Entities', 'EBloodBank\Models'); |
||
29 | $config->setMetadataDriverImpl($driverImpl); |
||
30 | |||
31 | $config->setProxyDir(trimTrailingSlash(EBB_DIR) . '/src/EBloodBank/Proxies/'); |
||
32 | $config->setAutoGenerateProxyClasses((bool) EBB_DEV_MODE); |
||
33 | $config->setProxyNamespace('EBloodBank\Proxies'); |
||
34 | |||
35 | $cacheDriver = $this->setupCacheDriver(); |
||
36 | $config->setMetadataCacheImpl($cacheDriver); |
||
37 | $config->setResultCacheImpl($cacheDriver); |
||
38 | $config->setQueryCacheImpl($cacheDriver); |
||
39 | |||
40 | $entityManager = ORM\EntityManager::create($container->get('db_connection'), $config); |
||
41 | |||
42 | return $entityManager; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @return \Doctrine\Common\Cache\Cache |
||
47 | * @since 1.6 |
||
48 | */ |
||
49 | protected function setupCacheDriver() |
||
50 | { |
||
51 | if (EBB_DEV_MODE) { |
||
52 | $cacheDriver = new Doctrine\Common\Cache\ArrayCache(); |
||
53 | } else { |
||
54 | if (EBB_REDIS_CACHE && extension_loaded('redis')) { |
||
55 | $redis = new Redis(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
56 | $redis->connect(EBB_REDIS_HOST, EBB_REDIS_PORT); |
||
57 | if (EBB_REDIS_PASS) { |
||
58 | $redis->auth(EBB_REDIS_PASS); |
||
59 | } |
||
60 | if (EBB_REDIS_DB) { |
||
61 | $redis->select(EBB_REDIS_DB); |
||
62 | } |
||
63 | $cacheDriver = new Doctrine\Common\Cache\RedisCache(); |
||
64 | $cacheDriver->setRedis($redis); |
||
65 | } elseif (EBB_APCU_CACHE && extension_loaded('apcu')) { |
||
66 | $cacheDriver = new Doctrine\Common\Cache\ApcuCache(); |
||
67 | } elseif (EBB_FS_CACHE && is_writable(EBB_CACHE_DIR)) { |
||
68 | $cacheDriver = new Doctrine\Common\Cache\FilesystemCache(EBB_CACHE_DIR, '.ebb.data'); |
||
69 | } else { |
||
70 | $cacheDriver = new Doctrine\Common\Cache\ArrayCache(); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | return $cacheDriver; |
||
75 | } |
||
76 | } |
||
77 |