-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathORMSetup.php
222 lines (189 loc) · 6.76 KB
/
ORMSetup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
declare(strict_types=1);
namespace Doctrine\ORM;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Doctrine\ORM\Mapping\Driver\YamlDriver;
use LogicException;
use Psr\Cache\CacheItemPoolInterface;
use Redis;
use RuntimeException;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use function apcu_enabled;
use function class_exists;
use function extension_loaded;
use function md5;
use function sprintf;
use function sys_get_temp_dir;
final class ORMSetup
{
/**
* Creates a configuration with an annotation metadata driver.
*
* @deprecated Use another mapping driver.
*
* @param string[] $paths
*/
public static function createAnnotationMetadataConfiguration(
array $paths,
bool $isDevMode = false,
?string $proxyDir = null,
?CacheItemPoolInterface $cache = null
): Configuration {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/issues/10098',
'%s is deprecated and will be removed in Doctrine ORM 3.0',
__METHOD__
);
$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(self::createDefaultAnnotationDriver($paths));
return $config;
}
/**
* Adds a new default annotation driver with a correctly configured annotation reader.
*
* @deprecated Use another mapping driver.
*
* @param string[] $paths
*/
public static function createDefaultAnnotationDriver(
array $paths = [],
?CacheItemPoolInterface $cache = null
): AnnotationDriver {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/issues/10098',
'%s is deprecated and will be removed in Doctrine ORM 3.0',
__METHOD__
);
if (! class_exists(AnnotationReader::class)) {
throw new LogicException(sprintf(
'The annotation metadata driver cannot be enabled because the "doctrine/annotations" library'
. ' is not installed. Please run "composer require doctrine/annotations" or choose a different'
. ' metadata driver.'
));
}
$reader = new AnnotationReader();
if ($cache === null && class_exists(ArrayAdapter::class)) {
$cache = new ArrayAdapter();
}
if ($cache !== null) {
$reader = new PsrCachedReader($reader, $cache);
}
return new AnnotationDriver($reader, $paths);
}
/**
* Creates a configuration with an attribute metadata driver.
*
* @param string[] $paths
*/
public static function createAttributeMetadataConfiguration(
array $paths,
bool $isDevMode = false,
?string $proxyDir = null,
?CacheItemPoolInterface $cache = null
): Configuration {
$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(new AttributeDriver($paths));
return $config;
}
/**
* Creates a configuration with an XML metadata driver.
*
* @param string[] $paths
*/
public static function createXMLMetadataConfiguration(
array $paths,
bool $isDevMode = false,
?string $proxyDir = null,
?CacheItemPoolInterface $cache = null,
bool $isXsdValidationEnabled = false
): Configuration {
$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(new XmlDriver($paths, XmlDriver::DEFAULT_FILE_EXTENSION, $isXsdValidationEnabled));
return $config;
}
/**
* Creates a configuration with a YAML metadata driver.
*
* @deprecated YAML metadata mapping is deprecated and will be removed in 3.0
*
* @param string[] $paths
*/
public static function createYAMLMetadataConfiguration(
array $paths,
bool $isDevMode = false,
?string $proxyDir = null,
?CacheItemPoolInterface $cache = null
): Configuration {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/issues/8465',
'YAML mapping driver is deprecated and will be removed in Doctrine ORM 3.0, please migrate to attribute or XML driver.'
);
$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(new YamlDriver($paths));
return $config;
}
/**
* Creates a configuration without a metadata driver.
*/
public static function createConfiguration(
bool $isDevMode = false,
?string $proxyDir = null,
?CacheItemPoolInterface $cache = null
): Configuration {
$proxyDir = $proxyDir ?: sys_get_temp_dir();
$cache = self::createCacheInstance($isDevMode, $proxyDir, $cache);
$config = new Configuration();
$config->setMetadataCache($cache);
$config->setQueryCache($cache);
$config->setResultCache($cache);
$config->setProxyDir($proxyDir);
$config->setProxyNamespace('DoctrineProxies');
$config->setAutoGenerateProxyClasses($isDevMode);
return $config;
}
private static function createCacheInstance(
bool $isDevMode,
string $proxyDir,
?CacheItemPoolInterface $cache
): CacheItemPoolInterface {
if ($cache !== null) {
return $cache;
}
if (! class_exists(ArrayAdapter::class)) {
throw new RuntimeException(
'The Doctrine setup tool cannot configure caches without symfony/cache.'
. ' Please add symfony/cache as explicit dependency or pass your own cache implementation.'
);
}
if ($isDevMode) {
return new ArrayAdapter();
}
$namespace = 'dc2_' . md5($proxyDir);
if (extension_loaded('apcu') && apcu_enabled()) {
return new ApcuAdapter($namespace);
}
if (MemcachedAdapter::isSupported()) {
return new MemcachedAdapter(MemcachedAdapter::createConnection('memcached://127.0.0.1'), $namespace);
}
if (extension_loaded('redis')) {
$redis = new Redis();
$redis->connect('127.0.0.1');
return new RedisAdapter($redis, $namespace);
}
return new ArrayAdapter();
}
private function __construct()
{
}
}