1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Slim3 Doctrine integration (https://github.com/juliangut/slim-doctrine) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/juliangut/slim-doctrine for the canonical source repository |
6
|
|
|
* |
7
|
|
|
* @license https://raw.githubusercontent.com/juliangut/slim-doctrine/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Jgut\Slim\Doctrine; |
11
|
|
|
|
12
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
13
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
14
|
|
|
use Doctrine\Common\Cache\ApcuCache; |
15
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
16
|
|
|
use Doctrine\Common\Cache\MemcacheCache; |
17
|
|
|
use Doctrine\Common\Cache\RedisCache; |
18
|
|
|
use Doctrine\Common\Cache\XcacheCache; |
19
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver; |
20
|
|
|
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Doctrine Object Manager commons. |
24
|
|
|
*/ |
25
|
|
|
trait ObjectManagerTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param \Doctrine\Common\Cache\CacheProvider|null $cacheDriver |
29
|
|
|
* @param string $cacheNamespace |
30
|
|
|
* |
31
|
|
|
* @throws \InvalidArgumentException |
32
|
|
|
* |
33
|
|
|
* @return \Doctrine\Common\Cache\CacheProvider |
34
|
|
|
*/ |
35
|
|
|
protected static function getCacheDriver($cacheDriver, $cacheNamespace) |
36
|
|
|
{ |
37
|
|
|
if ($cacheDriver === null) { |
38
|
|
|
// @codeCoverageIgnoreStart |
39
|
|
|
if (extension_loaded('apc')) { |
40
|
|
|
$cacheDriver = new ApcuCache; |
41
|
|
|
} elseif (extension_loaded('xcache')) { |
42
|
|
|
$cacheDriver = new XcacheCache; |
43
|
|
|
} elseif (extension_loaded('memcache')) { |
44
|
|
|
$memcache = new \Memcache; |
45
|
|
|
$memcache->connect('127.0.0.1'); |
46
|
|
|
|
47
|
|
|
$cacheDriver = new MemcacheCache; |
48
|
|
|
$cacheDriver->setMemcache($memcache); |
49
|
|
|
} elseif (extension_loaded('redis')) { |
50
|
|
|
$redis = new \Redis(); |
51
|
|
|
$redis->connect('127.0.0.1'); |
52
|
|
|
|
53
|
|
|
$cacheDriver = new RedisCache; |
54
|
|
|
$cacheDriver->setRedis($redis); |
55
|
|
|
} else { |
56
|
|
|
$cacheDriver = new ArrayCache; |
57
|
|
|
} |
58
|
|
|
// @codeCoverageIgnoreEnd |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!$cacheDriver instanceof CacheProvider) { |
62
|
|
|
throw new \InvalidArgumentException('Cache Driver provided is not valid'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$cacheDriver->setNamespace( |
66
|
|
|
trim($cacheNamespace) !== '' ? trim($cacheNamespace) : 'dc2_' . sha1(sys_get_temp_dir()) . '_' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
return $cacheDriver; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Set up annotation metadata. |
74
|
|
|
* |
75
|
|
|
* @param array $options |
76
|
|
|
* |
77
|
|
|
* @throws \InvalidArgumentException |
78
|
|
|
*/ |
79
|
|
|
protected static function setupAnnotationMetadata(array $options) |
80
|
|
|
{ |
81
|
|
|
foreach ($options['annotation_files'] as $file) { |
82
|
|
|
AnnotationRegistry::registerFile($file); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
AnnotationRegistry::registerAutoloadNamespaces($options['annotation_namespaces']); |
86
|
|
|
|
87
|
|
|
foreach ($options['annotation_autoloaders'] as $autoLoader) { |
88
|
|
|
AnnotationRegistry::registerLoader($autoLoader); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
AnnotationRegistry::registerLoader('class_exists'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Setup metadata driver. |
96
|
|
|
* |
97
|
|
|
* @param \Doctrine\ORM\Configuration|\Doctrine\ODM\MongoDB\Configuration $config |
98
|
|
|
* @param \Doctrine\Common\Persistence\Mapping\Driver\MappingDriver $driver |
99
|
|
|
* |
100
|
|
|
* @throws \RuntimeException |
101
|
|
|
*/ |
102
|
|
|
protected static function setupMetadataDriver($config, MappingDriver $driver) |
103
|
|
|
{ |
104
|
|
|
$metadataDriver = new MappingDriverChain; |
105
|
|
|
$metadataDriver->setDefaultDriver($driver); |
106
|
|
|
|
107
|
|
|
$config->setMetadataDriverImpl($metadataDriver); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Setup proxies. |
112
|
|
|
* |
113
|
|
|
* @param \Doctrine\ORM\Configuration|\Doctrine\ODM\MongoDB\Configuration $config |
114
|
|
|
* @param array $options |
115
|
|
|
* |
116
|
|
|
* @throws \InvalidArgumentException |
117
|
|
|
*/ |
118
|
|
|
protected static function setupProxy($config, array $options) |
119
|
|
|
{ |
120
|
|
|
$config->setProxyDir((string) ($options['proxy_path'] ?: sys_get_temp_dir())); |
121
|
|
|
|
122
|
|
|
$config->setProxyNamespace((string) $options['proxies_namespace']); |
123
|
|
|
|
124
|
|
|
$config->setAutoGenerateProxyClasses((int) $options['auto_generate_proxies']); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|