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