Skip to content

Commit

Permalink
Transformer use Symfony Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanmac committed Aug 6, 2022
1 parent 3bd113a commit ba3e240
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 118 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@
"symfony/http-kernel": "^4.4 || ^5.4 || ^6.0",
"symfony/options-resolver": "^4.4 || ^5.4 || ^6.0",
"symfony/process": "^4.4 || ^5.4 || ^6.0",
"symfony/property-access": "^4.4 || ^5.4 || ^6.0",
"symfony/property-info": "^4.4 || ^5.4 || ^6.0",
"symfony/routing": "^4.4 || ^5.4 || ^6.0",
"symfony/security-core": "^4.4 || ^5.4 || ^6.0",
"symfony/security-http": "^4.4 || ^5.4 || ^6.0",
"symfony/serializer": "^4.4 || ^5.4 || ^6.0",
"symfony/validator": "^4.4 || ^5.4 || ^6.0",
"twig/string-extra": "^3.0",
"twig/twig": "^2.12.1 || ^3.0"
Expand Down
157 changes: 53 additions & 104 deletions src/Entity/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
use Sonata\PageBundle\Model\SnapshotInterface;
use Sonata\PageBundle\Model\SnapshotManagerInterface;
use Sonata\PageBundle\Model\TransformerInterface;
use Sonata\PageBundle\Serializer\InterfaceDenormalizer;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;

/**
* This class transform a SnapshotInterface into PageInterface.
Expand All @@ -51,18 +57,26 @@ final class Transformer implements TransformerInterface
private ManagerRegistry $registry;

/**
* @param ManagerInterface<PageBlockInterface> $blockManager
* @var SerializerInterface&NormalizerInterface&DenormalizerInterface
*/
private $serializer;

/**
* @param ManagerInterface<PageBlockInterface> $blockManager
* @param SerializerInterface&NormalizerInterface&DenormalizerInterface $serializer
*/
public function __construct(
SnapshotManagerInterface $snapshotManager,
PageManagerInterface $pageManager,
ManagerInterface $blockManager,
ManagerRegistry $registry
ManagerRegistry $registry,
SerializerInterface $serializer
) {
$this->snapshotManager = $snapshotManager;
$this->pageManager = $pageManager;
$this->blockManager = $blockManager;
$this->registry = $registry;
$this->serializer = $serializer;
}

public function create(PageInterface $page)
Expand All @@ -89,33 +103,22 @@ public function create(PageInterface $page)
$snapshot->setParentId($page->getParent()->getId());
}

$blocks = [];
foreach ($page->getBlocks() as $block) {
if (null !== $block->getParent()) { // ignore block with a parent => must be a child of a main
continue;
}

$blocks[] = $this->createBlock($block);
}

$snapshot->setContent([
'id' => $page->getId(),
'name' => $page->getName(),
'javascript' => $page->getJavascript(),
'stylesheet' => $page->getStylesheet(),
'raw_headers' => $page->getRawHeaders(),
'title' => $page->getTitle(),
'meta_description' => $page->getMetaDescription(),
'meta_keyword' => $page->getMetaKeyword(),
'template_code' => $page->getTemplateCode(),
'request_method' => $page->getRequestMethod(),
'created_at' => null !== $page->getCreatedAt() ? (int) $page->getCreatedAt()->format('U') : null,
'updated_at' => null !== $page->getUpdatedAt() ? (int) $page->getUpdatedAt()->format('U') : null,
'slug' => $page->getSlug(),
'parent_id' => null !== $page->getParent() ? $page->getParent()->getId() : null,
'blocks' => $blocks,
/**
* @var PageContent $content
*/
$content = $this->serializer->normalize($page, null, [
DateTimeNormalizer::FORMAT_KEY => 'U',
AbstractNormalizer::GROUPS => ['page_transformer'],
AbstractNormalizer::CALLBACKS => [
'blocks' => static fn (Collection $innerObject, PageInterface $outerObject, string $attributeName, ?string $format = null, array $context = []) => $innerObject->filter(static fn (BlockInterface $block) => !$block->hasParent()),
'parent' => static fn ($innerObject, $outerObject, string $attributeName, ?string $format = null, array $context = []) => $innerObject instanceof PageInterface ? $innerObject->getId() : $innerObject,
// remove NEXT_MAYOR
'target' => static fn ($innerObject, $outerObject, string $attributeName, ?string $format = null, array $context = []) => $innerObject instanceof PageInterface ? $innerObject->getId() : $innerObject,
],
]);

$snapshot->setContent($content);

return $snapshot;
}

Expand All @@ -135,27 +138,19 @@ public function load(SnapshotInterface $snapshot)

$content = $snapshot->getContent();

if (null !== $content) {
$page->setId($content['id']);
$page->setJavascript($content['javascript']);
$page->setStylesheet($content['stylesheet']);
$page->setRawHeaders($content['raw_headers']);
$page->setTitle($content['title'] ?? null);
$page->setMetaDescription($content['meta_description']);
$page->setMetaKeyword($content['meta_keyword']);
$page->setName($content['name']);
$page->setSlug($content['slug']);
$page->setTemplateCode($content['template_code']);
$page->setRequestMethod($content['request_method']);

$createdAt = new \DateTime();
$createdAt->setTimestamp((int) $content['created_at']);
$page->setCreatedAt($createdAt);

$updatedAt = new \DateTime();
$updatedAt->setTimestamp((int) $content['updated_at']);
$page->setUpdatedAt($updatedAt);
}
$pageClass = $this->pageManager->getClass();
$blockClass = $this->blockManager->getClass();

$this->serializer->denormalize($content, $pageClass, null, [
DateTimeNormalizer::FORMAT_KEY => 'U',
AbstractNormalizer::GROUPS => ['page_transformer'],
AbstractNormalizer::OBJECT_TO_POPULATE => $page,
InterfaceDenormalizer::SUPPORTED_INTERFACES_KEY => [
PageInterface::class => $pageClass,
BlockInterface::class => $blockClass,
PageBlockInterface::class => $blockClass,
],
]);

return $page;
}
Expand All @@ -166,37 +161,17 @@ public function loadBlock(array $content, PageInterface $page)

$block->setPage($page);

if (isset($content['id'])) {
$block->setId($content['id']);
}

if (isset($content['name'])) {
$block->setName($content['name']);
}

$block->setEnabled($content['enabled']);

if (isset($content['position'])) {
$block->setPosition($content['position']);
}
$blockClass = $this->blockManager->getClass();

$block->setSettings($content['settings']);

if (isset($content['type'])) {
$block->setType($content['type']);
}

$createdAt = new \DateTime();
$createdAt->setTimestamp((int) $content['created_at']);
$block->setCreatedAt($createdAt);

$updatedAt = new \DateTime();
$updatedAt->setTimestamp((int) $content['updated_at']);
$block->setUpdatedAt($updatedAt);

foreach ($content['blocks'] as $child) {
$block->addChild($this->loadBlock($child, $page));
}
$this->serializer->denormalize($content, $blockClass, null, [
DateTimeNormalizer::FORMAT_KEY => 'U',
AbstractNormalizer::GROUPS => ['page_transformer'],
AbstractNormalizer::OBJECT_TO_POPULATE => $block,
InterfaceDenormalizer::SUPPORTED_INTERFACES_KEY => [
BlockInterface::class => $blockClass,
PageBlockInterface::class => $blockClass,
],
]);

return $block;
}
Expand Down Expand Up @@ -241,30 +216,4 @@ public function getChildren(PageInterface $page)

return $this->children[$page->getId()];
}

/**
* @return array<string, mixed>
*
* @phpstan-return BlockContent
*/
private function createBlock(BlockInterface $block)
{
$childBlocks = [];

foreach ($block->getChildren() as $child) {
$childBlocks[] = $this->createBlock($child);
}

return [
'id' => $block->getId(),
'name' => $block->getName(),
'enabled' => $block->getEnabled(),
'position' => $block->getPosition(),
'settings' => $block->getSettings(),
'type' => $block->getType(),
'created_at' => null !== $block->getCreatedAt() ? (int) $block->getCreatedAt()->format('U') : null,
'updated_at' => null !== $block->getUpdatedAt() ? (int) $block->getUpdatedAt()->format('U') : null,
'blocks' => $childBlocks,
];
}
}
12 changes: 6 additions & 6 deletions src/Model/TransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
* position: int|null,
* settings: array<string, mixed>,
* type: string|null,
* created_at: int|null,
* updated_at: int|null,
* created_at: int|string|null,
* updated_at: int|string|null,
* parent_id?: int|string|null,
* blocks: array<array{
* id: int|string|null,
Expand All @@ -35,8 +35,8 @@
* position: int|null,
* settings: array<string, mixed>,
* type: string|null,
* created_at: int|null,
* updated_at: int|null,
* created_at: int|string|null,
* updated_at: int|string|null,
* blocks: array<mixed>,
* }>,
* }
Expand All @@ -54,8 +54,8 @@
* slug: string|null,
* template_code: string|null,
* request_method: string|null,
* created_at: int|null,
* updated_at: int|null,
* created_at: int|string|null,
* updated_at: int|string|null,
* blocks: array<BlockContent>,
* }
*/
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<argument type="service" id="sonata.page.manager.page"/>
<argument type="service" id="sonata.page.manager.block"/>
<argument type="service" id="doctrine"/>
<argument type="service" id="serializer"/>
</service>
</services>
</container>
44 changes: 44 additions & 0 deletions src/Resources/config/serialization/Model.Block.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping">
<class name="Sonata\PageBundle\Model\Block">
<attribute name="id" serialized-name="id">
<group>page_transformer</group>
</attribute>
<attribute name="name" serialized-name="name">
<group>page_transformer</group>
</attribute>
<attribute name="enabled" serialized-name="enabled">
<group>page_transformer</group>
</attribute>
<attribute name="position" serialized-name="position">
<group>page_transformer</group>
</attribute>
<attribute name="settings" serialized-name="settings">
<group>page_transformer</group>
</attribute>
<attribute name="type" serialized-name="type">
<group>page_transformer</group>
</attribute>
<attribute name="createdAt" serialized-name="created_at">
<group>page_transformer</group>
</attribute>
<attribute name="updatedAt" serialized-name="updated_at">
<group>page_transformer</group>
</attribute>
<attribute name="children" serialized-name="blocks">
<group>page_transformer</group>
</attribute>
<attribute name="parents">
<group>ignore</group>
</attribute>
<attribute name="page">
<group>ignore</group>
</attribute>
<attribute name="parent">
<group>ignore</group>
</attribute>
<attribute name="ttl">
<group>ignore</group>
</attribute>
</class>
</serializer>
65 changes: 65 additions & 0 deletions src/Resources/config/serialization/Model.Page.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<serializer xmlns="http://symfony.com/schema/dic/serializer-mapping">
<class name="Sonata\PageBundle\Model\Page">
<attribute name="id" serialized-name="id">
<group>page_transformer</group>
</attribute>
<attribute name="name" serialized-name="name">
<group>page_transformer</group>
</attribute>
<attribute name="javascript" serialized-name="javascript">
<group>page_transformer</group>
</attribute>
<attribute name="stylesheet" serialized-name="stylesheet">
<group>page_transformer</group>
</attribute>
<attribute name="rawHeaders" serialized-name="raw_headers">
<group>page_transformer</group>
</attribute>
<attribute name="title" serialized-name="title">
<group>page_transformer</group>
</attribute>
<attribute name="metaDescription" serialized-name="meta_description">
<group>page_transformer</group>
</attribute>
<attribute name="metaKeyword" serialized-name="meta_keyword">
<group>page_transformer</group>
</attribute>
<attribute name="templateCode" serialized-name="template_code">
<group>page_transformer</group>
</attribute>
<attribute name="requestMethod" serialized-name="request_method">
<group>page_transformer</group>
</attribute>
<attribute name="createdAt" serialized-name="created_at">
<group>page_transformer</group>
</attribute>
<attribute name="updatedAt" serialized-name="updated_at">
<group>page_transformer</group>
</attribute>
<attribute name="slug" serialized-name="slug">
<group>page_transformer</group>
</attribute>
<attribute name="parent" serialized-name="parent_id">
<group>page_transformer</group>
</attribute>
<attribute name="blocks" serialized-name="blocks">
<group>page_transformer</group>
</attribute>
<attribute name="routeName">
<group>ignore</group>
</attribute>
<attribute name="pageAlias">
<group>ignore</group>
</attribute>
<attribute name="customUrl">
<group>ignore</group>
</attribute>
<attribute name="site">
<group>ignore</group>
</attribute>
<attribute name="edited">
<group>ignore</group>
</attribute>
</class>
</serializer>

0 comments on commit ba3e240

Please sign in to comment.