Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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;
Expand Down Expand Up @@ -2128,6 +2129,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;
Expand Down
4 changes: 4 additions & 0 deletions 4 src/Symfony/Bundle/FrameworkBundle/Resources/config/lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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])
;
};
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add support for `valkey:` / `valkeys:` schemes
* Add `LockKeyNormalizer`

7.2
---
Expand Down
53 changes: 53 additions & 0 deletions 53 src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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;

class LockKeyNormalizer implements NormalizerInterface, DenormalizerInterface
{
public function getSupportedTypes(?string $format): array

Check failure on line 20 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificImplementedReturnType

src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php:20:57: LessSpecificImplementedReturnType: The inherited return type 'array<string, bool|null>' for Symfony\Component\Serializer\Normalizer\NormalizerInterface::getSupportedTypes is more specific than the implemented return type for Symfony\Component\Lock\Serializer\LockKeyNormalizer::getsupportedtypes 'array<array-key, mixed>' (see https://psalm.dev/166)

Check failure on line 20 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificImplementedReturnType

src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php:20:57: LessSpecificImplementedReturnType: The inherited return type 'array<string, bool|null>' for Symfony\Component\Serializer\Normalizer\DenormalizerInterface::getSupportedTypes is more specific than the implemented return type for Symfony\Component\Lock\Serializer\LockKeyNormalizer::getsupportedtypes 'array<array-key, mixed>' (see https://psalm.dev/166)

Check failure on line 20 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificImplementedReturnType

src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php:20:57: LessSpecificImplementedReturnType: The inherited return type 'array<string, bool|null>' for Symfony\Component\Serializer\Normalizer\NormalizerInterface::getSupportedTypes is more specific than the implemented return type for Symfony\Component\Lock\Serializer\LockKeyNormalizer::getsupportedtypes 'array<array-key, mixed>' (see https://psalm.dev/166)

Check failure on line 20 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

LessSpecificImplementedReturnType

src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php:20:57: LessSpecificImplementedReturnType: The inherited return type 'array<string, bool|null>' for Symfony\Component\Serializer\Normalizer\DenormalizerInterface::getSupportedTypes is more specific than the implemented return type for Symfony\Component\Lock\Serializer\LockKeyNormalizer::getsupportedtypes 'array<array-key, mixed>' (see https://psalm.dev/166)
{
return [Key::class => true];
}

/**
* @param Key $data
*/
public function normalize(mixed $data, ?string $format = null, array $context = []): array

Check failure on line 28 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificImplementedParamType

src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php:28:37: MoreSpecificImplementedParamType: Argument 1 of Symfony\Component\Lock\Serializer\LockKeyNormalizer::normalize has the more specific type 'Symfony\Component\Lock\Key', expecting 'mixed' as defined by Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize (see https://psalm.dev/140)

Check failure on line 28 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificImplementedParamType

src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php:28:37: MoreSpecificImplementedParamType: Argument 1 of Symfony\Component\Lock\Serializer\LockKeyNormalizer::normalize has the more specific type 'Symfony\Component\Lock\Key', expecting 'mixed' as defined by Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize (see https://psalm.dev/140)
{
return (fn () => array_intersect_key(get_object_vars($this), array_flip($this->__sleep())))->bindTo($data, Key::class)();

Check failure on line 30 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php:30:88: UndefinedMethod: Method Symfony\Component\Lock\Serializer\LockKeyNormalizer::__sleep does not exist (see https://psalm.dev/022)

Check failure on line 30 in src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php:30:88: UndefinedMethod: Method Symfony\Component\Lock\Serializer\LockKeyNormalizer::__sleep does not exist (see https://psalm.dev/022)
}

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();
$setter = (fn ($field) => $this->$field = $data[$field])->bindTo($key, Key::class);
foreach ($key->__sleep() as $serializedField) {
$setter($serializedField);
}

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,38 @@
<?php

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);
}
}
3 changes: 2 additions & 1 deletion 3 src/Symfony/Component/Lock/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.