-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce DoctrineSetup as a replacement for Setup
- Loading branch information
Showing
15 changed files
with
444 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Tools; | ||
|
||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use Doctrine\Common\Annotations\PsrCachedReader; | ||
use Doctrine\Deprecations\Deprecation; | ||
use Doctrine\ORM\Configuration; | ||
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 Memcached; | ||
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 class_exists; | ||
use function extension_loaded; | ||
use function md5; | ||
use function sys_get_temp_dir; | ||
|
||
final class DoctrineSetup | ||
{ | ||
/** | ||
* Creates a configuration with an annotation metadata driver. | ||
* | ||
* @param string[] $paths | ||
*/ | ||
public static function createAnnotationMetadataConfiguration( | ||
array $paths, | ||
bool $isDevMode = false, | ||
?string $proxyDir = null, | ||
?CacheItemPoolInterface $cache = null | ||
): Configuration { | ||
$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. | ||
* | ||
* @param string[] $paths | ||
*/ | ||
public static function createDefaultAnnotationDriver( | ||
array $paths = [], | ||
?CacheItemPoolInterface $cache = null | ||
): AnnotationDriver { | ||
$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 | ||
): Configuration { | ||
$config = self::createConfiguration($isDevMode, $proxyDir, $cache); | ||
$config->setMetadataDriverImpl(new XmlDriver($paths)); | ||
|
||
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')) { | ||
return new ApcuAdapter($namespace); | ||
} | ||
|
||
if (extension_loaded('memcached')) { | ||
$memcached = new Memcached(); | ||
$memcached->addServer('127.0.0.1', 11211); | ||
|
||
return new MemcachedAdapter($memcached, $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() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.