Skip to content

[RFC] add HydrationFactory as extension point for tracking hydrations #12059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 3.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Doctrine\ORM\Cache\CacheConfiguration;
use Doctrine\ORM\Exception\InvalidEntityRepository;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use Doctrine\ORM\Internal\Hydration\DefaultHydratorFactory;
use Doctrine\ORM\Internal\Hydration\HydratorFactory;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
Expand Down Expand Up @@ -47,6 +49,16 @@
/** @phpstan-var array<class-string<AbstractPlatform>, ClassMetadata::GENERATOR_TYPE_*> */
private $identityGenerationPreferences = [];

public function setHydratorFactory(HydratorFactory $factory): void

Check warning on line 52 in src/Configuration.php

View check run for this annotation

Codecov / codecov/patch

src/Configuration.php#L52

Added line #L52 was not covered by tests
{
$this->attributes['hydratorFactory'] = $factory;

Check warning on line 54 in src/Configuration.php

View check run for this annotation

Codecov / codecov/patch

src/Configuration.php#L54

Added line #L54 was not covered by tests
}

public function getHydratorFactory(): HydratorFactory
{
return $this->attributes['hydratorFactory'] ?? new DefaultHydratorFactory();
}

/** @phpstan-param array<class-string<AbstractPlatform>, ClassMetadata::GENERATOR_TYPE_*> $value */
public function setIdentityGenerationPreferences(array $value): void
{
Expand Down
30 changes: 9 additions & 21 deletions src/EntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\Exception\EntityManagerClosed;
use Doctrine\ORM\Exception\InvalidHydrationMode;
use Doctrine\ORM\Exception\MissingIdentifierField;
use Doctrine\ORM\Exception\MissingMappingDriverImplementation;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\Exception\UnrecognizedIdentifierFields;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use Doctrine\ORM\Internal\Hydration\HydratorFactory;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
Expand Down Expand Up @@ -104,6 +104,11 @@ class EntityManager implements EntityManagerInterface
*/
private Cache|null $cache = null;

/**
* The hydrator factory to use.
*/
private HydratorFactory $hydratorFactory;

/**
* Creates a new EntityManager that operates on the given database connection
* and uses the given Configuration and EventManager implementations.
Expand Down Expand Up @@ -150,6 +155,8 @@ public function __construct(
$cacheFactory = $cacheConfig->getCacheFactory();
$this->cache = $cacheFactory->createCache($this);
}

$this->hydratorFactory = $this->config->getHydratorFactory();
}

public function getConnection(): Connection
Expand Down Expand Up @@ -545,15 +552,7 @@ public function getUnitOfWork(): UnitOfWork

public function newHydrator(string|int $hydrationMode): AbstractHydrator
{
return match ($hydrationMode) {
Query::HYDRATE_OBJECT => new Internal\Hydration\ObjectHydrator($this),
Query::HYDRATE_ARRAY => new Internal\Hydration\ArrayHydrator($this),
Query::HYDRATE_SCALAR => new Internal\Hydration\ScalarHydrator($this),
Query::HYDRATE_SINGLE_SCALAR => new Internal\Hydration\SingleScalarHydrator($this),
Query::HYDRATE_SIMPLEOBJECT => new Internal\Hydration\SimpleObjectHydrator($this),
Query::HYDRATE_SCALAR_COLUMN => new Internal\Hydration\ScalarColumnHydrator($this),
default => $this->createCustomHydrator((string) $hydrationMode),
};
return $this->hydratorFactory->create($this, $this->config, $hydrationMode);
}

public function getProxyFactory(): ProxyFactory
Expand Down Expand Up @@ -621,15 +620,4 @@ private function configureMetadataCache(): void

$this->metadataFactory->setCache($metadataCache);
}

private function createCustomHydrator(string $hydrationMode): AbstractHydrator
{
$class = $this->config->getCustomHydrationMode($hydrationMode);

if ($class !== null) {
return new $class($this);
}

throw InvalidHydrationMode::fromMode($hydrationMode);
}
}
37 changes: 37 additions & 0 deletions src/Internal/Hydration/DefaultHydratorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Internal\Hydration;

use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Exception\InvalidHydrationMode;
use Doctrine\ORM\Query;

final class DefaultHydratorFactory implements HydratorFactory
{
public function create(EntityManagerInterface $em, Configuration $config, string|int $hydrationMode): AbstractHydrator
{
return match ($hydrationMode) {
Query::HYDRATE_OBJECT => new ObjectHydrator($em),
Query::HYDRATE_ARRAY => new ArrayHydrator($em),
Query::HYDRATE_SCALAR => new ScalarHydrator($em),
Query::HYDRATE_SINGLE_SCALAR => new SingleScalarHydrator($em),
Query::HYDRATE_SIMPLEOBJECT => new SimpleObjectHydrator($em),
Query::HYDRATE_SCALAR_COLUMN => new ScalarColumnHydrator($em),
default => $this->createCustomHydrator((string) $hydrationMode, $em, $config),
};
}

private function createCustomHydrator(string $hydrationMode, EntityManagerInterface $em, Configuration $config): AbstractHydrator
{
$class = $config->getCustomHydrationMode($hydrationMode);

if ($class !== null) {
return new $class($em);
}

throw InvalidHydrationMode::fromMode($hydrationMode);

Check warning on line 35 in src/Internal/Hydration/DefaultHydratorFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Internal/Hydration/DefaultHydratorFactory.php#L35

Added line #L35 was not covered by tests
}
}
19 changes: 19 additions & 0 deletions src/Internal/Hydration/HydratorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Internal\Hydration;

use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;

interface HydratorFactory
{
/**
* Create a new instance for the given hydration mode.
*
* @psalm-param string|AbstractQuery::HYDRATE_* $hydrationMode
*/
public function create(EntityManagerInterface $em, Configuration $config, string|int $hydrationMode): AbstractHydrator;
}