Skip to content

Commit

Permalink
Merge pull request #2011 from malarzm/id-generator-interface
Browse files Browse the repository at this point in the history
[2.1] Deprecate AbstractIdGenerator
  • Loading branch information
alcaeus authored May 4, 2020
2 parents b852d47 + 282b399 commit bc5e595
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
7 changes: 7 additions & 0 deletions UPGRADE-2.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# UPGRADE FROM 2.0 to 2.1

The `Doctrine\ODM\MongoDB\Id\AbstractIdGenerator` class has been deprecated. Custom ID generators must implement
the `Doctrine\ODM\MongoDB\Id\IdGenerator` interface.

The `Doctrine\ODM\MongoDB\Mapping\ClassMetadata` class has been marked final. The class will no longer be extendable
in ODM 3.0.
12 changes: 2 additions & 10 deletions lib/Doctrine/ODM/MongoDB/Id/AbstractIdGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@

namespace Doctrine\ODM\MongoDB\Id;

use Doctrine\ODM\MongoDB\DocumentManager;

/**
* AbstractIdGenerator
* @deprecated AbstractIdGenerator was deprecated in 2.1 and will be removed in 3.0. Implement IdGenerator interface instead.
*/
abstract class AbstractIdGenerator
abstract class AbstractIdGenerator implements IdGenerator
{
/**
* Generates an identifier for a document.
*
* @return mixed
*/
abstract public function generate(DocumentManager $dm, object $document);
}
17 changes: 17 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Id/IdGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Doctrine\ODM\MongoDB\Id;

use Doctrine\ODM\MongoDB\DocumentManager;

interface IdGenerator
{
/**
* Generates an identifier for a document.
*
* @return mixed
*/
public function generate(DocumentManager $dm, object $document);
}
12 changes: 7 additions & 5 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use BadMethodCallException;
use Doctrine\Instantiator\Instantiator;
use Doctrine\Instantiator\InstantiatorInterface;
use Doctrine\ODM\MongoDB\Id\AbstractIdGenerator;
use Doctrine\ODM\MongoDB\Id\IdGenerator;
use Doctrine\ODM\MongoDB\LockException;
use Doctrine\ODM\MongoDB\Types\Type;
use Doctrine\ODM\MongoDB\Utility\CollectionHelper;
Expand Down Expand Up @@ -48,8 +48,10 @@
* 2) To drastically reduce the size of a serialized instance (private/protected members
* get the whole class name, namespace inclusive, prepended to every property in
* the serialized representation).
*
* @final
*/
class ClassMetadata implements BaseClassMetadata
/* final */ class ClassMetadata implements BaseClassMetadata
{
/* The Id generator types. */
/**
Expand Down Expand Up @@ -80,7 +82,7 @@ class ClassMetadata implements BaseClassMetadata
* does not exist or if an option was passed for that there is not setter in the new
* generator class.
*
* The class will have to be a subtype of AbstractIdGenerator.
* The class will have to implement IdGenerator.
*/
public const GENERATOR_TYPE_CUSTOM = 5;

Expand Down Expand Up @@ -328,7 +330,7 @@ class ClassMetadata implements BaseClassMetadata
/**
* READ-ONLY: The ID generator used for generating IDs for this class.
*
* @var AbstractIdGenerator|null
* @var IdGenerator|null
*/
public $idGenerator;

Expand Down Expand Up @@ -1390,7 +1392,7 @@ public function isCollectionValuedEmbed(string $fieldName) : bool
/**
* Sets the ID generator used to generate IDs for instances of this class.
*/
public function setIdGenerator(AbstractIdGenerator $generator) : void
public function setIdGenerator(IdGenerator $generator) : void
{
$this->idGenerator = $generator;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
use Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs;
use Doctrine\ODM\MongoDB\Event\OnClassMetadataNotFoundEventArgs;
use Doctrine\ODM\MongoDB\Events;
use Doctrine\ODM\MongoDB\Id\AbstractIdGenerator;
use Doctrine\ODM\MongoDB\Id\AlnumGenerator;
use Doctrine\ODM\MongoDB\Id\AutoGenerator;
use Doctrine\ODM\MongoDB\Id\IdGenerator;
use Doctrine\ODM\MongoDB\Id\IncrementGenerator;
use Doctrine\ODM\MongoDB\Id\UuidGenerator;
use Doctrine\Persistence\Mapping\AbstractClassMetadataFactory;
Expand Down Expand Up @@ -283,7 +283,7 @@ private function completeIdGeneratorMapping(ClassMetadata $class) : void

$customGenerator = new $idGenOptions['class']();
unset($idGenOptions['class']);
if (! $customGenerator instanceof AbstractIdGenerator) {
if (! $customGenerator instanceof IdGenerator) {
throw MappingException::classIsNotAValidGenerator(get_class($customGenerator));
}

Expand Down

0 comments on commit bc5e595

Please sign in to comment.