1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Tools; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\ApcuCache; |
8
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
9
|
|
|
use Doctrine\Common\Cache\Cache; |
10
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
11
|
|
|
use Doctrine\Common\Cache\MemcachedCache; |
12
|
|
|
use Doctrine\Common\Cache\RedisCache; |
13
|
|
|
use Doctrine\ORM\Configuration; |
14
|
|
|
use Doctrine\ORM\Mapping\Driver\XmlDriver; |
15
|
|
|
use Memcached; |
16
|
|
|
use Redis; |
17
|
|
|
use function extension_loaded; |
18
|
|
|
use function md5; |
19
|
|
|
use function sys_get_temp_dir; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Convenience class for setting up Doctrine from different installations and configurations. |
23
|
|
|
*/ |
24
|
|
|
class Setup |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Creates a configuration with an annotation metadata driver. |
28
|
|
|
* |
29
|
|
|
* @param string[] $paths |
30
|
|
|
* @param bool $isDevMode |
31
|
|
|
* @param string $proxyDir |
32
|
|
|
* |
33
|
|
|
* @return Configuration |
34
|
|
|
*/ |
35
|
3 |
|
public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, ?Cache $cache = null) |
36
|
|
|
{ |
37
|
3 |
|
$config = self::createConfiguration($isDevMode, $proxyDir, $cache); |
38
|
3 |
|
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths)); |
39
|
|
|
|
40
|
3 |
|
return $config; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Creates a configuration with a xml metadata driver. |
45
|
|
|
* |
46
|
|
|
* @param string[] $paths |
47
|
|
|
* @param bool $isDevMode |
48
|
|
|
* @param string $proxyDir |
49
|
|
|
* |
50
|
|
|
* @return Configuration |
51
|
|
|
*/ |
52
|
1 |
|
public static function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, ?Cache $cache = null) |
53
|
|
|
{ |
54
|
1 |
|
$config = self::createConfiguration($isDevMode, $proxyDir, $cache); |
55
|
1 |
|
$config->setMetadataDriverImpl(new XmlDriver($paths)); |
56
|
|
|
|
57
|
1 |
|
return $config; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Creates a configuration without a metadata driver. |
62
|
|
|
* |
63
|
|
|
* @param bool $isDevMode |
64
|
|
|
* @param string $proxyDir |
65
|
|
|
* |
66
|
|
|
* @return Configuration |
67
|
|
|
*/ |
68
|
8 |
|
public static function createConfiguration($isDevMode = false, $proxyDir = null, ?Cache $cache = null) |
69
|
|
|
{ |
70
|
8 |
|
$proxyDir = $proxyDir ?: sys_get_temp_dir(); |
71
|
|
|
|
72
|
8 |
|
$cache = self::createCacheConfiguration($isDevMode, $proxyDir, $cache); |
73
|
|
|
|
74
|
8 |
|
$config = new Configuration(); |
75
|
8 |
|
$config->setMetadataCacheImpl($cache); |
76
|
8 |
|
$config->setQueryCacheImpl($cache); |
77
|
8 |
|
$config->setResultCacheImpl($cache); |
78
|
8 |
|
$config->setProxyDir($proxyDir); |
79
|
8 |
|
$config->setProxyNamespace('DoctrineProxies'); |
80
|
8 |
|
$config->setAutoGenerateProxyClasses($isDevMode); |
81
|
|
|
|
82
|
8 |
|
return $config; |
83
|
|
|
} |
84
|
|
|
|
85
|
8 |
|
private static function createCacheConfiguration(bool $isDevMode, string $proxyDir, ?Cache $cache) : Cache |
86
|
|
|
{ |
87
|
8 |
|
$cache = self::createCacheInstance($isDevMode, $cache); |
88
|
|
|
|
89
|
8 |
|
if (! $cache instanceof CacheProvider) { |
90
|
1 |
|
return $cache; |
91
|
|
|
} |
92
|
|
|
|
93
|
7 |
|
$namespace = $cache->getNamespace(); |
94
|
|
|
|
95
|
7 |
|
if ($namespace !== '') { |
96
|
1 |
|
$namespace .= ':'; |
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
$cache->setNamespace($namespace . 'dc2_' . md5($proxyDir) . '_'); // to avoid collisions |
100
|
|
|
|
101
|
7 |
|
return $cache; |
102
|
|
|
} |
103
|
|
|
|
104
|
8 |
|
private static function createCacheInstance(bool $isDevMode, ?Cache $cache) : Cache |
105
|
|
|
{ |
106
|
8 |
|
if ($cache !== null) { |
107
|
4 |
|
return $cache; |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
if ($isDevMode === true) { |
111
|
3 |
|
return new ArrayCache(); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
if (extension_loaded('apcu')) { |
115
|
|
|
return new ApcuCache(); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
if (extension_loaded('memcached')) { |
119
|
|
|
$memcached = new Memcached(); |
120
|
|
|
$memcached->addServer('127.0.0.1', 11211); |
121
|
|
|
|
122
|
|
|
$cache = new MemcachedCache(); |
123
|
|
|
$cache->setMemcached($memcached); |
124
|
|
|
|
125
|
|
|
return $cache; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
if (extension_loaded('redis')) { |
129
|
|
|
$redis = new Redis(); |
130
|
|
|
$redis->connect('127.0.0.1'); |
131
|
|
|
|
132
|
|
|
$cache = new RedisCache(); |
133
|
|
|
$cache->setRedis($redis); |
134
|
|
|
|
135
|
|
|
return $cache; |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
return new ArrayCache(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|