Skip to content
Merged
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 @@ -174,7 +174,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {

$container->register('ux.live_component.deterministic_id_calculator', DeterministicTwigIdCalculator::class);
$container->register('ux.live_component.fingerprint_calculator', FingerprintCalculator::class)
->setArguments(['%kernel.secret%']);
->setArguments([new Reference('serializer'), '%kernel.secret%']);

$container->setAlias(ComponentValidatorInterface::class, ComponentValidator::class);

Expand Down
15 changes: 12 additions & 3 deletions src/LiveComponent/src/Util/FingerprintCalculator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Symfony package.
*
Expand All @@ -11,6 +13,9 @@

namespace Symfony\UX\LiveComponent\Util;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\UX\LiveComponent\LiveComponentHydrator;

/**
* @author Ryan Weaver <[email protected]>
*
Expand All @@ -20,12 +25,16 @@
*/
class FingerprintCalculator
{
public function __construct(private string $secret)
{
public function __construct(
private NormalizerInterface $objectNormalizer,
private string $secret,
) {
}

public function calculateFingerprint(array $data): string
{
return base64_encode(hash_hmac('sha256', serialize($data), $this->secret, true));
$normalizedData = $this->objectNormalizer->normalize($data, context: [LiveComponentHydrator::LIVE_CONTEXT => true]);

return base64_encode(hash_hmac('sha256', serialize($normalizedData), $this->secret, true));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\LiveComponent\Tests\Integration\Util;

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\Entity1;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

use function Zenstruck\Foundry\create;

final class FingerprintCalculatorTest extends KernelTestCase
{
use Factories;
use ResetDatabase;

public function testFingerprintEqual()
{
$fingerprintCalculator = self::getContainer()->get('ux.live_component.fingerprint_calculator');

$entityOne = create(Entity1::class)->object();

$entityTwo = clone $entityOne;

self::assertNotSame(
spl_object_id($entityOne),
spl_object_id($entityTwo)
);

self::assertSame(
$fingerprintCalculator->calculateFingerprint([$entityOne]),
$fingerprintCalculator->calculateFingerprint([$entityTwo])
);
}

public function testFingerprintNotEqual()
{
$fingerprintCalculator = self::getContainer()->get('ux.live_component.fingerprint_calculator');

$entityOne = create(Entity1::class)->object();

$entityTwo = create(Entity1::class)->object();

self::assertNotSame(
$fingerprintCalculator->calculateFingerprint([$entityOne]),
$fingerprintCalculator->calculateFingerprint([$entityTwo])
);
}
}
4 changes: 2 additions & 2 deletions src/TwigComponent/src/Attribute/PostMount.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace Symfony\UX\TwigComponent\Attribute;

/*
* This file is part of the Symfony package.
*
Expand All @@ -11,6 +9,8 @@
* file that was distributed with this source code.
*/

namespace Symfony\UX\TwigComponent\Attribute;

/**
* @author Kevin Bond <[email protected]>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/TwigComponent/src/Attribute/PreMount.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace Symfony\UX\TwigComponent\Attribute;

/*
* This file is part of the Symfony package.
*
Expand All @@ -11,6 +9,8 @@
* file that was distributed with this source code.
*/

namespace Symfony\UX\TwigComponent\Attribute;

/**
* @author Kevin Bond <[email protected]>
*/
Expand Down
9 changes: 9 additions & 0 deletions src/TwigComponent/src/Twig/ComponentNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\TwigComponent\Twig;

use Twig\Compiler;
Expand Down
9 changes: 9 additions & 0 deletions src/TwigComponent/src/Twig/ComponentTokenParser.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\TwigComponent\Twig;

use Symfony\UX\TwigComponent\ComponentFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\UX\TwigComponent\Tests\Fixtures\Component;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Tests\Fixtures\Service\ServiceA;

#[AsTwigComponent('parent_form_component')]
final class ParentFormComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\UX\TwigComponent\Tests\Fixtures\Component;

use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\Tests\Fixtures\Service\ServiceA;

#[AsTwigComponent('textarea_component')]
final class TextareaComponent
Expand Down