Skip to content

Commit

Permalink
Introduce DoctrineSetup as a replacement for Setup (#9443)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Jan 30, 2022
1 parent e9e54d8 commit f81980e
Show file tree
Hide file tree
Showing 15 changed files with 447 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"
extensions: "pdo, pdo_sqlite"
extensions: "apcu, pdo, pdo_sqlite"
coverage: "pcov"
ini-values: "zend.assertions=1"

Expand Down
14 changes: 14 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Upgrade to 2.12

## Deprecate `Doctrine\ORM\Configuration::newDefaultAnnotationDriver`

This functionality has been moved to the new `DoctrineSetup` class. Call
`Doctrine\ORM\Tools\DoctrineSetup::createDefaultAnnotationDriver()` to create
a new annotation driver.

## Deprecate `Doctrine\ORM\Tools\Setup`

In our effort to migrate from Doctrine Cache to PSR-6, the `Setup` class which
accepted a Doctrine Cache instance in each method has been deprecated.

The replacement is `Doctrine\ORM\Tools\DoctrineSetup` which accepts a PSR-6
cache instead.

## Deprecate `Doctrine\ORM\Cache\MultiGetRegion`

The interface will be merged with `Doctrine\ORM\Cache\Region` in 3.0.
Expand Down
11 changes: 11 additions & 0 deletions lib/Doctrine/ORM/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
use Doctrine\ORM\Repository\RepositoryFactory;
use Doctrine\ORM\Tools\DoctrineSetup;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\ObjectRepository;
use Psr\Cache\CacheItemPoolInterface;
Expand Down Expand Up @@ -154,6 +155,8 @@ public function setMetadataDriverImpl(MappingDriver $driverImpl)
* Adds a new default annotation driver with a correctly configured annotation reader. If $useSimpleAnnotationReader
* is true, the notation `@Entity` will work, otherwise, the notation `@ORM\Entity` will be supported.
*
* @deprecated Use {@see DoctrineSetup::createDefaultAnnotationDriver()} instead.
*
* @param string|string[] $paths
* @param bool $useSimpleAnnotationReader
* @psalm-param string|list<string> $paths
Expand All @@ -162,6 +165,14 @@ public function setMetadataDriverImpl(MappingDriver $driverImpl)
*/
public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationReader = true)
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9443',
'%s is deprecated, call %s::createDefaultAnnotationDriver() instead.',
__METHOD__,
DoctrineSetup::class
);

AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php');

if ($useSimpleAnnotationReader) {
Expand Down
199 changes: 199 additions & 0 deletions lib/Doctrine/ORM/Tools/DoctrineSetup.php
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()
{
}
}
68 changes: 51 additions & 17 deletions lib/Doctrine/ORM/Tools/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

/**
* Convenience class for setting up Doctrine from different installations and configurations.
*
* @deprecated Use {@see DoctrineSetup} instead.
*/
class Setup
{
Expand Down Expand Up @@ -62,15 +64,23 @@ public static function registerAutoloadDirectory($directory)
/**
* Creates a configuration with an annotation metadata driver.
*
* @param mixed[] $paths
* @param bool $isDevMode
* @param string $proxyDir
* @param bool $useSimpleAnnotationReader
* @param string[] $paths
* @param bool $isDevMode
* @param string|null $proxyDir
* @param bool $useSimpleAnnotationReader
*
* @return Configuration
*/
public static function createAnnotationMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, ?Cache $cache = null, $useSimpleAnnotationReader = true)
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9443',
'%s is deprecated and will be removed in Doctrine 3.0, please use %s instead.',
self::class,
DoctrineSetup::class
);

$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl($config->newDefaultAnnotationDriver($paths, $useSimpleAnnotationReader));

Expand All @@ -80,47 +90,63 @@ public static function createAnnotationMetadataConfiguration(array $paths, $isDe
/**
* Creates a configuration with an attribute metadata driver.
*
* @param mixed[] $paths
* @param bool $isDevMode
* @param string $proxyDir
* @param string[] $paths
* @param bool $isDevMode
* @param string|null $proxyDir
*/
public static function createAttributeMetadataConfiguration(
array $paths,
$isDevMode = false,
$proxyDir = null,
?Cache $cache = null
): Configuration {
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9443',
'%s is deprecated and will be removed in Doctrine 3.0, please use %s instead.',
self::class,
DoctrineSetup::class
);

$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(new AttributeDriver($paths));

return $config;
}

/**
* Creates a configuration with a xml metadata driver.
* Creates a configuration with an XML metadata driver.
*
* @param mixed[] $paths
* @param bool $isDevMode
* @param string $proxyDir
* @param string[] $paths
* @param bool $isDevMode
* @param string|null $proxyDir
*
* @return Configuration
*/
public static function createXMLMetadataConfiguration(array $paths, $isDevMode = false, $proxyDir = null, ?Cache $cache = null)
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9443',
'%s is deprecated and will be removed in Doctrine 3.0, please use %s instead.',
self::class,
DoctrineSetup::class
);

$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(new XmlDriver($paths));

return $config;
}

/**
* Creates a configuration with a yaml metadata driver.
* Creates a configuration with a YAML metadata driver.
*
* @deprecated YAML metadata mapping is deprecated and will be removed in 3.0
*
* @param mixed[] $paths
* @param bool $isDevMode
* @param string $proxyDir
* @param string[] $paths
* @param bool $isDevMode
* @param string|null $proxyDir
*
* @return Configuration
*/
Expand All @@ -141,13 +167,21 @@ public static function createYAMLMetadataConfiguration(array $paths, $isDevMode
/**
* Creates a configuration without a metadata driver.
*
* @param bool $isDevMode
* @param string $proxyDir
* @param bool $isDevMode
* @param string|null $proxyDir
*
* @return Configuration
*/
public static function createConfiguration($isDevMode = false, $proxyDir = null, ?Cache $cache = null)
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9443',
'%s is deprecated and will be removed in Doctrine 3.0, please use %s instead.',
self::class,
DoctrineSetup::class
);

$proxyDir = $proxyDir ?: sys_get_temp_dir();

$cache = self::createCacheConfiguration($isDevMode, $proxyDir, $cache);
Expand Down
Loading

0 comments on commit f81980e

Please sign in to comment.