|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the API Platform project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Test; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
17
|
|
|
use Doctrine\Common\Cache\Cache; |
|
18
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
|
19
|
|
|
use Doctrine\ODM\MongoDB\Configuration; |
|
20
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Convenience class for setting up Doctrine from different installations and configurations. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Benjamin Eberlei <[email protected]> |
|
26
|
|
|
* @author Alan Poulain <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class DoctrineMongoDbOdmSetup |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Creates a configuration with an annotation metadata driver. |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function createAnnotationMetadataConfiguration(array $paths, bool $isDevMode = false, string $proxyDir = null, string $hydratorDir = null, Cache $cache = null): Configuration |
|
34
|
|
|
{ |
|
35
|
|
|
$config = self::createConfiguration($isDevMode, $proxyDir, $hydratorDir, $cache); |
|
36
|
|
|
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths)); |
|
37
|
|
|
|
|
38
|
|
|
return $config; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates a configuration with a xml metadata driver. |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function createXMLMetadataConfiguration(array $paths, bool $isDevMode = false, string $proxyDir = null, string $hydratorDir = null, Cache $cache = null): Configuration |
|
45
|
|
|
{ |
|
46
|
|
|
$config = self::createConfiguration($isDevMode, $proxyDir, $hydratorDir, $cache); |
|
47
|
|
|
$config->setMetadataDriverImpl(new XmlDriver($paths)); |
|
48
|
|
|
|
|
49
|
|
|
return $config; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Creates a configuration without a metadata driver. |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function createConfiguration(bool $isDevMode = false, string $proxyDir = null, string $hydratorDir = null, Cache $cache = null): Configuration |
|
56
|
|
|
{ |
|
57
|
|
|
$proxyDir = $proxyDir ?: sys_get_temp_dir(); |
|
58
|
|
|
$hydratorDir = $hydratorDir ?: sys_get_temp_dir(); |
|
59
|
|
|
|
|
60
|
|
|
$cache = self::createCacheConfiguration($isDevMode, $proxyDir, $hydratorDir, $cache); |
|
61
|
|
|
|
|
62
|
|
|
$config = new Configuration(); |
|
63
|
|
|
$config->setMetadataCacheImpl($cache); |
|
64
|
|
|
$config->setProxyDir($proxyDir); |
|
65
|
|
|
$config->setHydratorDir($hydratorDir); |
|
66
|
|
|
$config->setProxyNamespace('DoctrineProxies'); |
|
67
|
|
|
$config->setHydratorNamespace('DoctrineHydrators'); |
|
68
|
|
|
$config->setAutoGenerateProxyClasses($isDevMode ? Configuration::AUTOGENERATE_EVAL : Configuration::AUTOGENERATE_FILE_NOT_EXISTS); |
|
69
|
|
|
|
|
70
|
|
|
return $config; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private static function createCacheConfiguration(bool $isDevMode, string $proxyDir, string $hydratorDir, ?Cache $cache): Cache |
|
74
|
|
|
{ |
|
75
|
|
|
$cache = self::createCacheInstance($isDevMode, $cache); |
|
76
|
|
|
|
|
77
|
|
|
if (!$cache instanceof CacheProvider) { |
|
78
|
|
|
return $cache; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$namespace = $cache->getNamespace(); |
|
82
|
|
|
|
|
83
|
|
|
if ('' !== $namespace) { |
|
84
|
|
|
$namespace .= ':'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$cache->setNamespace($namespace.'dc2_'.md5($proxyDir.$hydratorDir).'_'); // to avoid collisions |
|
88
|
|
|
|
|
89
|
|
|
return $cache; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private static function createCacheInstance(bool $isDevMode, ?Cache $cache): Cache |
|
93
|
|
|
{ |
|
94
|
|
|
if (null !== $cache) { |
|
95
|
|
|
return $cache; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (true === $isDevMode) { |
|
99
|
|
|
return new ArrayCache(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (\extension_loaded('apcu')) { |
|
103
|
|
|
return new \Doctrine\Common\Cache\ApcuCache(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return new ArrayCache(); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|