diff --git a/composer.json b/composer.json index 5e5e849fd..2a55ca33f 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "symfony/console": "^5.4 || ^6.4 || ^7.0 || ^8.0", "symfony/deprecation-contracts": "^2.2 || ^3.0", "symfony/var-dumper": "^5.4 || ^6.4 || ^7.0 || ^8.0", - "symfony/var-exporter": "^6.4 || ^7.0" + "symfony/var-exporter": "^6.4 || ^7.0 || ^8.0" }, "require-dev": { "ext-bcmath": "*", diff --git a/src/Configuration.php b/src/Configuration.php index 15b3703b6..fa1e7de5a 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -36,6 +36,7 @@ use Psr\Cache\CacheItemPoolInterface; use ReflectionClass; use stdClass; +use Symfony\Component\VarExporter\LazyGhostTrait; use Throwable; use function array_diff_key; @@ -44,6 +45,7 @@ use function class_exists; use function interface_exists; use function is_string; +use function trait_exists; use function trigger_deprecation; use function trim; @@ -695,12 +697,18 @@ public function setUseLazyGhostObject(bool $flag): void throw new LogicException('Cannot enable or disable LazyGhostObject when native lazy objects are enabled.'); } - if ($flag === false) { + if ($flag && ! trait_exists(LazyGhostTrait::class)) { + throw new LogicException('Package "symfony/var-exporter" >= 8.0 does not provide lazy ghost objects, use native lazy objects instead.'); + } + + if (! $flag) { if (! class_exists(ProxyManagerConfiguration::class)) { throw new LogicException('Package "friendsofphp/proxy-manager-lts" is required to disable LazyGhostObject.'); } - trigger_deprecation('doctrine/mongodb-odm', '2.10', 'Using "friendsofphp/proxy-manager-lts" is deprecated. Use "symfony/var-exporter" LazyGhostObjects instead.'); + if (PHP_VERSION_ID < 80400) { + trigger_deprecation('doctrine/mongodb-odm', '2.10', 'Using "friendsofphp/proxy-manager-lts" is deprecated. Use "symfony/var-exporter" LazyGhostObjects instead.'); + } } $this->lazyGhostObject = $flag; diff --git a/tests/Tests/ConfigurationTest.php b/tests/Tests/ConfigurationTest.php index c07d62c8f..e9b0bc8a8 100644 --- a/tests/Tests/ConfigurationTest.php +++ b/tests/Tests/ConfigurationTest.php @@ -4,6 +4,7 @@ namespace Doctrine\ODM\MongoDB\Tests; +use Composer\InstalledVersions; use Doctrine\ODM\MongoDB\Configuration; use Doctrine\ODM\MongoDB\ConfigurationException; use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionFactory; @@ -17,6 +18,7 @@ use function base64_encode; use function str_repeat; +use function version_compare; class ConfigurationTest extends TestCase { @@ -33,6 +35,10 @@ public function testUseNativeLazyObjectBeforePHP84(): void public function testUseLazyGhostObject(): void { + if (! version_compare(InstalledVersions::getVersion('symfony/var-exporter'), '8', '<')) { + $this->markTestSkipped('Symfony VarExporter 8 or higher is not installed.'); + } + $c = new Configuration(); self::assertFalse($c->isLazyGhostObjectEnabled()); @@ -42,6 +48,21 @@ public function testUseLazyGhostObject(): void self::assertFalse($c->isLazyGhostObjectEnabled()); } + #[RequiresPhp('>= 8.4')] + public function testUseLazyGhostObjectWithSymfony8(): void + { + if (version_compare(InstalledVersions::getVersion('symfony/var-exporter'), '8', '<')) { + $this->markTestSkipped('Symfony VarExporter 8 or higher is not installed.'); + } + + $c = new Configuration(); + + self::expectException(LogicException::class); + self::expectExceptionMessage('Package "symfony/var-exporter" >= 8.0 does not provide lazy ghost objects, use native lazy objects instead.'); + + $c->setUseLazyGhostObject(true); + } + public function testNativeLazyObjectDeprecatedByDefault(): void { $c = new Configuration();