From 612d447361dc4f8da08a0c8ee86a713801b9a789 Mon Sep 17 00:00:00 2001 From: Michael Babker Date: Tue, 28 May 2024 16:44:07 -0400 Subject: [PATCH] Add an internal trait to allow cross-version compatibility for ORM hydrators --- src/Tool/ORM/Hydration/HydratorCompat.php | 131 ++++++++++++++++++ .../Hydrator/ORM/ObjectHydrator.php | 16 +-- .../Hydrator/ORM/SimpleObjectHydrator.php | 16 +-- src/Tree/Hydrator/ORM/TreeObjectHydrator.php | 4 +- 4 files changed, 144 insertions(+), 23 deletions(-) create mode 100644 src/Tool/ORM/Hydration/HydratorCompat.php diff --git a/src/Tool/ORM/Hydration/HydratorCompat.php b/src/Tool/ORM/Hydration/HydratorCompat.php new file mode 100644 index 0000000000..16af099586 --- /dev/null +++ b/src/Tool/ORM/Hydration/HydratorCompat.php @@ -0,0 +1,131 @@ + http://www.gediminasm.org + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gedmo\Tool\ORM\Hydration; + +use Doctrine\ORM\Internal\Hydration\AbstractHydrator; + +// The methods we need the compat bridge for are protected, so we're using a public method for this check +if ((new \ReflectionClass(AbstractHydrator::class))->getMethod('onClear')->hasReturnType()) { + // ORM 3.x + /** + * Helper trait to address compatibility issues between ORM 2.x and 3.x. + * + * @mixin AbstractHydrator + * + * @internal + */ + trait HydratorCompat + { + /** + * Executes one-time preparation tasks, once each time hydration is started + * through {@link hydrateAll} or {@link toIterable()}. + */ + protected function prepare(): void + { + $this->doPrepareWithCompat(); + } + + protected function doPrepareWithCompat(): void + { + parent::prepare(); + } + + /** + * Executes one-time cleanup tasks at the end of a hydration that was initiated + * through {@link hydrateAll} or {@link toIterable()}. + */ + protected function cleanup(): void + { + $this->doCleanupWithCompat(); + } + + protected function doCleanupWithCompat(): void + { + parent::cleanup(); + } + + /** + * Hydrates all rows from the current statement instance at once. + */ + protected function hydrateAllData(): mixed + { + return $this->doHydrateAllData(); + } + + /** + * @return mixed[] + */ + protected function doHydrateAllData() + { + return parent::hydrateAllData(); + } + } +} else { + // ORM 2.x + /** + * Helper trait to address compatibility issues between ORM 2.x and 3.x. + * + * @mixin AbstractHydrator + * + * @internal + */ + trait HydratorCompat + { + /** + * Executes one-time preparation tasks, once each time hydration is started + * through {@link hydrateAll} or {@link toIterable()}. + * + * @return void + */ + protected function prepare() + { + $this->doPrepareWithCompat(); + } + + protected function doPrepareWithCompat(): void + { + parent::prepare(); + } + + /** + * Executes one-time cleanup tasks at the end of a hydration that was initiated + * through {@link hydrateAll} or {@link toIterable()}. + * + * @return void + */ + protected function cleanup() + { + $this->doCleanupWithCompat(); + } + + protected function doCleanupWithCompat(): void + { + parent::cleanup(); + } + + /** + * Hydrates all rows from the current statement instance at once. + * + * @return mixed[] + */ + protected function hydrateAllData() + { + return $this->doHydrateAllData(); + } + + /** + * @return mixed[] + */ + protected function doHydrateAllData() + { + return parent::hydrateAllData(); + } + } +} diff --git a/src/Translatable/Hydrator/ORM/ObjectHydrator.php b/src/Translatable/Hydrator/ORM/ObjectHydrator.php index 35bac67a54..a8e9983de0 100644 --- a/src/Translatable/Hydrator/ORM/ObjectHydrator.php +++ b/src/Translatable/Hydrator/ORM/ObjectHydrator.php @@ -12,6 +12,7 @@ use Doctrine\ORM\Internal\Hydration\ObjectHydrator as BaseObjectHydrator; use Gedmo\Exception\RuntimeException; use Gedmo\Tool\ORM\Hydration\EntityManagerRetriever; +use Gedmo\Tool\ORM\Hydration\HydratorCompat; use Gedmo\Translatable\TranslatableListener; /** @@ -27,21 +28,17 @@ class ObjectHydrator extends BaseObjectHydrator { use EntityManagerRetriever; + use HydratorCompat; /** * State of skipOnLoad for listener between hydrations * * @see ObjectHydrator::prepare() * @see ObjectHydrator::cleanup() - * - * @var bool|null */ - private $savedSkipOnLoad; + private ?bool $savedSkipOnLoad = null; - /** - * @return void - */ - protected function prepare() + protected function doPrepareWithCompat(): void { $listener = $this->getTranslatableListener(); $this->savedSkipOnLoad = $listener->isSkipOnLoad(); @@ -49,10 +46,7 @@ protected function prepare() parent::prepare(); } - /** - * @return void - */ - protected function cleanup() + protected function doCleanupWithCompat(): void { parent::cleanup(); $listener = $this->getTranslatableListener(); diff --git a/src/Translatable/Hydrator/ORM/SimpleObjectHydrator.php b/src/Translatable/Hydrator/ORM/SimpleObjectHydrator.php index a9a2963f88..0410018c46 100644 --- a/src/Translatable/Hydrator/ORM/SimpleObjectHydrator.php +++ b/src/Translatable/Hydrator/ORM/SimpleObjectHydrator.php @@ -12,6 +12,7 @@ use Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator as BaseSimpleObjectHydrator; use Gedmo\Exception\RuntimeException; use Gedmo\Tool\ORM\Hydration\EntityManagerRetriever; +use Gedmo\Tool\ORM\Hydration\HydratorCompat; use Gedmo\Translatable\TranslatableListener; /** @@ -27,21 +28,17 @@ class SimpleObjectHydrator extends BaseSimpleObjectHydrator { use EntityManagerRetriever; + use HydratorCompat; /** * State of skipOnLoad for listener between hydrations * * @see SimpleObjectHydrator::prepare() * @see SimpleObjectHydrator::cleanup() - * - * @var bool|null */ - private $savedSkipOnLoad; + private ?bool $savedSkipOnLoad = null; - /** - * @return void - */ - protected function prepare() + protected function doPrepareWithCompat(): void { $listener = $this->getTranslatableListener(); $this->savedSkipOnLoad = $listener->isSkipOnLoad(); @@ -49,10 +46,7 @@ protected function prepare() parent::prepare(); } - /** - * @return void - */ - protected function cleanup() + protected function doCleanupWithCompat(): void { parent::cleanup(); $listener = $this->getTranslatableListener(); diff --git a/src/Tree/Hydrator/ORM/TreeObjectHydrator.php b/src/Tree/Hydrator/ORM/TreeObjectHydrator.php index 0dc984c6d1..cb16778419 100644 --- a/src/Tree/Hydrator/ORM/TreeObjectHydrator.php +++ b/src/Tree/Hydrator/ORM/TreeObjectHydrator.php @@ -15,6 +15,7 @@ use Doctrine\ORM\PersistentCollection; use Gedmo\Exception\InvalidMappingException; use Gedmo\Tool\ORM\Hydration\EntityManagerRetriever; +use Gedmo\Tool\ORM\Hydration\HydratorCompat; use Gedmo\Tree\TreeListener; /** @@ -27,6 +28,7 @@ class TreeObjectHydrator extends ObjectHydrator { use EntityManagerRetriever; + use HydratorCompat; /** * @var array @@ -66,7 +68,7 @@ public function setPropertyValue($object, $property, $value) * * @return array */ - protected function hydrateAllData() + protected function doHydrateAllData() { $data = parent::hydrateAllData();