|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Atk4\Data\Type; |
| 6 | + |
| 7 | +use Atk4\Data\Exception; |
| 8 | +use Doctrine\DBAL\Platforms\AbstractPlatform; |
| 9 | +use Doctrine\DBAL\Types as DbalTypes; |
| 10 | + |
| 11 | +/** |
| 12 | + * Type that allows to weakly reference a local PHP object using a scalar string |
| 13 | + * and get the original object instance back using the string. |
| 14 | + * |
| 15 | + * The local object is never serialized. |
| 16 | + * |
| 17 | + * An exception is thrown when getting an object from a string back and the original |
| 18 | + * object instance has been destroyed/released. |
| 19 | + */ |
| 20 | +class LocalObjectType extends DbalTypes\Type |
| 21 | +{ |
| 22 | + private ?string $instanceUid = null; |
| 23 | + |
| 24 | + private int $localUidCounter; |
| 25 | + |
| 26 | + /** @var \WeakMap<object, LocalObjectHandle> */ |
| 27 | + private \WeakMap $handles; |
| 28 | + /** @var array<int, \WeakReference<LocalObjectHandle>> */ |
| 29 | + private array $handlesIndex; |
| 30 | + |
| 31 | + protected function __clone() |
| 32 | + { |
| 33 | + // prevent clonning |
| 34 | + } |
| 35 | + |
| 36 | + protected function init(): void |
| 37 | + { |
| 38 | + $this->instanceUid = hash('sha256', microtime(true) . random_bytes(64)); |
| 39 | + $this->localUidCounter = 0; |
| 40 | + $this->handles = new \WeakMap(); |
| 41 | + $this->handlesIndex = []; |
| 42 | + } |
| 43 | + |
| 44 | + public function getName(): string |
| 45 | + { |
| 46 | + return Types::LOCAL_OBJECT; |
| 47 | + } |
| 48 | + |
| 49 | + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string |
| 50 | + { |
| 51 | + return DbalTypes\Type::getType(DbalTypes\Types::STRING)->getSQLDeclaration($fieldDeclaration, $platform); |
| 52 | + } |
| 53 | + |
| 54 | + public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string |
| 55 | + { |
| 56 | + if ($value === null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + if ($this->instanceUid === null) { |
| 61 | + $this->init(); |
| 62 | + } |
| 63 | + |
| 64 | + $handle = $this->handles->offsetExists($value) |
| 65 | + ? $this->handles->offsetGet($value) |
| 66 | + : null; |
| 67 | + |
| 68 | + if ($handle === null) { |
| 69 | + $handle = new LocalObjectHandle(++$this->localUidCounter, $value, function (LocalObjectHandle $handle): void { |
| 70 | + unset($this->handlesIndex[$handle->getLocalUid()]); |
| 71 | + }); |
| 72 | + $this->handles->offsetSet($value, $handle); |
| 73 | + $this->handlesIndex[$handle->getLocalUid()] = \WeakReference::create($handle); |
| 74 | + } |
| 75 | + |
| 76 | + $className = get_debug_type($value); |
| 77 | + if (strlen($className) > 160) { // keep result below 255 bytes |
| 78 | + $className = mb_strcut($className, 0, 80) |
| 79 | + . '...' |
| 80 | + . mb_strcut(substr($className, strlen(mb_strcut($className, 0, 80))), -80); |
| 81 | + } |
| 82 | + |
| 83 | + return $className . '-' . $this->instanceUid . '-' . $handle->getLocalUid(); |
| 84 | + } |
| 85 | + |
| 86 | + public function convertToPHPValue($value, AbstractPlatform $platform): ?object |
| 87 | + { |
| 88 | + if ($value === null || trim($value) === '') { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + $valueExploded = explode('-', $value, 3); |
| 93 | + if (count($valueExploded) !== 3 |
| 94 | + || $valueExploded[1] !== $this->instanceUid |
| 95 | + || $valueExploded[2] !== (string) (int) $valueExploded[2] |
| 96 | + ) { |
| 97 | + throw new Exception('Local object does not match the DBAL type instance'); |
| 98 | + } |
| 99 | + $handle = $this->handlesIndex[(int) $valueExploded[2]] ?? null; |
| 100 | + if ($handle !== null) { |
| 101 | + $handle = $handle->get(); |
| 102 | + } |
| 103 | + $res = $handle !== null ? $handle->getValue() : null; |
| 104 | + if ($res === null) { |
| 105 | + throw new Exception('Local object does no longer exist'); |
| 106 | + } |
| 107 | + |
| 108 | + return $res; |
| 109 | + } |
| 110 | + |
| 111 | + public function requiresSQLCommentHint(AbstractPlatform $platform): bool |
| 112 | + { |
| 113 | + return true; |
| 114 | + } |
| 115 | +} |
0 commit comments