Skip to content

Commit

Permalink
Use EntityManagerInterface in type declarations (#9325)
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabus authored Jan 12, 2022
1 parent e974313 commit ee59119
Show file tree
Hide file tree
Showing 52 changed files with 255 additions and 158 deletions.
7 changes: 7 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Upgrade to 2.11

## Rename `AbstractIdGenerator::generate()` to `generateId()`

Implementations of `AbstractIdGenerator` have to override the method
`generateId()` without calling the parent implementation. Not doing so is
deprecated. Calling `generate()` on any `AbstractIdGenerator` implementation
is deprecated.

## PSR-6-based second level cache

The second level cache has been reworked to consume a PSR-6 cache. Using a
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class EntityRepository implements ObjectRepository, Selectable
/** @var string */
protected $_entityName;

/** @var EntityManager */
/** @var EntityManagerInterface */
protected $_em;

/** @var ClassMetadata */
Expand Down Expand Up @@ -282,7 +282,7 @@ public function getClassName()
}

/**
* @return EntityManager
* @return EntityManagerInterface
*/
protected function getEntityManager()
{
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Event/LifecycleEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ORM\Event;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Event\LifecycleEventArgs as BaseLifecycleEventArgs;

/**
Expand All @@ -28,7 +28,7 @@ public function getEntity()
/**
* Retrieves associated EntityManager.
*
* @return EntityManager
* @return EntityManagerInterface
*/
public function getEntityManager()
{
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Event/LoadClassMetadataEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

namespace Doctrine\ORM\Event;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs as BaseLoadClassMetadataEventArgs;

/**
* Class that holds event arguments for a loadMetadata event.
*
* @method __construct(ClassMetadata $classMetadata, EntityManager $objectManager)
* @method __construct(ClassMetadata $classMetadata, EntityManagerInterface $objectManager)
* @method ClassMetadata getClassMetadata()
*/
class LoadClassMetadataEventArgs extends BaseLoadClassMetadataEventArgs
{
/**
* Retrieve associated EntityManager.
*
* @return EntityManager
* @return EntityManagerInterface
*/
public function getEntityManager()
{
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/ORM/Event/PostFlushEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\ORM\Event;

use Doctrine\Common\EventArgs;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
Expand All @@ -15,7 +14,7 @@
*/
class PostFlushEventArgs extends EventArgs
{
/** @var EntityManager */
/** @var EntityManagerInterface */
private $em;

public function __construct(EntityManagerInterface $em)
Expand All @@ -26,7 +25,7 @@ public function __construct(EntityManagerInterface $em)
/**
* Retrieves associated EntityManager.
*
* @return EntityManager
* @return EntityManagerInterface
*/
public function getEntityManager()
{
Expand Down
5 changes: 2 additions & 3 deletions lib/Doctrine/ORM/Event/PreFlushEventArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\ORM\Event;

use Doctrine\Common\EventArgs;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
Expand All @@ -15,7 +14,7 @@
*/
class PreFlushEventArgs extends EventArgs
{
/** @var EntityManager */
/** @var EntityManagerInterface */
private $em;

public function __construct(EntityManagerInterface $em)
Expand All @@ -24,7 +23,7 @@ public function __construct(EntityManagerInterface $em)
}

/**
* @return EntityManager
* @return EntityManagerInterface
*/
public function getEntityManager()
{
Expand Down
61 changes: 60 additions & 1 deletion lib/Doctrine/ORM/Id/AbstractIdGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,77 @@

namespace Doctrine\ORM\Id;

use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use LogicException;

use function get_debug_type;
use function sprintf;

abstract class AbstractIdGenerator
{
/** @var bool */
private $alreadyDelegatedToGenerateId = false;

/**
* Generates an identifier for an entity.
*
* @deprecated Call {@see generateId()} instead.
*
* @param object|null $entity
*
* @return mixed
*/
public function generate(EntityManager $em, $entity)
{
if ($this->alreadyDelegatedToGenerateId) {
throw new LogicException(sprintf(
'Endless recursion detected in %s. Please implement generateId() without calling the parent implementation.',
get_debug_type($this)
));
}

Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9325',
'%s::generate() is deprecated, call generateId() instead.',
get_debug_type($this)
);

$this->alreadyDelegatedToGenerateId = true;

try {
return $this->generateId($em, $entity);
} finally {
$this->alreadyDelegatedToGenerateId = false;
}
}

/**
* Generates an identifier for an entity.
*
* @param object|null $entity
*
* @return mixed
*/
abstract public function generate(EntityManager $em, $entity);
public function generateId(EntityManagerInterface $em, $entity)
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9325',
'Not implementing %s in %s is deprecated.',
__FUNCTION__,
get_debug_type($this)
);

if (! $em instanceof EntityManager) {
throw new InvalidArgumentException('Unsupported entity manager implementation.');
}

return $this->generate($em, $entity);
}

/**
* Gets whether this generator is a post-insert generator which means that
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Id/AssignedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ORM\Id;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\EntityMissingAssignedId;

use function get_class;
Expand All @@ -17,11 +17,11 @@ class AssignedGenerator extends AbstractIdGenerator
/**
* Returns the identifier assigned to the given entity.
*
* {@inheritDoc}
* {@inheritdoc}
*
* @throws EntityMissingAssignedId
*/
public function generate(EntityManager $em, $entity)
public function generateId(EntityManagerInterface $em, $entity)
{
$class = $em->getClassMetadata(get_class($entity));
$idFields = $class->getIdentifierFieldNames();
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ORM\Id;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
* Id generator that obtains IDs from special "identity" columns. These are columns
Expand Down Expand Up @@ -33,7 +33,7 @@ public function __construct($sequenceName = null)
/**
* {@inheritDoc}
*/
public function generate(EntityManager $em, $entity)
public function generateId(EntityManagerInterface $em, $entity)
{
return (string) $em->getConnection()->lastInsertId($this->sequenceName);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Id/IdentityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ORM\Id;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
* Id generator that obtains IDs from special "identity" columns. These are columns
Expand Down Expand Up @@ -33,7 +33,7 @@ public function __construct($sequenceName = null)
/**
* {@inheritDoc}
*/
public function generate(EntityManager $em, $entity)
public function generateId(EntityManagerInterface $em, $entity)
{
return (int) $em->getConnection()->lastInsertId($this->sequenceName);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Id/SequenceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Doctrine\ORM\Id;

use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Serializable;

use function serialize;
Expand Down Expand Up @@ -51,7 +51,7 @@ public function __construct($sequenceName, $allocationSize)
/**
* {@inheritDoc}
*/
public function generate(EntityManager $em, $entity)
public function generateId(EntityManagerInterface $em, $entity)
{
if ($this->_maxValue === null || $this->_nextValue === $this->_maxValue) {
// Allocate new values
Expand Down
6 changes: 3 additions & 3 deletions lib/Doctrine/ORM/Id/TableGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\ORM\Id;

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;

/**
* Id generator that uses a single-row database table and a hi/lo algorithm.
Expand Down Expand Up @@ -43,8 +43,8 @@ public function __construct($tableName, $sequenceName = 'default', $allocationSi
/**
* {@inheritDoc}
*/
public function generate(
EntityManager $em,
public function generateId(
EntityManagerInterface $em,
$entity
) {
if ($this->_maxValue === null || $this->_nextValue === $this->_maxValue) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ORM/Id/UuidGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\NotSupported;

use function method_exists;
Expand Down Expand Up @@ -38,7 +38,7 @@ public function __construct()
*
* @throws NotSupported
*/
public function generate(EntityManager $em, $entity)
public function generateId(EntityManagerInterface $em, $entity)
{
$connection = $em->getConnection();
$sql = 'SELECT ' . $connection->getDatabasePlatform()->getGuidExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function executeInserts()
$stmt->executeStatement();

if ($isPostInsertId) {
$generatedId = $idGenerator->generate($this->em, $entity);
$generatedId = $idGenerator->generateId($this->em, $entity);
$id = [$this->class->identifier[0] => $generatedId];
$postInsertIds[] = [
'generatedId' => $generatedId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public function executeInserts()
$rootTableStmt->executeStatement();

if ($isPostInsertId) {
$generatedId = $idGenerator->generate($this->em, $entity);
$generatedId = $idGenerator->generateId($this->em, $entity);
$id = [$this->class->identifier[0] => $generatedId];
$postInsertIds[] = [
'generatedId' => $generatedId,
Expand Down
3 changes: 1 addition & 2 deletions lib/Doctrine/ORM/Query/FilterCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Doctrine\ORM\Query;

use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Filter\SQLFilter;
use InvalidArgumentException;
Expand Down Expand Up @@ -40,7 +39,7 @@ class FilterCollection
/**
* The EntityManager that "owns" this FilterCollection instance.
*
* @var EntityManager
* @var EntityManagerInterface
*/
private $em;

Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ private function persistNew(ClassMetadata $class, $entity): void
$idGen = $class->idGenerator;

if (! $idGen->isPostInsertGenerator()) {
$idValue = $idGen->generate($this->em, $entity);
$idValue = $idGen->generateId($this->em, $entity);

if (! $idGen instanceof AssignedGenerator) {
$idValue = [$class->getSingleIdentifierFieldName() => $this->convertSingleFieldIdentifierToPHPValue($class, $idValue)];
Expand Down
Loading

0 comments on commit ee59119

Please sign in to comment.