diff --git a/UPGRADE.md b/UPGRADE.md index 61b6280261..8db5260c41 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,17 @@ # Upgrade to 2.12 +## Deprecated more APIs related to entity namespace aliases + +```diff +-$config = $entityManager->getConfiguration(); +-$config->addEntityNamespace('CMS', 'My\App\Cms'); ++use My\App\Cms\CmsUser; + +-$entityManager->getRepository('CMS:CmsUser'); ++$entityManager->getRepository(CmsUser::class); +``` + + ## BC Break: `AttributeDriver` and `AnnotationDriver` no longer extends parent class from `doctrine/persistence` Both these classes used to extend an abstract `AnnotationDriver` class defined diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index c4a05efcd4..40ff3cb271 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -12,6 +12,7 @@ use Doctrine\Common\Cache\Cache as CacheDriver; use Doctrine\Common\Cache\Psr6\CacheAdapter; use Doctrine\Common\Cache\Psr6\DoctrineProvider; +use Doctrine\Common\Persistence\PersistentObject; use Doctrine\Common\Proxy\AbstractProxyFactory; use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\Cache\CacheConfiguration; @@ -23,6 +24,7 @@ use Doctrine\ORM\Exception\InvalidEntityRepository; use Doctrine\ORM\Exception\NamedNativeQueryNotFound; use Doctrine\ORM\Exception\NamedQueryNotFound; +use Doctrine\ORM\Exception\NotSupported; use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating; use Doctrine\ORM\Exception\UnknownEntityNamespace; use Doctrine\ORM\Internal\Hydration\AbstractHydrator; @@ -196,6 +198,8 @@ public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationRead } /** + * @deprecated No replacement planned. + * * Adds a namespace under a certain alias. * * @param string $alias @@ -205,6 +209,21 @@ public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationRead */ public function addEntityNamespace($alias, $namespace) { + if (class_exists(PersistentObject::class)) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/issues/8818', + 'Short namespace aliases such as "%s" are deprecated and will be removed in Doctrine ORM 3.0.', + $alias + ); + } else { + NotSupported::createForPersistence3(sprintf( + 'Using short namespace alias "%s" by calling %s', + $alias, + __METHOD__ + )); + } + $this->_attributes['entityNamespaces'][$alias] = $namespace; } diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 2697a8e88c..9c28ff0d0f 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -7,6 +7,7 @@ use BadMethodCallException; use Doctrine\Common\Cache\Psr6\CacheAdapter; use Doctrine\Common\EventManager; +use Doctrine\Common\Persistence\PersistentObject; use Doctrine\Common\Util\ClassUtils; use Doctrine\DBAL\Connection; use Doctrine\DBAL\DriverManager; @@ -17,6 +18,7 @@ use Doctrine\ORM\Exception\MismatchedEventManager; use Doctrine\ORM\Exception\MissingIdentifierField; use Doctrine\ORM\Exception\MissingMappingDriverImplementation; +use Doctrine\ORM\Exception\NotSupported; use Doctrine\ORM\Exception\UnrecognizedIdentifierFields; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadataFactory; @@ -32,6 +34,7 @@ use function array_keys; use function call_user_func; +use function class_exists; use function get_debug_type; use function gettype; use function is_array; @@ -40,6 +43,7 @@ use function is_string; use function ltrim; use function sprintf; +use function strpos; /** * The EntityManager is the central access point to ORM functionality. @@ -782,6 +786,23 @@ public function lock($entity, $lockMode, $lockVersion = null) */ public function getRepository($entityName) { + if (strpos($entityName, ':') !== false) { + if (class_exists(PersistentObject::class)) { + Deprecation::trigger( + 'doctrine/orm', + 'https://github.com/doctrine/orm/issues/8818', + 'Short namespace aliases such as "%s" are deprecated and will be removed in Doctrine ORM 3.0.', + $entityName + ); + } else { + NotSupported::createForPersistence3(sprintf( + 'Using short namespace alias "%s" when calling %s', + $entityName, + __METHOD__ + )); + } + } + return $this->repositoryFactory->getRepository($this, $entityName); } diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index b2a7313579..0506458b62 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -287,6 +287,12 @@ public function testFindAll(): void public function testFindByAlias(): void { + if (! class_exists(PersistentObject::class)) { + $this->markTestSkipped('This test requires doctrine/persistence 2'); + } + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8818'); + $user1Id = $this->loadFixture(); $repos = $this->_em->getRepository(CmsUser::class); @@ -673,6 +679,11 @@ public function testSetDefaultRepositoryInvalidClassError(): void */ public function testSingleRepositoryInstanceForDifferentEntityAliases(): void { + if (! class_exists(PersistentObject::class)) { + $this->markTestSkipped('This test requires doctrine/persistence 2'); + } + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8818'); $config = $this->_em->getConfiguration(); $config->addEntityNamespace('Aliased', 'Doctrine\Tests\Models\CMS'); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2256Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2256Test.php index 0caee15af7..57a5712a2f 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2256Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2256Test.php @@ -6,6 +6,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Persistence\PersistentObject; +use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; use Doctrine\ORM\Mapping\Column; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\GeneratedValue; @@ -18,11 +20,15 @@ use Doctrine\ORM\Query\ResultSetMappingBuilder; use Doctrine\Tests\OrmFunctionalTestCase; +use function class_exists; + /** * @group DDC-2256 */ class DDC2256Test extends OrmFunctionalTestCase { + use VerifyDeprecations; + protected function setUp(): void { parent::setUp(); @@ -37,6 +43,11 @@ protected function setUp(): void public function testIssue(): void { + if (! class_exists(PersistentObject::class)) { + $this->markTestSkipped('This test requires doctrine/persistence 2'); + } + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8818'); $config = $this->_em->getConfiguration(); $config->addEntityNamespace('MyNamespace', __NAMESPACE__); diff --git a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php index 65eb46278a..32856d30c2 100644 --- a/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php @@ -5,9 +5,11 @@ namespace Doctrine\Tests\ORM\Mapping; use Doctrine\Common\EventManager; +use Doctrine\Common\Persistence\PersistentObject; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\Deprecations\PHPUnit\VerifyDeprecations; use Doctrine\ORM\Configuration; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs; @@ -45,11 +47,14 @@ use function array_search; use function assert; +use function class_exists; use function count; use function sprintf; class ClassMetadataFactoryTest extends OrmTestCase { + use VerifyDeprecations; + public function testGetMetadataForSingleClass(): void { $platform = $this->createMock(AbstractPlatform::class); @@ -196,6 +201,12 @@ public function testIsTransient(): void */ public function testIsTransientEntityNamespace(): void { + if (! class_exists(PersistentObject::class)) { + $this->markTestSkipped('This test requires doctrine/persistence 2'); + } + + $this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/8818'); + $cmf = new ClassMetadataFactory(); $driver = $this->createMock(MappingDriver::class); $driver->expects(self::exactly(2))