Skip to content

Commit

Permalink
Add an internal trait to allow cross-version compatibility for ORM hy…
Browse files Browse the repository at this point in the history
…drators
  • Loading branch information
mbabker committed May 28, 2024
1 parent 8d658b4 commit aa1bfd2
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 23 deletions.
131 changes: 131 additions & 0 deletions src/Tool/ORM/Hydration/HydratorCompat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/*
* This file is part of the Doctrine Behavioral Extensions package.
* (c) Gediminas Morkevicius <[email protected]> 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();
}
}
}
16 changes: 5 additions & 11 deletions src/Translatable/Hydrator/ORM/ObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -27,32 +28,25 @@
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();
$listener->setSkipOnLoad(true);
parent::prepare();
}

/**
* @return void
*/
protected function cleanup()
protected function doCleanupWithCompat(): void
{
parent::cleanup();
$listener = $this->getTranslatableListener();
Expand Down
16 changes: 5 additions & 11 deletions src/Translatable/Hydrator/ORM/SimpleObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -27,32 +28,25 @@
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();
$listener->setSkipOnLoad(true);
parent::prepare();
}

/**
* @return void
*/
protected function cleanup()
protected function doCleanupWithCompat(): void
{
parent::cleanup();
$listener = $this->getTranslatableListener();
Expand Down
4 changes: 3 additions & 1 deletion src/Tree/Hydrator/ORM/TreeObjectHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -27,6 +28,7 @@
class TreeObjectHydrator extends ObjectHydrator
{
use EntityManagerRetriever;
use HydratorCompat;

/**
* @var array<string, mixed>
Expand Down Expand Up @@ -66,7 +68,7 @@ public function setPropertyValue($object, $property, $value)
*
* @return array<int, object>
*/
protected function hydrateAllData()
protected function doHydrateAllData()
{
$data = parent::hydrateAllData();

Expand Down

0 comments on commit aa1bfd2

Please sign in to comment.