-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RawValuePropertyAccessor to see how it will look in 8.4, pre supp…
…ort for lazy objects.
- Loading branch information
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
use Doctrine\ORM\Mapping\PropertyAccessors\EnumPropertyAccessor; | ||
use Doctrine\ORM\Mapping\PropertyAccessors\ObjectCastPropertyAccessor; | ||
use Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor; | ||
use Doctrine\ORM\Mapping\PropertyAccessors\RawValuePropertyAccessor; | ||
use Doctrine\ORM\Mapping\PropertyAccessors\ReadonlyAccessor; | ||
use Doctrine\ORM\Mapping\PropertyAccessors\TypedNoDefaultPropertyAccessor; | ||
use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata; | ||
|
@@ -62,6 +63,8 @@ | |
use function trait_exists; | ||
use function trim; | ||
|
||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata | ||
* of an entity and its associations. | ||
|
@@ -2689,7 +2692,10 @@ public function getSequencePrefix(AbstractPlatform $platform): string | |
private function createPropertyAccessor(string $className, string $propertyName): PropertyAccessor | ||
Check failure on line 2692 in src/Mapping/ClassMetadata.php GitHub Actions / Static Analysis with PHPStan (default, phpstan.neon)
Check failure on line 2692 in src/Mapping/ClassMetadata.php GitHub Actions / Static Analysis with PHPStan (3.8.2, phpstan-dbal3.neon)
|
||
{ | ||
$reflectionProperty = new ReflectionProperty($className, $propertyName); | ||
$accessor = ObjectCastPropertyAccessor::fromReflectionProperty($reflectionProperty); | ||
|
||
$accessor = PHP_VERSION_ID >= 80400 | ||
? RawValuePropertyAccessor::fromReflectionProperty($reflectionProperty) | ||
: ObjectCastPropertyAccessor::fromReflectionProperty($reflectionProperty); | ||
|
||
if ($reflectionProperty->hasType() && ! $reflectionProperty->getType()->allowsNull()) { | ||
$accessor = new TypedNoDefaultPropertyAccessor($accessor, $reflectionProperty); | ||
|
52 changes: 52 additions & 0 deletions
52
src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Mapping\PropertyAccessors; | ||
|
||
use Doctrine\ORM\Proxy\InternalProxy; | ||
use ReflectionProperty; | ||
|
||
use function ltrim; | ||
|
||
/** | ||
* This is a PHP 8.4 and up only class and replaces ObjectCastPropertyAccessor. | ||
* | ||
* It works based on the raw values of a property, which for a case of property hooks | ||
* is the backed value. If we kept using setValue/getValue, this would go through the hooks, | ||
* which potentially change the data. | ||
*/ | ||
class RawValuePropertyAccessor implements PropertyAccessor | ||
{ | ||
public static function fromReflectionProperty(ReflectionProperty $reflectionProperty): self | ||
{ | ||
$name = $reflectionProperty->getName(); | ||
$key = $reflectionProperty->isPrivate() ? "\0" . ltrim($reflectionProperty->getDeclaringClass()->getName(), '\\') . "\0" . $name : ($reflectionProperty->isProtected() ? "\0*\0" . $name : $name); | ||
|
||
return new self($reflectionProperty, $key); | ||
} | ||
|
||
private function __construct(private ReflectionProperty $reflectionProperty, private string $key) | ||
{ | ||
} | ||
|
||
public function setValue(object $object, mixed $value): void | ||
{ | ||
if (! ($object instanceof InternalProxy && ! $object->__isInitialized())) { | ||
$this->reflectionProperty->setRawValue($object, $value); | ||
|
||
return; | ||
} | ||
|
||
$object->__setInitialized(true); | ||
|
||
$this->reflectionProperty->setRawValue($object, $value); | ||
|
||
$object->__setInitialized(false); | ||
} | ||
|
||
public function getValue(object $object): mixed | ||
{ | ||
return ((array) $object)[$this->key] ?? null; | ||
} | ||
} |