Skip to content

Commit

Permalink
TASK: Remove hard dependency from fusion cache serialization to flows…
Browse files Browse the repository at this point in the history
… property mapping
  • Loading branch information
mhsdesign committed Feb 3, 2024
1 parent 07ffe14 commit 76a72a8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
53 changes: 53 additions & 0 deletions Neos.Fusion/Classes/Core/Cache/FusionContextSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Neos\Fusion\Core\Cache;

use Neos\Flow\Property\PropertyMapper;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* Serializer for Fusion's [at]cache.context values
*
* Uses the flow property mapper as implementation.
* It relies on a converter being available from the context value type to string and reverse.
*
* {@see RuntimeContentCache::serializeContext()}
* {@see RuntimeContentCache::unserializeContext()}
*
* @internal
*/
final class FusionContextSerializer implements NormalizerInterface, DenormalizerInterface
{
public function __construct(
private readonly PropertyMapper $propertyMapper
) {
}

public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
{
return $this->propertyMapper->convert($data, $type);
}

public function normalize(mixed $object, string $format = null, array $context = [])
{
return $this->propertyMapper->convert($object, 'string');
}

public function supportsDenormalization(mixed $data, string $type, string $format = null)
{
throw new \BadMethodCallException(sprintf('Method %s is not supported.', __METHOD__), 1706978912);
}

public function supportsNormalization(mixed $data, string $format = null)
{
throw new \BadMethodCallException(sprintf('Method %s is not supported.', __METHOD__), 1706978913);
}

public function getSupportedTypes(?string $format): array
{
throw new \BadMethodCallException(sprintf('Method %s is not supported.', __METHOD__), 1706978914);
}
}
22 changes: 12 additions & 10 deletions Neos.Fusion/Classes/Core/Cache/RuntimeContentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Neos\Utility\Unicode\Functions;
use Neos\Fusion\Core\Runtime;
use Neos\Fusion\Exception;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* Integrate the ContentCache into the Fusion Runtime
Expand Down Expand Up @@ -62,11 +64,7 @@ class RuntimeContentCache
*/
protected $tags = [];

/**
* @var \Neos\Flow\Property\PropertyMapper
* @Flow\Inject
*/
protected $propertyMapper;
private NormalizerInterface&DenormalizerInterface $serializer;

/**
* @param Runtime $runtime
Expand All @@ -76,6 +74,11 @@ public function __construct(Runtime $runtime)
$this->runtime = $runtime;
}

public function injectSerializer(NormalizerInterface&DenormalizerInterface $serializer)
{
$this->serializer = $serializer;
}

/**
* Adds a tag built from the given key and value.
*
Expand Down Expand Up @@ -368,7 +371,7 @@ protected function buildCacheTags(array $configuration, $fusionPath, $fusionObje
}

/**
* Encodes an array of context variables to its serialized representation via flows property mapping.
* Encodes an array of context variables to its serialized representation
* {@see self::unserializeContext()}
*
* @param array<string, mixed> $contextVariables
Expand All @@ -378,18 +381,17 @@ protected function serializeContext(array $contextVariables): array
{
$serializedContextArray = [];
foreach ($contextVariables as $variableName => $contextValue) {
// TODO This relies on a converter being available from the context value type to string
if ($contextValue !== null) {
$serializedContextArray[$variableName]['type'] = TypeHandling::getTypeForValue($contextValue);
$serializedContextArray[$variableName]['value'] = $this->propertyMapper->convert($contextValue, 'string');
$serializedContextArray[$variableName]['value'] = $this->serializer->normalize($contextValue);
}
}

return $serializedContextArray;
}

/**
* Decodes and serialized array of context variables to its original values via flows property mapping.
* Decodes and serialized array of context variables to its original values
* {@see self::serializeContext()}
*
* @param array<string, array{type: string, value: string}> $contextArray
Expand All @@ -399,7 +401,7 @@ protected function unserializeContext(array $contextArray): array
{
$unserializedContext = [];
foreach ($contextArray as $variableName => $typeAndValue) {
$value = $this->propertyMapper->convert($typeAndValue['value'], $typeAndValue['type']);
$value = $this->serializer->denormalize($typeAndValue['value'], $typeAndValue['type']);
$unserializedContext[$variableName] = $value;
}

Expand Down
5 changes: 5 additions & 0 deletions Neos.Fusion/Configuration/Objects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ Neos\Fusion\Core\Cache\ParserCache:
arguments:
1:
value: Neos_Fusion_ParsePartials

Neos\Fusion\Core\Cache\RuntimeContentCache:
properties:
serializer:
object: Neos\Fusion\Core\Cache\FusionContextSerializer

0 comments on commit 76a72a8

Please sign in to comment.