diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index c4a05efcd47..22651ef6ff3 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -26,6 +26,8 @@ use Doctrine\ORM\Exception\ProxyClassesAlwaysRegenerating; use Doctrine\ORM\Exception\UnknownEntityNamespace; use Doctrine\ORM\Internal\Hydration\AbstractHydrator; +use Doctrine\ORM\Internal\Hydration\DefaultHydratorFactory; +use Doctrine\ORM\Internal\Hydration\HydratorFactory; use Doctrine\ORM\Mapping\ClassMetadataFactory; use Doctrine\ORM\Mapping\DefaultEntityListenerResolver; use Doctrine\ORM\Mapping\DefaultNamingStrategy; @@ -64,6 +66,16 @@ class Configuration extends \Doctrine\DBAL\Configuration /** @var mixed[] */ protected $_attributes = []; + public function setHydratorFactory(HydratorFactory $factory): void + { + $this->_attributes['hydratorFactory'] = $factory; + } + + public function getHydratorFactory(): HydratorFactory + { + return $this->_attributes['hydratorFactory'] ?? new DefaultHydratorFactory(); + } + /** * Sets the directory where Doctrine generates any necessary proxy class files. * diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 2697a8e88c7..138f4286239 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -13,11 +13,11 @@ use Doctrine\DBAL\LockMode; use Doctrine\Deprecations\Deprecation; use Doctrine\ORM\Exception\EntityManagerClosed; -use Doctrine\ORM\Exception\InvalidHydrationMode; use Doctrine\ORM\Exception\MismatchedEventManager; use Doctrine\ORM\Exception\MissingIdentifierField; use Doctrine\ORM\Exception\MissingMappingDriverImplementation; use Doctrine\ORM\Exception\UnrecognizedIdentifierFields; +use Doctrine\ORM\Internal\Hydration\HydratorFactory; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\Mapping\ClassMetadataFactory; use Doctrine\ORM\Proxy\ProxyFactory; @@ -145,6 +145,13 @@ */ private $cache; + /** + * The hydrator factory to use. + * + * @var HydratorFactory + */ + private $hydratorFactory; + /** * Creates a new EntityManager that operates on the given database connection * and uses the given Configuration and EventManager implementations. @@ -162,6 +169,7 @@ protected function __construct(Connection $conn, Configuration $config, EventMan $this->configureMetadataCache(); + $this->hydratorFactory = $config->getHydratorFactory(); $this->repositoryFactory = $config->getRepositoryFactory(); $this->unitOfWork = new UnitOfWork($this); $this->proxyFactory = new ProxyFactory( @@ -856,34 +864,7 @@ public function getHydrator($hydrationMode) */ public function newHydrator($hydrationMode) { - switch ($hydrationMode) { - case Query::HYDRATE_OBJECT: - return new Internal\Hydration\ObjectHydrator($this); - - case Query::HYDRATE_ARRAY: - return new Internal\Hydration\ArrayHydrator($this); - - case Query::HYDRATE_SCALAR: - return new Internal\Hydration\ScalarHydrator($this); - - case Query::HYDRATE_SINGLE_SCALAR: - return new Internal\Hydration\SingleScalarHydrator($this); - - case Query::HYDRATE_SIMPLEOBJECT: - return new Internal\Hydration\SimpleObjectHydrator($this); - - case Query::HYDRATE_SCALAR_COLUMN: - return new Internal\Hydration\ScalarColumnHydrator($this); - - default: - $class = $this->config->getCustomHydrationMode($hydrationMode); - - if ($class !== null) { - return new $class($this); - } - } - - throw InvalidHydrationMode::fromMode((string) $hydrationMode); + return $this->hydratorFactory->create($this, $this->config, $hydrationMode); } /** diff --git a/lib/Doctrine/ORM/Internal/Hydration/DefaultHydratorFactory.php b/lib/Doctrine/ORM/Internal/Hydration/DefaultHydratorFactory.php new file mode 100644 index 00000000000..84b3056ae27 --- /dev/null +++ b/lib/Doctrine/ORM/Internal/Hydration/DefaultHydratorFactory.php @@ -0,0 +1,49 @@ +getCustomHydrationMode($hydrationMode); + + if ($class !== null) { + return new $class($em); + } + } + + throw InvalidHydrationMode::fromMode((string) $hydrationMode); + } +} diff --git a/lib/Doctrine/ORM/Internal/Hydration/HydratorFactory.php b/lib/Doctrine/ORM/Internal/Hydration/HydratorFactory.php new file mode 100644 index 00000000000..7ae3e065e0d --- /dev/null +++ b/lib/Doctrine/ORM/Internal/Hydration/HydratorFactory.php @@ -0,0 +1,20 @@ + new $metadataFactoryClassName() - - (string) $hydrationMode - is_object($connection) ': "' . $connection . '"' + + - new $class($this) + new $class($em) + + (string) $hydrationMode +