-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Lock] Add LockKeyNormalizer
#60023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.4
Are you sure you want to change the base?
[Lock] Add LockKeyNormalizer
#60023
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.4 | ||
--- | ||
|
||
* Add `LockKeyNormalizer` | ||
|
||
7.3 | ||
--- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Lock\Serializer; | ||
|
||
use Symfony\Component\Lock\Key; | ||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
/** | ||
* Normalize {@see Key} instance to transfer an acquired lock between processes. | ||
* | ||
* @author Valtteri R <valtzu@gmail.com> | ||
*/ | ||
class LockKeyNormalizer implements NormalizerInterface, DenormalizerInterface | ||
{ | ||
public function getSupportedTypes(?string $format): array | ||
Check failure on line 25 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php
|
||
{ | ||
return [Key::class => true]; | ||
} | ||
|
||
/** | ||
* @param Key $data | ||
*/ | ||
public function normalize(mixed $data, ?string $format = null, array $context = []): array | ||
Check failure on line 33 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php
|
||
{ | ||
return (fn () => array_intersect_key(get_object_vars($this), array_flip($this->__sleep())))->bindTo($data, Key::class)(); | ||
Check failure on line 35 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php
|
||
} | ||
|
||
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool | ||
{ | ||
return $data instanceof Key; | ||
} | ||
|
||
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): Key | ||
{ | ||
$key = (new \ReflectionClass(Key::class))->newInstanceWithoutConstructor(); | ||
(function () use ($data) { | ||
foreach ($this->__sleep() as $serializedField) { | ||
Check failure on line 47 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php
|
||
$this->$serializedField = $data[$serializedField]; | ||
} | ||
})->bindTo($key, Key::class)(); | ||
|
||
return $key; | ||
} | ||
|
||
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool | ||
{ | ||
return Key::class === $type; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Lock\Tests\Serializer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Lock\Exception\UnserializableKeyException; | ||
use Symfony\Component\Lock\Key; | ||
use Symfony\Component\Lock\Serializer\LockKeyNormalizer; | ||
|
||
class LockKeyNormalizerTest extends TestCase | ||
{ | ||
private LockKeyNormalizer $normalizer; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->normalizer = new LockKeyNormalizer(); | ||
} | ||
|
||
public function testNormalizeAndDenormalize() | ||
{ | ||
$key = new Key(__METHOD__); | ||
$key->reduceLifetime(1); | ||
$key->setState('foo', 'bar'); | ||
|
||
$copy = $this->normalizer->denormalize($this->normalizer->normalize($key), Key::class); | ||
$this->assertSame($key->getState('foo'), $copy->getState('foo')); | ||
$this->assertEqualsWithDelta($key->getRemainingLifetime(), $copy->getRemainingLifetime(), 0.001); | ||
} | ||
|
||
public function testNormalizingUnserializableLockThrows() | ||
{ | ||
$key = new Key(__METHOD__); | ||
$key->markUnserializable(); | ||
|
||
$this->expectException(UnserializableKeyException::class); | ||
$this->normalizer->normalize($key); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add @auhor and a description?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, done