Skip to content

Commit

Permalink
Merge pull request #409 from VincentLanglet/references
Browse files Browse the repository at this point in the history
Implement reference by class names
  • Loading branch information
greg0ire authored Nov 29, 2022
2 parents 0bae67f + 0a88983 commit fb3fa64
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 70 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ext-sqlite3": "*",
"doctrine/coding-standard": "^10.0",
"doctrine/dbal": "^2.13 || ^3.0",
"doctrine/deprecations": "^1.0",
"doctrine/mongodb-odm": "^1.3.0 || ^2.0.0",
"doctrine/orm": "^2.12",
"phpstan/phpstan": "^1.5",
Expand Down
2 changes: 1 addition & 1 deletion docs/en/how-to/sharing-objects-between-fixtures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ And the ``User`` data loading fixture:
$user->setUsername('jwage');
$user->setPassword('test');
$user->setRole(
$this->getReference('admin-role') // load the stored reference
$this->getReference('admin-role', Role::class) // load the stored reference
);
$manager->persist($user);
Expand Down
32 changes: 28 additions & 4 deletions lib/Doctrine/Common/DataFixtures/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Common\DataFixtures;

use BadMethodCallException;
use Doctrine\Deprecations\Deprecation;

/**
* Abstract Fixture class helps to manage references
Expand Down Expand Up @@ -72,12 +73,25 @@ public function addReference($name, $object)
* @see Doctrine\Common\DataFixtures\ReferenceRepository::getReference
*
* @param string $name
* @psalm-param class-string<T>|null $class
*
* @return object
* @psalm-return $class is null ? object : T
*
* @template T of object
*/
public function getReference($name)
public function getReference($name, ?string $class = null)
{
return $this->referenceRepository->getReference($name);
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__
);
}

return $this->referenceRepository->getReference($name, $class);
}

/**
Expand All @@ -87,11 +101,21 @@ public function getReference($name)
* @see Doctrine\Common\DataFixtures\ReferenceRepository::hasReference
*
* @param string $name
* @psalm-param class-string $class
*
* @return bool
*/
public function hasReference($name)
public function hasReference($name, ?string $class = null)
{
return $this->referenceRepository->hasReference($name);
if ($class === null) {
Deprecation::trigger(
'doctrine/data-fixtures',
'https://github.com/doctrine/data-fixtures/pull/409',
'Argument $class of %s() will be mandatory in 2.0.',
__METHOD__
);
}

return $this->referenceRepository->hasReference($name, $class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Doctrine\Common\EventSubscriber;
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;

use function get_class;

/**
* Reference Listener populates identities for
* stored references
Expand Down Expand Up @@ -49,7 +51,7 @@ public function postPersist(LifecycleEventArgs $args)
->getUnitOfWork()
->getDocumentIdentifier($object);

$this->referenceRepository->setReferenceIdentity($name, $identity);
$this->referenceRepository->setReferenceIdentity($name, $identity, get_class($object));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;

use function get_class;

/**
* Reference Listener populates identities for
* stored references
Expand Down Expand Up @@ -50,7 +52,7 @@ public function postPersist(LifecycleEventArgs $args)
->getUnitOfWork()
->getEntityIdentifier($object);

$this->referenceRepository->setReferenceIdentity($name, $identity);
$this->referenceRepository->setReferenceIdentity($name, $identity, get_class($object));
}
}
}
70 changes: 37 additions & 33 deletions lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use function file_put_contents;
use function get_class;
use function serialize;
use function substr;
use function unserialize;

/**
Expand All @@ -20,22 +19,6 @@
*/
class ProxyReferenceRepository extends ReferenceRepository
{
/**
* Get real class name of a reference that could be a proxy
*
* @param string $className Class name of reference object
*
* @return string
*/
protected function getRealClass($className)
{
if (substr($className, -5) === 'Proxy') {
return substr($className, 0, -5);
}

return $className;
}

/**
* Serialize reference repository
*
Expand All @@ -53,8 +36,9 @@ public function serialize()
}

return serialize([
'references' => $simpleReferences,
'identities' => $this->getIdentities(),
'references' => $simpleReferences, // For BC, remove in next major.
'identities' => $this->getIdentities(), // For BC, remove in next major.
'identitiesByClass' => $this->getIdentitiesByClass(),
]);
}

Expand All @@ -68,22 +52,42 @@ public function serialize()
public function unserialize($serializedData)
{
$repositoryData = unserialize($serializedData);
$references = $repositoryData['references'];

foreach ($references as $name => $proxyReference) {
$this->setReference(
$name,
$this->getManager()->getReference(
$proxyReference[0], // entity class name
$proxyReference[1] // identifiers
)
);
}

$identities = $repositoryData['identities'];
// For BC, remove in next major.
if (! isset($repositoryData['identitiesByClass'])) {
$references = $repositoryData['references'];

foreach ($references as $name => $proxyReference) {
$this->setReference(
$name,
$this->getManager()->getReference(
$proxyReference[0], // entity class name
$proxyReference[1] // identifiers
)
);
}

$identities = $repositoryData['identities'];

foreach ($identities as $name => $identity) {
$this->setReferenceIdentity($name, $identity);
}

return;
}

foreach ($identities as $name => $identity) {
$this->setReferenceIdentity($name, $identity);
foreach ($repositoryData['identitiesByClass'] as $className => $identities) {
foreach ($identities as $name => $identity) {
$this->setReference(
$name,
$this->getManager()->getReference(
$className,
$identity
)
);

$this->setReferenceIdentity($name, $identity, $className);
}
}
}

Expand Down
Loading

0 comments on commit fb3fa64

Please sign in to comment.