|
4 | 4 |
|
5 | 5 | namespace Doctrine\ORM\Id; |
6 | 6 |
|
| 7 | +use Doctrine\Deprecations\Deprecation; |
7 | 8 | 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; |
8 | 15 |
|
9 | 16 | abstract class AbstractIdGenerator |
10 | 17 | { |
| 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 | + |
11 | 55 | /** |
12 | 56 | * Generates an identifier for an entity. |
13 | 57 | * |
14 | 58 | * @param object|null $entity |
15 | 59 | * |
16 | 60 | * @return mixed |
17 | 61 | */ |
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 | + } |
19 | 78 |
|
20 | 79 | /** |
21 | 80 | * Gets whether this generator is a post-insert generator which means that |
|
0 commit comments