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\MappingDriverChain; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Doctrine Object Manager commons. |
23
|
|
|
*/ |
24
|
|
|
trait ObjectManagerTrait |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param \Doctrine\Common\Cache\CacheProvider|null $cacheDriver |
28
|
|
|
* @param string $cacheNamespace |
29
|
|
|
* |
30
|
|
|
* @throws \InvalidArgumentException |
31
|
|
|
* |
32
|
|
|
* @return \Doctrine\Common\Cache\CacheProvider |
33
|
|
|
*/ |
34
|
|
|
protected static function getCacheDriver($cacheDriver, $cacheNamespace) |
35
|
|
|
{ |
36
|
|
|
if ($cacheDriver === null) { |
37
|
|
|
// @codeCoverageIgnoreStart |
38
|
|
|
if (extension_loaded('apc')) { |
39
|
|
|
$cacheDriver = new ApcuCache; |
40
|
|
|
} elseif (extension_loaded('xcache')) { |
41
|
|
|
$cacheDriver = new XcacheCache; |
42
|
|
|
} elseif (extension_loaded('memcache')) { |
43
|
|
|
$memcache = new \Memcache; |
44
|
|
|
$memcache->connect('127.0.0.1'); |
45
|
|
|
|
46
|
|
|
$cacheDriver = new MemcacheCache; |
47
|
|
|
$cacheDriver->setMemcache($memcache); |
48
|
|
|
} elseif (extension_loaded('redis')) { |
49
|
|
|
$redis = new \Redis(); |
50
|
|
|
$redis->connect('127.0.0.1'); |
51
|
|
|
|
52
|
|
|
$cacheDriver = new RedisCache; |
53
|
|
|
$cacheDriver->setRedis($redis); |
54
|
|
|
} else { |
55
|
|
|
$cacheDriver = new ArrayCache; |
56
|
|
|
} |
57
|
|
|
// @codeCoverageIgnoreEnd |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!$cacheDriver instanceof CacheProvider) { |
61
|
|
|
throw new \InvalidArgumentException('Cache Driver provided is not valid'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$cacheDriver->setNamespace(trim($cacheNamespace) !== '' ? trim($cacheNamespace) : 'dc2_' . sha1(time()) . '_'); |
65
|
|
|
|
66
|
|
|
return $cacheDriver; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set up annotation metadata. |
71
|
|
|
* |
72
|
|
|
* @param array $options |
73
|
|
|
* |
74
|
|
|
* @throws \InvalidArgumentException |
75
|
|
|
*/ |
76
|
|
|
protected static function setupAnnotationMetadata(array $options) |
77
|
|
|
{ |
78
|
|
|
foreach ($options['annotation_files'] as $file) { |
79
|
|
|
AnnotationRegistry::registerFile($file); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
AnnotationRegistry::registerAutoloadNamespaces($options['annotation_namespaces']); |
83
|
|
|
|
84
|
|
|
foreach ($options['annotation_autoloaders'] as $autoLoader) { |
85
|
|
|
AnnotationRegistry::registerLoader($autoLoader); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Create Doctrine ODM configuration. |
91
|
|
|
* |
92
|
|
|
* @param \Doctrine\ORM\Configuration|\Doctrine\ODM\MongoDB\Configuration $config |
93
|
|
|
* @param array $options |
94
|
|
|
* |
95
|
|
|
* @throws \RuntimeException |
96
|
|
|
*/ |
97
|
|
|
protected static function setupMetadataDriver($config, array $options) |
98
|
|
|
{ |
99
|
|
|
$metadataDriver = new MappingDriverChain; |
100
|
|
|
$metadataDriver->setDefaultDriver(self::getMetadataDriver($options)); |
101
|
|
|
|
102
|
|
|
$config->setMetadataDriverImpl($metadataDriver); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Setup proxies. |
107
|
|
|
* |
108
|
|
|
* @param \Doctrine\ORM\Configuration|\Doctrine\ODM\MongoDB\Configuration $config |
109
|
|
|
* @param array $options |
110
|
|
|
* |
111
|
|
|
* @throws \InvalidArgumentException |
112
|
|
|
*/ |
113
|
|
|
protected static function setupProxy($config, array $options) |
114
|
|
|
{ |
115
|
|
|
$config->setProxyDir((string) ($options['proxy_path'] ?: sys_get_temp_dir())); |
116
|
|
|
|
117
|
|
|
$config->setProxyNamespace((string) $options['proxies_namespace']); |
118
|
|
|
|
119
|
|
|
$config->setAutoGenerateProxyClasses((int) $options['auto_generate_proxies']); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|