forked from doctrine/orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 2.12.x: Introduce DoctrineSetup as a replacement for Setup (doctrine#9443) Introduce __unserialize behaviour in docs (doctrine#9390) Adapt test logic to PHP and SQLite II (doctrine#9442) Use the identify generator strategy Added php 8.1 to CI Psalm 4.19.0, PHPStan 1.4.3 (doctrine#9438) Ignore PHPUnit result cache everywhere (doctrine#9425)
- Loading branch information
Showing
23 changed files
with
489 additions
and
88 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
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,172 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Tools; | ||
|
||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use Doctrine\Common\Annotations\PsrCachedReader; | ||
use Doctrine\ORM\Configuration; | ||
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; | ||
use Doctrine\ORM\Mapping\Driver\AttributeDriver; | ||
use Doctrine\ORM\Mapping\Driver\XmlDriver; | ||
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 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() | ||
{ | ||
} | ||
} |
Oops, something went wrong.