Skip to content

Commit ee59119

Browse files
authored
Use EntityManagerInterface in type declarations (#9325)
1 parent e974313 commit ee59119

File tree

52 files changed

+255
-158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+255
-158
lines changed

UPGRADE.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Upgrade to 2.11
22

3+
## Rename `AbstractIdGenerator::generate()` to `generateId()`
4+
5+
Implementations of `AbstractIdGenerator` have to override the method
6+
`generateId()` without calling the parent implementation. Not doing so is
7+
deprecated. Calling `generate()` on any `AbstractIdGenerator` implementation
8+
is deprecated.
9+
310
## PSR-6-based second level cache
411

512
The second level cache has been reworked to consume a PSR-6 cache. Using a

lib/Doctrine/ORM/EntityRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class EntityRepository implements ObjectRepository, Selectable
3939
/** @var string */
4040
protected $_entityName;
4141

42-
/** @var EntityManager */
42+
/** @var EntityManagerInterface */
4343
protected $_em;
4444

4545
/** @var ClassMetadata */
@@ -282,7 +282,7 @@ public function getClassName()
282282
}
283283

284284
/**
285-
* @return EntityManager
285+
* @return EntityManagerInterface
286286
*/
287287
protected function getEntityManager()
288288
{

lib/Doctrine/ORM/Event/LifecycleEventArgs.php

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

55
namespace Doctrine\ORM\Event;
66

7-
use Doctrine\ORM\EntityManager;
7+
use Doctrine\ORM\EntityManagerInterface;
88
use Doctrine\Persistence\Event\LifecycleEventArgs as BaseLifecycleEventArgs;
99

1010
/**
@@ -28,7 +28,7 @@ public function getEntity()
2828
/**
2929
* Retrieves associated EntityManager.
3030
*
31-
* @return EntityManager
31+
* @return EntityManagerInterface
3232
*/
3333
public function getEntityManager()
3434
{

lib/Doctrine/ORM/Event/LoadClassMetadataEventArgs.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44

55
namespace Doctrine\ORM\Event;
66

7-
use Doctrine\ORM\EntityManager;
7+
use Doctrine\ORM\EntityManagerInterface;
88
use Doctrine\ORM\Mapping\ClassMetadata;
99
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs as BaseLoadClassMetadataEventArgs;
1010

1111
/**
1212
* Class that holds event arguments for a loadMetadata event.
1313
*
14-
* @method __construct(ClassMetadata $classMetadata, EntityManager $objectManager)
14+
* @method __construct(ClassMetadata $classMetadata, EntityManagerInterface $objectManager)
1515
* @method ClassMetadata getClassMetadata()
1616
*/
1717
class LoadClassMetadataEventArgs extends BaseLoadClassMetadataEventArgs
1818
{
1919
/**
2020
* Retrieve associated EntityManager.
2121
*
22-
* @return EntityManager
22+
* @return EntityManagerInterface
2323
*/
2424
public function getEntityManager()
2525
{

lib/Doctrine/ORM/Event/PostFlushEventArgs.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Doctrine\ORM\Event;
66

77
use Doctrine\Common\EventArgs;
8-
use Doctrine\ORM\EntityManager;
98
use Doctrine\ORM\EntityManagerInterface;
109

1110
/**
@@ -15,7 +14,7 @@
1514
*/
1615
class PostFlushEventArgs extends EventArgs
1716
{
18-
/** @var EntityManager */
17+
/** @var EntityManagerInterface */
1918
private $em;
2019

2120
public function __construct(EntityManagerInterface $em)
@@ -26,7 +25,7 @@ public function __construct(EntityManagerInterface $em)
2625
/**
2726
* Retrieves associated EntityManager.
2827
*
29-
* @return EntityManager
28+
* @return EntityManagerInterface
3029
*/
3130
public function getEntityManager()
3231
{

lib/Doctrine/ORM/Event/PreFlushEventArgs.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Doctrine\ORM\Event;
66

77
use Doctrine\Common\EventArgs;
8-
use Doctrine\ORM\EntityManager;
98
use Doctrine\ORM\EntityManagerInterface;
109

1110
/**
@@ -15,7 +14,7 @@
1514
*/
1615
class PreFlushEventArgs extends EventArgs
1716
{
18-
/** @var EntityManager */
17+
/** @var EntityManagerInterface */
1918
private $em;
2019

2120
public function __construct(EntityManagerInterface $em)
@@ -24,7 +23,7 @@ public function __construct(EntityManagerInterface $em)
2423
}
2524

2625
/**
27-
* @return EntityManager
26+
* @return EntityManagerInterface
2827
*/
2928
public function getEntityManager()
3029
{

lib/Doctrine/ORM/Id/AbstractIdGenerator.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,77 @@
44

55
namespace Doctrine\ORM\Id;
66

7+
use Doctrine\Deprecations\Deprecation;
78
use Doctrine\ORM\EntityManager;
9+
use Doctrine\ORM\EntityManagerInterface;
10+
use InvalidArgumentException;
11+
use LogicException;
12+
13+
use function get_debug_type;
14+
use function sprintf;
815

916
abstract class AbstractIdGenerator
1017
{
18+
/** @var bool */
19+
private $alreadyDelegatedToGenerateId = false;
20+
21+
/**
22+
* Generates an identifier for an entity.
23+
*
24+
* @deprecated Call {@see generateId()} instead.
25+
*
26+
* @param object|null $entity
27+
*
28+
* @return mixed
29+
*/
30+
public function generate(EntityManager $em, $entity)
31+
{
32+
if ($this->alreadyDelegatedToGenerateId) {
33+
throw new LogicException(sprintf(
34+
'Endless recursion detected in %s. Please implement generateId() without calling the parent implementation.',
35+
get_debug_type($this)
36+
));
37+
}
38+
39+
Deprecation::trigger(
40+
'doctrine/orm',
41+
'https://github.com/doctrine/orm/pull/9325',
42+
'%s::generate() is deprecated, call generateId() instead.',
43+
get_debug_type($this)
44+
);
45+
46+
$this->alreadyDelegatedToGenerateId = true;
47+
48+
try {
49+
return $this->generateId($em, $entity);
50+
} finally {
51+
$this->alreadyDelegatedToGenerateId = false;
52+
}
53+
}
54+
1155
/**
1256
* Generates an identifier for an entity.
1357
*
1458
* @param object|null $entity
1559
*
1660
* @return mixed
1761
*/
18-
abstract public function generate(EntityManager $em, $entity);
62+
public function generateId(EntityManagerInterface $em, $entity)
63+
{
64+
Deprecation::trigger(
65+
'doctrine/orm',
66+
'https://github.com/doctrine/orm/pull/9325',
67+
'Not implementing %s in %s is deprecated.',
68+
__FUNCTION__,
69+
get_debug_type($this)
70+
);
71+
72+
if (! $em instanceof EntityManager) {
73+
throw new InvalidArgumentException('Unsupported entity manager implementation.');
74+
}
75+
76+
return $this->generate($em, $entity);
77+
}
1978

2079
/**
2180
* Gets whether this generator is a post-insert generator which means that

lib/Doctrine/ORM/Id/AssignedGenerator.php

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

55
namespace Doctrine\ORM\Id;
66

7-
use Doctrine\ORM\EntityManager;
7+
use Doctrine\ORM\EntityManagerInterface;
88
use Doctrine\ORM\Exception\EntityMissingAssignedId;
99

1010
use function get_class;
@@ -17,11 +17,11 @@ class AssignedGenerator extends AbstractIdGenerator
1717
/**
1818
* Returns the identifier assigned to the given entity.
1919
*
20-
* {@inheritDoc}
20+
* {@inheritdoc}
2121
*
2222
* @throws EntityMissingAssignedId
2323
*/
24-
public function generate(EntityManager $em, $entity)
24+
public function generateId(EntityManagerInterface $em, $entity)
2525
{
2626
$class = $em->getClassMetadata(get_class($entity));
2727
$idFields = $class->getIdentifierFieldNames();

lib/Doctrine/ORM/Id/BigIntegerIdentityGenerator.php

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

55
namespace Doctrine\ORM\Id;
66

7-
use Doctrine\ORM\EntityManager;
7+
use Doctrine\ORM\EntityManagerInterface;
88

99
/**
1010
* Id generator that obtains IDs from special "identity" columns. These are columns
@@ -33,7 +33,7 @@ public function __construct($sequenceName = null)
3333
/**
3434
* {@inheritDoc}
3535
*/
36-
public function generate(EntityManager $em, $entity)
36+
public function generateId(EntityManagerInterface $em, $entity)
3737
{
3838
return (string) $em->getConnection()->lastInsertId($this->sequenceName);
3939
}

lib/Doctrine/ORM/Id/IdentityGenerator.php

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

55
namespace Doctrine\ORM\Id;
66

7-
use Doctrine\ORM\EntityManager;
7+
use Doctrine\ORM\EntityManagerInterface;
88

99
/**
1010
* Id generator that obtains IDs from special "identity" columns. These are columns
@@ -33,7 +33,7 @@ public function __construct($sequenceName = null)
3333
/**
3434
* {@inheritDoc}
3535
*/
36-
public function generate(EntityManager $em, $entity)
36+
public function generateId(EntityManagerInterface $em, $entity)
3737
{
3838
return (int) $em->getConnection()->lastInsertId($this->sequenceName);
3939
}

0 commit comments

Comments
 (0)