Skip to content

Commit a9201d2

Browse files
committed
[Lock] Add LockKeyNormalizer
1 parent d573b58 commit a9201d2

File tree

6 files changed

+123
-1
lines changed

6 files changed

+123
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
use Symfony\Component\Lock\LockFactory;
116116
use Symfony\Component\Lock\LockInterface;
117117
use Symfony\Component\Lock\PersistingStoreInterface;
118+
use Symfony\Component\Lock\Serializer\LockKeyNormalizer;
118119
use Symfony\Component\Lock\Store\StoreFactory;
119120
use Symfony\Component\Mailer\Bridge as MailerBridge;
120121
use Symfony\Component\Mailer\Command\MailerTestCommand;
@@ -2199,6 +2200,11 @@ private function registerLockConfiguration(array $config, ContainerBuilder $cont
21992200
{
22002201
$loader->load('lock.php');
22012202

2203+
// BC layer Lock < 7.3
2204+
if (!interface_exists(DenormalizerInterface::class) || !class_exists(LockKeyNormalizer::class)) {
2205+
$container->removeDefinition('serializer.normalizer.lock_key');
2206+
}
2207+
22022208
foreach ($config['resources'] as $resourceName => $resourceStores) {
22032209
if (0 === \count($resourceStores)) {
22042210
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.4
5+
---
6+
7+
* Add `LockKeyNormalizer`
8+
49
7.3
510
---
611

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
/**
19+
* Normalize {@see Key} instance to transfer an acquired lock between processes.
20+
*
21+
* @author Valtteri R <valtzu@gmail.com>
22+
*/
23+
class LockKeyNormalizer implements NormalizerInterface, DenormalizerInterface
24+
{
25+
public function getSupportedTypes(?string $format): array
26+
{
27+
return [Key::class => true];
28+
}
29+
30+
/**
31+
* @param Key $data
32+
*/
33+
public function normalize(mixed $data, ?string $format = null, array $context = []): array
34+
{
35+
return (fn () => array_intersect_key(get_object_vars($this), array_flip($this->__sleep())))->bindTo($data, Key::class)();
36+
}
37+
38+
public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
39+
{
40+
return $data instanceof Key;
41+
}
42+
43+
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): Key
44+
{
45+
$key = (new \ReflectionClass(Key::class))->newInstanceWithoutConstructor();
46+
(function () use ($data) {
47+
foreach ($this->__sleep() as $serializedField) {
48+
$this->$serializedField = $data[$serializedField];
49+
}
50+
})->bindTo($key, Key::class)();
51+
52+
return $key;
53+
}
54+
55+
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
56+
{
57+
return Key::class === $type;
58+
}
59+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Tests\Serializer;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Lock\Exception\UnserializableKeyException;
16+
use Symfony\Component\Lock\Key;
17+
use Symfony\Component\Lock\Serializer\LockKeyNormalizer;
18+
19+
class LockKeyNormalizerTest extends TestCase
20+
{
21+
private LockKeyNormalizer $normalizer;
22+
23+
protected function setUp(): void
24+
{
25+
$this->normalizer = new LockKeyNormalizer();
26+
}
27+
28+
public function testNormalizeAndDenormalize()
29+
{
30+
$key = new Key(__METHOD__);
31+
$key->reduceLifetime(1);
32+
$key->setState('foo', 'bar');
33+
34+
$copy = $this->normalizer->denormalize($this->normalizer->normalize($key), Key::class);
35+
$this->assertSame($key->getState('foo'), $copy->getState('foo'));
36+
$this->assertEqualsWithDelta($key->getRemainingLifetime(), $copy->getRemainingLifetime(), 0.001);
37+
}
38+
39+
public function testNormalizingUnserializableLockThrows()
40+
{
41+
$key = new Key(__METHOD__);
42+
$key->markUnserializable();
43+
44+
$this->expectException(UnserializableKeyException::class);
45+
$this->normalizer->normalize($key);
46+
}
47+
}

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)