Skip to content

Commit 60ff966

Browse files
authored
Merge pull request #12022 from greg0ire/depr-proxy-dir
Provide upgrade path to new ORMSetup::create* signature
2 parents 3368425 + 76852cf commit 60ff966

File tree

6 files changed

+128
-18
lines changed

6 files changed

+128
-18
lines changed

UPGRADE.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ objects have been deprecated on PHP 8.4+:
2121
- `Doctrine\ORM\Configuration::setAutoGenerateProxyClasses()`
2222
- `Doctrine\ORM\Configuration::setProxyDir()`
2323
- `Doctrine\ORM\Configuration::setProxyNamespace()`
24-
- Passing anything but `null` as $proxyDir argument to `Doctrine\ORM\ORMSetup` methods
25-
- Passing more than one Argument to `Doctrine\ORM\Proxy\ProxyFactory::__construct()`
24+
- Passing more than one argument to `Doctrine\ORM\Proxy\ProxyFactory::__construct()`
25+
26+
Additionally, some methods of ORMSetup have been deprecated in favor of a new
27+
counterpart.
28+
29+
- `Doctrine\ORM\ORMSetup::createAttributeMetadataConfiguration()` is deprecated in favor of
30+
`Doctrine\ORM\ORMSetup::createAttributeMetadataConfig()`
31+
- `Doctrine\ORM\ORMSetup::createXMLMetadataConfiguration()` is deprecated in favor of
32+
`Doctrine\ORM\ORMSetup::createXMLMetadataConfig()`
33+
- `Doctrine\ORM\ORMSetup::createConfiguration()` is deprecated in favor of
34+
`Doctrine\ORM\ORMSetup::createConfig()`
2635

2736
## Deprecate methods for configuring no longer configurable features
2837

docs/en/reference/configuration.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ access point to ORM functionality provided by Doctrine.
5656
'dbname' => 'foo',
5757
];
5858
59-
$config = ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
59+
$config = ORMSetup::createAttributeMetadataConfig($paths, $isDevMode);
60+
// on PHP < 8.4, use ORMSetup::createAttributeMetadataConfiguration() instead
6061
$connection = DriverManager::getConnection($dbParams, $config);
6162
$entityManager = new EntityManager($connection, $config);
6263
@@ -66,7 +67,8 @@ Or if you prefer XML:
6667
6768
<?php
6869
$paths = ['/path/to/xml-mappings'];
69-
$config = ORMSetup::createXMLMetadataConfiguration($paths, $isDevMode);
70+
$config = ORMSetup::createXMLMetadataConfig($paths, $isDevMode);
71+
// on PHP < 8.4, use ORMSetup::createXMLMetadataConfiguration() instead
7072
$connection = DriverManager::getConnection($dbParams, $config);
7173
$entityManager = new EntityManager($connection, $config);
7274

docs/en/tutorials/getting-started.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ step:
138138
require_once "vendor/autoload.php";
139139
140140
// Create a simple "default" Doctrine ORM configuration for Attributes
141-
$config = ORMSetup::createAttributeMetadataConfiguration(
141+
$config = ORMSetup::createAttributeMetadataConfig( // on PHP < 8.4, use ORMSetup::createAttributeMetadataConfiguration()
142142
paths: [__DIR__ . '/src'],
143143
isDevMode: true,
144144
);
145145
// or if you prefer XML
146-
// $config = ORMSetup::createXMLMetadataConfiguration(
146+
// $config = ORMSetup::createXMLMetadataConfig( // on PHP < 8.4, use ORMSetup::createXMLMetadataConfiguration()
147147
// paths: [__DIR__ . '/config/xml'],
148148
// isDevMode: true,
149149
//);

src/EntityManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
*
4444
* $paths = ['/path/to/entity/mapping/files'];
4545
*
46-
* $config = ORMSetup::createAttributeMetadataConfiguration($paths);
46+
* $config = ORMSetup::createAttributeMetadataConfig($paths);
4747
* $connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true], $config);
4848
* $entityManager = new EntityManager($connection, $config);
4949
*

src/ORMSetup.php

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ public static function createAttributeMetadataConfiguration(
3636
string|null $proxyDir = null,
3737
CacheItemPoolInterface|null $cache = null,
3838
): Configuration {
39-
if (PHP_VERSION_ID >= 80400 && $proxyDir !== null) {
39+
if (PHP_VERSION_ID >= 80400) {
4040
Deprecation::trigger(
4141
'doctrine/orm',
4242
'https://github.com/doctrine/orm/pull/12005',
43-
'Passing anything but null as $proxyDir to %s is deprecated and will not be possible in Doctrine ORM 3.0.',
43+
'%s is deprecated in favor of %s, and will be removed in 4.0.',
4444
__METHOD__,
45+
self::class . '::createAttributeMetadataConfig()',
4546
);
4647
}
4748

@@ -51,6 +52,23 @@ public static function createAttributeMetadataConfiguration(
5152
return $config;
5253
}
5354

55+
/**
56+
* Creates a configuration with an attribute metadata driver.
57+
*
58+
* @param string[] $paths
59+
*/
60+
public static function createAttributeMetadataConfig(
61+
array $paths,
62+
bool $isDevMode = false,
63+
string|null $cacheNamespaceSeed = null,
64+
CacheItemPoolInterface|null $cache = null,
65+
): Configuration {
66+
$config = self::createConfig($isDevMode, $cacheNamespaceSeed, $cache);
67+
$config->setMetadataDriverImpl(new AttributeDriver($paths));
68+
69+
return $config;
70+
}
71+
5472
/**
5573
* Creates a configuration with an XML metadata driver.
5674
*
@@ -63,12 +81,13 @@ public static function createXMLMetadataConfiguration(
6381
CacheItemPoolInterface|null $cache = null,
6482
bool $isXsdValidationEnabled = true,
6583
): Configuration {
66-
if (PHP_VERSION_ID >= 80400 && $proxyDir !== null) {
84+
if (PHP_VERSION_ID >= 80400) {
6785
Deprecation::trigger(
6886
'doctrine/orm',
6987
'https://github.com/doctrine/orm/pull/12005',
70-
'Passing anything but null as $proxyDir to %s is deprecated and will not be possible in Doctrine ORM 3.0.',
88+
'%s is deprecated in favor of %s, and will be removed in 4.0.',
7189
__METHOD__,
90+
self::class . '::createXMLMetadataConfig()',
7291
);
7392
}
7493

@@ -78,6 +97,28 @@ public static function createXMLMetadataConfiguration(
7897
return $config;
7998
}
8099

100+
/**
101+
* Creates a configuration with an XML metadata driver.
102+
*
103+
* @param string[] $paths
104+
*/
105+
public static function createXMLMetadataConfig(
106+
array $paths,
107+
bool $isDevMode = false,
108+
string|null $cacheNamespaceSeed = null,
109+
CacheItemPoolInterface|null $cache = null,
110+
bool $isXsdValidationEnabled = true,
111+
): Configuration {
112+
$config = self::createConfig($isDevMode, $cacheNamespaceSeed, $cache);
113+
$config->setMetadataDriverImpl(new XmlDriver(
114+
$paths,
115+
XmlDriver::DEFAULT_FILE_EXTENSION,
116+
$isXsdValidationEnabled,
117+
));
118+
119+
return $config;
120+
}
121+
81122
/**
82123
* Creates a configuration without a metadata driver.
83124
*/
@@ -90,8 +131,9 @@ public static function createConfiguration(
90131
Deprecation::trigger(
91132
'doctrine/orm',
92133
'https://github.com/doctrine/orm/pull/12005',
93-
'Passing anything but null as $proxyDir to %s is deprecated and will not be possible in Doctrine ORM 3.0.',
134+
'%s is deprecated in favor of %s, and will be removed in 4.0.',
94135
__METHOD__,
136+
self::class . '::createConfig()',
95137
);
96138
}
97139

@@ -111,9 +153,23 @@ public static function createConfiguration(
111153
return $config;
112154
}
113155

156+
public static function createConfig(
157+
bool $isDevMode = false,
158+
string|null $cacheNamespaceSeed = null,
159+
CacheItemPoolInterface|null $cache = null,
160+
): Configuration {
161+
$cache = self::createCacheInstance($isDevMode, $cacheNamespaceSeed, $cache);
162+
$config = new Configuration();
163+
$config->setMetadataCache($cache);
164+
$config->setQueryCache($cache);
165+
$config->setResultCache($cache);
166+
167+
return $config;
168+
}
169+
114170
private static function createCacheInstance(
115171
bool $isDevMode,
116-
string $proxyDir,
172+
string|null $cacheNamespaceSeed,
117173
CacheItemPoolInterface|null $cache,
118174
): CacheItemPoolInterface {
119175
if ($cache !== null) {
@@ -131,7 +187,7 @@ private static function createCacheInstance(
131187
return new ArrayAdapter();
132188
}
133189

134-
$namespace = 'dc2_' . md5($proxyDir);
190+
$namespace = 'dc2_' . md5($cacheNamespaceSeed ?? 'default');
135191

136192
if (extension_loaded('apcu') && apcu_enabled()) {
137193
return new ApcuAdapter($namespace);

tests/Tests/ORM/ORMSetupTest.php

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Doctrine\Tests\ORM;
66

7+
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
78
use Doctrine\ORM\Configuration;
89
use Doctrine\ORM\Mapping as MappingNamespace;
910
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
@@ -12,6 +13,7 @@
1213
use PHPUnit\Framework\Attributes\Group;
1314
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
1415
use PHPUnit\Framework\Attributes\RequiresSetting;
16+
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
1517
use PHPUnit\Framework\TestCase;
1618
use ReflectionProperty;
1719
use Symfony\Component\Cache\Adapter\AbstractAdapter;
@@ -20,10 +22,19 @@
2022

2123
use function sys_get_temp_dir;
2224

25+
use const PHP_VERSION_ID;
26+
2327
class ORMSetupTest extends TestCase
2428
{
29+
use VerifyDeprecations;
30+
31+
#[WithoutErrorHandler]
2532
public function testAttributeConfiguration(): void
2633
{
34+
if (PHP_VERSION_ID >= 80400) {
35+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/12005');
36+
}
37+
2738
$config = ORMSetup::createAttributeMetadataConfiguration([], true);
2839

2940
self::assertInstanceOf(Configuration::class, $config);
@@ -32,8 +43,21 @@ public function testAttributeConfiguration(): void
3243
self::assertInstanceOf(AttributeDriver::class, $config->getMetadataDriverImpl());
3344
}
3445

46+
public function testAttributeConfig(): void
47+
{
48+
$config = ORMSetup::createAttributeMetadataConfig([], true);
49+
50+
self::assertInstanceOf(Configuration::class, $config);
51+
self::assertInstanceOf(AttributeDriver::class, $config->getMetadataDriverImpl());
52+
}
53+
54+
#[WithoutErrorHandler]
3555
public function testXMLConfiguration(): void
3656
{
57+
if (PHP_VERSION_ID >= 80400) {
58+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/12005');
59+
}
60+
3761
$config = ORMSetup::createXMLMetadataConfiguration([], true);
3862

3963
self::assertInstanceOf(Configuration::class, $config);
@@ -44,14 +68,18 @@ public function testDisablingXmlValidationIsPossible(): void
4468
{
4569
$this->expectNotToPerformAssertions();
4670

47-
ORMSetup::createXMLMetadataConfiguration(paths: [], isXsdValidationEnabled: false);
71+
ORMSetup::createXMLMetadataConfig(paths: [], isXsdValidationEnabled: false);
4872
}
4973

5074
#[RequiresPhpExtension('apcu')]
5175
#[RequiresSetting('apc.enable_cli', '1')]
5276
#[RequiresSetting('apc.enabled', '1')]
53-
public function testCacheNamespaceShouldBeGeneratedForApcu(): void
77+
public function testCacheNamespaceShouldBeGeneratedForApcuWhenUsingLegacyConstructor(): void
5478
{
79+
if (PHP_VERSION_ID >= 80400) {
80+
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/pull/12005');
81+
}
82+
5583
$config = ORMSetup::createConfiguration(false, '/foo');
5684
$cache = $config->getMetadataCache();
5785

@@ -61,7 +89,22 @@ public function testCacheNamespaceShouldBeGeneratedForApcu(): void
6189
self::assertSame('dc2_1effb2475fcfba4f9e8b8a1dbc8f3caf:', $namespaceProperty->getValue($cache));
6290
}
6391

92+
#[RequiresPhpExtension('apcu')]
93+
#[RequiresSetting('apc.enable_cli', '1')]
94+
#[RequiresSetting('apc.enabled', '1')]
95+
public function testCacheNamespaceShouldBeGeneratedForApcu(): void
96+
{
97+
$config = ORMSetup::createConfig(false, '/foo');
98+
$cache = $config->getMetadataCache();
99+
100+
$namespaceProperty = new ReflectionProperty(AbstractAdapter::class, 'namespace');
101+
102+
self::assertInstanceOf(ApcuAdapter::class, $cache);
103+
self::assertSame('dc2_1effb2475fcfba4f9e8b8a1dbc8f3caf:', $namespaceProperty->getValue($cache));
104+
}
105+
64106
#[Group('DDC-1350')]
107+
#[WithoutErrorHandler]
65108
public function testConfigureProxyDir(): void
66109
{
67110
$config = ORMSetup::createAttributeMetadataConfiguration([], true, '/foo');
@@ -72,7 +115,7 @@ public function testConfigureProxyDir(): void
72115
public function testConfigureCache(): void
73116
{
74117
$cache = new ArrayAdapter();
75-
$config = ORMSetup::createAttributeMetadataConfiguration([], true, null, $cache);
118+
$config = ORMSetup::createAttributeMetadataConfig([], true, null, $cache);
76119

77120
self::assertSame($cache, $config->getResultCache());
78121
self::assertSame($cache, $config->getQueryCache());
@@ -83,7 +126,7 @@ public function testConfigureCache(): void
83126
public function testConfigureCacheCustomInstance(): void
84127
{
85128
$cache = new ArrayAdapter();
86-
$config = ORMSetup::createConfiguration(true, null, $cache);
129+
$config = ORMSetup::createConfig(true, null, $cache);
87130

88131
self::assertSame($cache, $config->getResultCache());
89132
self::assertSame($cache, $config->getQueryCache());

0 commit comments

Comments
 (0)