Skip to content
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

EZP-31677: Added normalizer to decrese amount of data when compound matcher is encoded to JSON #3041

Merged
merged 1 commit into from
Jun 8, 2020
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
5 changes: 5 additions & 0 deletions eZ/Bundle/EzPublishCoreBundle/Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ services:
tags:
- { name: kernel.event_subscriber }

ezpublish.serializer.compound_matcher_normalizer:
class: eZ\Publish\Core\MVC\Symfony\Component\Serializer\CompoundMatcherNormalizer
tags:
- { name: serializer.normalizer }

ezpublish.url_generator.base:
class: "%ezpublish.url_generator.base.class%"
abstract: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\MVC\Symfony\Component\Serializer;
mikadamczyk marked this conversation as resolved.
Show resolved Hide resolved

use eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher;
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;

class CompoundMatcherNormalizer extends PropertyNormalizer
{
public function normalize($object, $format = null, array $context = [])
{
$data = parent::normalize($object, $format, $context);
$data['config'] = [];
$data['matchersMap'] = [];

return $data;
}

public function supportsNormalization($data, $format = null)
{
return $data instanceof Matcher\Compound;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ trait SerializerTrait
public function getSerializer()
{
return new Serializer(
[(new PropertyNormalizer())->setIgnoredAttributes(['request', 'container', 'matcherBuilder'])],
[
new CompoundMatcherNormalizer(),
(new PropertyNormalizer())->setIgnoredAttributes(['request', 'container', 'matcherBuilder']),
],
[new JsonEncoder()]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ class MatcherSerializationTest extends TestCase
use SerializerTrait;

/**
* @param \eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher|null $expected
*
* @dataProvider matcherProvider
*/
public function testDeserialize(Matcher $matcher)
public function testDeserialize(Matcher $matcher, $expected = null)
{
$serializedMatcher = $this->serializeMatcher($matcher);
$unserializedMatcher = $this->deserializeMatcher($serializedMatcher, get_class($matcher));
$expected = $expected === null ? $matcher : $expected;

$this->assertEquals($matcher, $unserializedMatcher);
$this->assertEquals($expected, $unserializedMatcher);
}

/**
* @param \eZ\Publish\Core\MVC\Symfony\SiteAccess\Matcher $matcher
*
* @return string
*/
private function serializeMatcher(Matcher $matcher)
Expand Down Expand Up @@ -55,6 +56,36 @@ private function deserializeMatcher($serializedMatcher, $matcherFQCN)

public function matcherProvider()
{
$subMatchers = [
'Map\URI' => [
'map' => ['key' => 'value'],
],
'Map\Host' => [
'map' => ['key' => 'value'],
],
];
$logicalAnd = new Matcher\Compound\LogicalAnd(
[
[
'match' => 'site_access_name',
],
]
);
$logicalAnd->setSubMatchers($subMatchers);
$expectedLogicalAnd = new Matcher\Compound\LogicalAnd([]);
$expectedLogicalAnd->setSubMatchers($subMatchers);

$logicalOr = new Matcher\Compound\LogicalOr(
[
[
'match' => 'site_access_name',
],
]
);
$logicalOr->setSubMatchers($subMatchers);
$expectedLogicalOr = new Matcher\Compound\LogicalOr([]);
$expectedLogicalOr->setSubMatchers($subMatchers);

return [
'URIText' => [
new Matcher\URIText([
Expand Down Expand Up @@ -106,38 +137,12 @@ public function matcherProvider()
]),
],
'CompoundAnd' => [
new Matcher\Compound\LogicalAnd(
[
[
'matchers' => [
'Map\URI' => [
'map' => ['key' => 'value'],
],
'Map\Host' => [
'map' => ['key' => 'value'],
],
],
'match' => 'site_access_name',
],
]
),
$logicalAnd,
$expectedLogicalAnd,
],
'CompoundOr' => [
new Matcher\Compound\LogicalOr(
[
[
'matchers' => [
'Map\URI' => [
'map' => ['key' => 'value'],
],
'Map\Host' => [
'map' => ['key' => 'value'],
],
],
'match' => 'site_access_name',
],
]
),
$logicalOr,
$expectedLogicalOr,
],
];
}
Expand Down