Skip to content

[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.4
Choose a base branch
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 @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions 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])
;
};
5 changes: 5 additions & 0 deletions src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add `LockKeyNormalizer`

7.3
---

Expand Down
59 changes: 59 additions & 0 deletions src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php
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
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, done

{
public function getSupportedTypes(?string $format): array

Check failure on line 25 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:25: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 25 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:25: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 25 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:25: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 25 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:25: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)
{
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

View workflow job for this annotation

GitHub Actions / Psalm

MoreSpecificImplementedParamType

src/Symfony/Component/Lock/Serializer/LockKeyNormalizer.php:33: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 33 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:33: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 35 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:35:88: UndefinedMethod: Method Symfony\Component\Lock\Serializer\LockKeyNormalizer::__sleep does not exist (see https://psalm.dev/022)

Check failure on line 35 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:35: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();
(function () use ($data) {
foreach ($this->__sleep() as $serializedField) {

Check failure on line 47 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:47:29: UndefinedMethod: Method Symfony\Component\Lock\Serializer\LockKeyNormalizer::__sleep does not exist (see https://psalm.dev/022)

Check failure on line 47 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:47:29: UndefinedMethod: Method Symfony\Component\Lock\Serializer\LockKeyNormalizer::__sleep does not exist (see https://psalm.dev/022)
$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);
}
}
3 changes: 2 additions & 1 deletion 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
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