Skip to content

Commit

Permalink
EZP-31677: Added normalizer to decrease amount of data when compound …
Browse files Browse the repository at this point in the history
…matcher is encoded to JSON (#3041)
  • Loading branch information
mikadamczyk authored Jun 8, 2020
1 parent f816402 commit c0efbbe
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 35 deletions.
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;

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

0 comments on commit c0efbbe

Please sign in to comment.