diff options
Diffstat (limited to 'vendor/doctrine/orm/src/ORMSetup.php')
| -rw-r--r-- | vendor/doctrine/orm/src/ORMSetup.php | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/ORMSetup.php b/vendor/doctrine/orm/src/ORMSetup.php new file mode 100644 index 0000000..7354c71 --- /dev/null +++ b/vendor/doctrine/orm/src/ORMSetup.php | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\ORM; | ||
| 6 | |||
| 7 | use Doctrine\ORM\Mapping\Driver\AttributeDriver; | ||
| 8 | use Doctrine\ORM\Mapping\Driver\XmlDriver; | ||
| 9 | use Psr\Cache\CacheItemPoolInterface; | ||
| 10 | use Redis; | ||
| 11 | use RuntimeException; | ||
| 12 | use Symfony\Component\Cache\Adapter\ApcuAdapter; | ||
| 13 | use Symfony\Component\Cache\Adapter\ArrayAdapter; | ||
| 14 | use Symfony\Component\Cache\Adapter\MemcachedAdapter; | ||
| 15 | use Symfony\Component\Cache\Adapter\RedisAdapter; | ||
| 16 | |||
| 17 | use function apcu_enabled; | ||
| 18 | use function class_exists; | ||
| 19 | use function extension_loaded; | ||
| 20 | use function md5; | ||
| 21 | use function sys_get_temp_dir; | ||
| 22 | |||
| 23 | final class ORMSetup | ||
| 24 | { | ||
| 25 | /** | ||
| 26 | * Creates a configuration with an attribute metadata driver. | ||
| 27 | * | ||
| 28 | * @param string[] $paths | ||
| 29 | */ | ||
| 30 | public static function createAttributeMetadataConfiguration( | ||
| 31 | array $paths, | ||
| 32 | bool $isDevMode = false, | ||
| 33 | string|null $proxyDir = null, | ||
| 34 | CacheItemPoolInterface|null $cache = null, | ||
| 35 | ): Configuration { | ||
| 36 | $config = self::createConfiguration($isDevMode, $proxyDir, $cache); | ||
| 37 | $config->setMetadataDriverImpl(new AttributeDriver($paths)); | ||
| 38 | |||
| 39 | return $config; | ||
| 40 | } | ||
| 41 | |||
| 42 | /** | ||
| 43 | * Creates a configuration with an XML metadata driver. | ||
| 44 | * | ||
| 45 | * @param string[] $paths | ||
| 46 | */ | ||
| 47 | public static function createXMLMetadataConfiguration( | ||
| 48 | array $paths, | ||
| 49 | bool $isDevMode = false, | ||
| 50 | string|null $proxyDir = null, | ||
| 51 | CacheItemPoolInterface|null $cache = null, | ||
| 52 | bool $isXsdValidationEnabled = true, | ||
| 53 | ): Configuration { | ||
| 54 | $config = self::createConfiguration($isDevMode, $proxyDir, $cache); | ||
| 55 | $config->setMetadataDriverImpl(new XmlDriver($paths, XmlDriver::DEFAULT_FILE_EXTENSION, $isXsdValidationEnabled)); | ||
| 56 | |||
| 57 | return $config; | ||
| 58 | } | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Creates a configuration without a metadata driver. | ||
| 62 | */ | ||
| 63 | public static function createConfiguration( | ||
| 64 | bool $isDevMode = false, | ||
| 65 | string|null $proxyDir = null, | ||
| 66 | CacheItemPoolInterface|null $cache = null, | ||
| 67 | ): Configuration { | ||
| 68 | $proxyDir = $proxyDir ?: sys_get_temp_dir(); | ||
| 69 | |||
| 70 | $cache = self::createCacheInstance($isDevMode, $proxyDir, $cache); | ||
| 71 | |||
| 72 | $config = new Configuration(); | ||
| 73 | |||
| 74 | $config->setMetadataCache($cache); | ||
| 75 | $config->setQueryCache($cache); | ||
| 76 | $config->setResultCache($cache); | ||
| 77 | $config->setProxyDir($proxyDir); | ||
| 78 | $config->setProxyNamespace('DoctrineProxies'); | ||
| 79 | $config->setAutoGenerateProxyClasses($isDevMode); | ||
| 80 | |||
| 81 | return $config; | ||
| 82 | } | ||
| 83 | |||
| 84 | private static function createCacheInstance( | ||
| 85 | bool $isDevMode, | ||
| 86 | string $proxyDir, | ||
| 87 | CacheItemPoolInterface|null $cache, | ||
| 88 | ): CacheItemPoolInterface { | ||
| 89 | if ($cache !== null) { | ||
| 90 | return $cache; | ||
| 91 | } | ||
| 92 | |||
| 93 | if (! class_exists(ArrayAdapter::class)) { | ||
| 94 | throw new RuntimeException( | ||
| 95 | 'The Doctrine setup tool cannot configure caches without symfony/cache.' | ||
| 96 | . ' Please add symfony/cache as explicit dependency or pass your own cache implementation.', | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | |||
| 100 | if ($isDevMode) { | ||
| 101 | return new ArrayAdapter(); | ||
| 102 | } | ||
| 103 | |||
| 104 | $namespace = 'dc2_' . md5($proxyDir); | ||
| 105 | |||
| 106 | if (extension_loaded('apcu') && apcu_enabled()) { | ||
| 107 | return new ApcuAdapter($namespace); | ||
| 108 | } | ||
| 109 | |||
| 110 | if (MemcachedAdapter::isSupported()) { | ||
| 111 | return new MemcachedAdapter(MemcachedAdapter::createConnection('memcached://127.0.0.1'), $namespace); | ||
| 112 | } | ||
| 113 | |||
| 114 | if (extension_loaded('redis')) { | ||
| 115 | $redis = new Redis(); | ||
| 116 | $redis->connect('127.0.0.1'); | ||
| 117 | |||
| 118 | return new RedisAdapter($redis, $namespace); | ||
| 119 | } | ||
| 120 | |||
| 121 | return new ArrayAdapter(); | ||
| 122 | } | ||
| 123 | |||
| 124 | private function __construct() | ||
| 125 | { | ||
| 126 | } | ||
| 127 | } | ||
