From a9201d2a62571caa59ed59ad04a0128be540e3ec Mon Sep 17 00:00:00 2001 From: valtzu Date: Sun, 23 Mar 2025 14:08:49 +0200 Subject: [PATCH] [Lock] Add `LockKeyNormalizer` --- .../FrameworkExtension.php | 6 ++ .../FrameworkBundle/Resources/config/lock.php | 4 ++ src/Symfony/Component/Lock/CHANGELOG.md | 5 ++ .../Lock/Serializer/LockKeyNormalizer.php | 59 +++++++++++++++++++ .../Serializer/LockKeyNormalizerTest.php | 47 +++++++++++++++ src/Symfony/Component/Lock/composer.json | 3 +- 6 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php create mode 100644 src/Symfony/Component/Lock/Tests/Serializer/LockKeyNormalizerTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index e055f5f8bea53..7c04a763bb370 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -115,6 +115,7 @@ use Symfony\Component\Lock\LockFactory; use Symfony\Component\Lock\LockInterface; use Symfony\Component\Lock\PersistingStoreInterface; +use Symfony\Component\Lock\Serializer\LockKeyNormalizer; use Symfony\Component\Lock\Store\StoreFactory; use Symfony\Component\Mailer\Bridge as MailerBridge; use Symfony\Component\Mailer\Command\MailerTestCommand; @@ -2199,6 +2200,11 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont { $loader->load('lock.php'); + // BC layer Lock < 7.3 + if (!interface_exists(DenormalizerInterface::class) || !class_exists(LockKeyNormalizer::class)) { + $container->removeDefinition('serializer.normalizer.lock_key'); + } + foreach ($config['resources'] as $resourceName => $resourceStores) { if (0 === \count($resourceStores)) { continue; diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/lock.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/lock.php index 4e14636211c2d..2dd64af772eb3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/lock.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/lock.php @@ -12,6 +12,7 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use Symfony\Component\Lock\LockFactory; +use Symfony\Component\Lock\Serializer\LockKeyNormalizer; use Symfony\Component\Lock\Store\CombinedStore; use Symfony\Component\Lock\Strategy\ConsensusStrategy; @@ -26,5 +27,8 @@ ->args([abstract_arg('Store')]) ->call('setLogger', [service('logger')->ignoreOnInvalid()]) ->tag('monolog.logger', ['channel' => 'lock']) + + ->set('serializer.normalizer.lock_key', LockKeyNormalizer::class) + ->tag('serializer.normalizer', ['built_in' => true, 'priority' => -880]) ; }; diff --git a/src/Symfony/Component/Lock/CHANGELOG.md b/src/Symfony/Component/Lock/CHANGELOG.md index 1ea898d7ee96d..69de4d54173a6 100644 --- a/src/Symfony/Component/Lock/CHANGELOG.md +++ b/src/Symfony/Component/Lock/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.4 +--- + + * Add `LockKeyNormalizer` + 7.3 --- diff --git a/src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php b/src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php new file mode 100644 index 0000000000000..26189d9ccf874 --- /dev/null +++ b/src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php @@ -0,0 +1,59 @@ + + * + * 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 + */ +class LockKeyNormalizer implements NormalizerInterface, DenormalizerInterface +{ + public function getSupportedTypes(?string $format): array + { + return [Key::class => true]; + } + + /** + * @param Key $data + */ + public function normalize(mixed $data, ?string $format = null, array $context = []): array + { + return (fn () => array_intersect_key(get_object_vars($this), array_flip($this->__sleep())))->bindTo($data, Key::class)(); + } + + 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) { + $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; + } +} diff --git a/src/Symfony/Component/Lock/Tests/Serializer/LockKeyNormalizerTest.php b/src/Symfony/Component/Lock/Tests/Serializer/LockKeyNormalizerTest.php new file mode 100644 index 0000000000000..b1540a5ff9502 --- /dev/null +++ b/src/Symfony/Component/Lock/Tests/Serializer/LockKeyNormalizerTest.php @@ -0,0 +1,47 @@ + + * + * 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); + } +} diff --git a/src/Symfony/Component/Lock/composer.json b/src/Symfony/Component/Lock/composer.json index 61f53e06f268e..bf11d1a3c1e16 100644 --- a/src/Symfony/Component/Lock/composer.json +++ b/src/Symfony/Component/Lock/composer.json @@ -21,7 +21,8 @@ }, "require-dev": { "doctrine/dbal": "^3.6|^4", - "predis/predis": "^1.1|^2.0" + "predis/predis": "^1.1|^2.0", + "symfony/serializer": "^6.4|^7.0" }, "conflict": { "doctrine/dbal": "<3.6", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy