Skip to content

Commit 3ad0ca6

Browse files
committed
Add LockKeyNormalizer
1 parent c3c37f2 commit 3ad0ca6

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
use Symfony\Component\Lock\LockFactory;
109109
use Symfony\Component\Lock\LockInterface;
110110
use Symfony\Component\Lock\PersistingStoreInterface;
111+
use Symfony\Component\Lock\Serializer\LockKeyNormalizer;
111112
use Symfony\Component\Lock\Store\StoreFactory;
112113
use Symfony\Component\Mailer\Bridge as MailerBridge;
113114
use Symfony\Component\Mailer\Command\MailerTestCommand;
@@ -2128,6 +2129,11 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
21282129
{
21292130
$loader->load('lock.php');
21302131

2132+
// BC layer Lock < 7.3
2133+
if (!class_exists(DenormalizerInterface::class) || !class_exists(LockKeyNormalizer::class)) {
2134+
$container->removeDefinition('serializer.normalizer.lock_key');
2135+
}
2136+
21312137
foreach ($config['resources'] as $resourceName => $resourceStores) {
21322138
if (0 === \count($resourceStores)) {
21332139
continue;

src/Symfony/Bundle/FrameworkBundle/Resources/config/lock.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

1414
use Symfony\Component\Lock\LockFactory;
15+
use Symfony\Component\Lock\Serializer\LockKeyNormalizer;
1516
use Symfony\Component\Lock\Store\CombinedStore;
1617
use Symfony\Component\Lock\Strategy\ConsensusStrategy;
1718

@@ -26,5 +27,8 @@
2627
->args([abstract_arg('Store')])
2728
->call('setLogger', [service('logger')->ignoreOnInvalid()])
2829
->tag('monolog.logger', ['channel' => 'lock'])
30+
31+
->set('serializer.normalizer.lock_key', LockKeyNormalizer::class)
32+
->tag('serializer.normalizer', ['built_in' => true, 'priority' => -880])
2933
;
3034
};

src/Symfony/Component/Lock/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support for `valkey:` / `valkeys:` schemes
8+
* Add `LockKeyNormalizer`
89

910
7.2
1011
---
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Lock\Serializer;
13+
14+
use Symfony\Component\Lock\Key;
15+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
16+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
17+
18+
class LockKeyNormalizer implements NormalizerInterface, DenormalizerInterface
19+
{
20+
public function getSupportedTypes(?string $format): array
21+
{
22+
return [Key::class => true];
23+
}
24+
25+
/**
26+
* @param Key $data
27+
*/
28+
public function normalize(mixed $data, ?string $format = null, array $context = []): array
29+
{
30+
return (fn () => array_intersect_key(get_object_vars($this), array_flip($this->__sleep())))->bindTo($data, Key::class)();
31+
}
32+
33+
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
34+
{
35+
return $data instanceof Key;
36+
}
37+
38+
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): Key
39+
{
40+
$key = (new \ReflectionClass(Key::class))->newInstanceWithoutConstructor();
41+
$setter = (fn ($field) => $this->$field = $data[$field])->bindTo($key, Key::class);
42+
foreach ($key->__sleep() as $serializedField) {
43+
$setter($serializedField);
44+
}
45+
46+
return $key;
47+
}
48+
49+
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
50+
{
51+
return Key::class === $type;
52+
}
53+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Symfony\Component\Lock\Tests\Serializer;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Lock\Exception\UnserializableKeyException;
7+
use Symfony\Component\Lock\Key;
8+
use Symfony\Component\Lock\Serializer\LockKeyNormalizer;
9+
10+
class LockKeyNormalizerTest extends TestCase
11+
{
12+
private LockKeyNormalizer $normalizer;
13+
14+
protected function setUp(): void
15+
{
16+
$this->normalizer = new LockKeyNormalizer();
17+
}
18+
19+
public function testNormalize()
20+
{
21+
$key = new Key(__METHOD__);
22+
$key->reduceLifetime(1);
23+
$key->setState('foo', 'bar');
24+
25+
$copy = $this->normalizer->denormalize($this->normalizer->normalize($key), Key::class);
26+
$this->assertSame($key->getState('foo'), $copy->getState('foo'));
27+
$this->assertEqualsWithDelta($key->getRemainingLifetime(), $copy->getRemainingLifetime(), 0.001);
28+
}
29+
30+
public function testDenormalize()
31+
{
32+
$key = new Key(__METHOD__);
33+
$key->markUnserializable();
34+
35+
$this->expectException(UnserializableKeyException::class);
36+
$this->normalizer->normalize($key);
37+
}
38+
}

src/Symfony/Component/Lock/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
},
2222
"require-dev": {
2323
"doctrine/dbal": "^3.6|^4",
24-
"predis/predis": "^1.1|^2.0"
24+
"predis/predis": "^1.1|^2.0",
25+
"symfony/serializer": "^6.4|^7.0"
2526
},
2627
"conflict": {
2728
"doctrine/dbal": "<3.6",

0 commit comments

Comments
 (0)