Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate or throw on namespace alias usage #9592

Merged
merged 1 commit into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 19 additions & 0 deletions lib/Doctrine/ORM/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -196,6 +198,8 @@ public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationRead
}

/**
* @deprecated No replacement planned.
*
* Adds a namespace under a certain alias.
*
* @param string $alias
Expand All @@ -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;
}

Expand Down
21 changes: 21 additions & 0 deletions lib/Doctrine/ORM/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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');
Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2256Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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__);

Expand Down
11 changes: 11 additions & 0 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand Down