Skip to content

Commit 4c44d3c

Browse files
author
Joan He
committed
MAGETWO-66318: Remove usages of serialize/unserialize in CmsSampleData
- address review comments
1 parent 49761eb commit 4c44d3c

File tree

8 files changed

+150
-86
lines changed

8 files changed

+150
-86
lines changed

app/code/Magento/Cms/Setup/ContentConverter.php

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Cms\Setup;
77

8+
use Magento\Widget\Model\Widget\Wysiwyg\Normalizer;
89
use Magento\Framework\DB\DataConverter\DataConversionException;
910
use Magento\Framework\DB\DataConverter\SerializedToJson;
1011
use Magento\Framework\Serialize\Serializer\Json;
@@ -21,19 +22,27 @@ class ContentConverter extends SerializedToJson
2122
*/
2223
private $parameterFactory;
2324

25+
/**
26+
* @var Normalizer
27+
*/
28+
private $normalizer;
29+
2430
/**
2531
* ContentConverter constructor
2632
*
2733
* @param Serialize $serialize
2834
* @param Json $json
2935
* @param \Magento\Framework\Filter\Template\Tokenizer\ParameterFactory $parameterFactory
36+
* @param Normalizer $normalizer
3037
*/
3138
public function __construct(
3239
Serialize $serialize,
3340
Json $json,
34-
\Magento\Framework\Filter\Template\Tokenizer\ParameterFactory $parameterFactory
41+
\Magento\Framework\Filter\Template\Tokenizer\ParameterFactory $parameterFactory,
42+
Normalizer $normalizer
3543
) {
3644
$this->parameterFactory = $parameterFactory;
45+
$this->normalizer = $normalizer;
3746
parent::__construct($serialize, $json);
3847
}
3948

@@ -46,13 +55,13 @@ public function __construct(
4655
*/
4756
public function convert($value)
4857
{
49-
preg_match_all('/(.*?){{(widget)(.*?)}}(.*?)/si', $value, $matches, PREG_SET_ORDER);
58+
preg_match_all('/(.*?){{widget(.*?)}}/si', $value, $matches, PREG_SET_ORDER);
5059
if (empty($matches)) {
5160
return $value;
5261
}
5362
$convertedValue = '';
5463
foreach ($matches as $match) {
55-
$convertedValue .= $this->convertMatchString($match);
64+
$convertedValue .= $match[1] . '{{widget' . $this->convertWidgetParams($match[2]) . '}}';
5665
}
5766
preg_match_all('/(.*?{{widget.*?}})*(?<ending>.*?)$/si', $value, $matchesTwo, PREG_SET_ORDER);
5867
if (isset($matchesTwo[0])) {
@@ -70,51 +79,37 @@ public function convert($value)
7079
*/
7180
private function isConvertedConditions($value)
7281
{
73-
// Restore WYSIWYG reserved characters in string
74-
$value = str_replace(
75-
array_values(Conditions::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
76-
array_keys(Conditions::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
77-
$value
78-
);
79-
return $this->isValidJsonValue($value);
82+
return $this->isValidJsonValue($this->normalizer->restoreReservedCharaters($value));
8083
}
8184

8285
/**
83-
* Convert matching string
86+
* Convert widget parameters from serialized format to JSON format
8487
*
85-
* @param array $matchSegments
88+
* @param string $paramsString
8689
* @return string
8790
*/
88-
private function convertMatchString(array $matchSegments)
91+
private function convertWidgetParams($paramsString)
8992
{
9093
/** @var \Magento\Framework\Filter\Template\Tokenizer\Parameter $tokenizer */
9194
$tokenizer = $this->parameterFactory->create();
92-
$tokenizer->setString($matchSegments[3]);
95+
$tokenizer->setString($paramsString);
9396
$widgetParameters = $tokenizer->tokenize();
9497
if (isset($widgetParameters['conditions_encoded'])) {
9598
if ($this->isConvertedConditions($widgetParameters['conditions_encoded'])) {
96-
return $matchSegments[0];
99+
return $paramsString;
97100
}
98101
// Restore WYSIWYG reserved characters in string
99-
$conditions = str_replace(
100-
array_values(Conditions::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
101-
array_keys(Conditions::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
102-
$widgetParameters['conditions_encoded']
103-
);
102+
$conditions = $this->normalizer->restoreReservedCharaters($widgetParameters['conditions_encoded']);
104103
$conditions = parent::convert($conditions);
105104
// Replace WYSIWYG reserved characters in string
106-
$widgetParameters['conditions_encoded'] = str_replace(
107-
array_keys(Conditions::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
108-
array_values(Conditions::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
109-
$conditions
110-
);
111-
$matchSegments[3] = '';
105+
$widgetParameters['conditions_encoded'] = $this->normalizer->replaceReservedCharaters($conditions);
106+
$newParamsString = '';
112107
foreach ($widgetParameters as $key => $parameter) {
113-
$matchSegments[3] .= ' ' . $key . '="' . $parameter . '"';
108+
$newParamsString .= ' ' . $key . '="' . $parameter . '"';
114109
}
115-
return $matchSegments[1] . '{{' . $matchSegments[2] . $matchSegments[3] . '}}' . $matchSegments[4];
110+
return $newParamsString;
116111
} else {
117-
return $matchSegments[0];
112+
return $paramsString;
118113
}
119114
}
120115
}

app/code/Magento/Cms/Setup/UpgradeData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
7272
$this->upgradeVersionTwoZeroOne();
7373
}
7474
if (version_compare($context->getVersion(), '2.0.2', '<')) {
75-
$this->upgradeVersionTwoZeroTwo($setup);
75+
$this->convertWidgetConditionsToJson($setup);
7676
}
7777
}
7878

@@ -82,7 +82,7 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
8282
* @param ModuleDataSetupInterface $setup
8383
* @return void
8484
*/
85-
private function upgradeVersionTwoZeroTwo(ModuleDataSetupInterface $setup)
85+
private function convertWidgetConditionsToJson(ModuleDataSetupInterface $setup)
8686
{
8787
$fieldDataConverter = $this->fieldDataConverterFactory->create(
8888
\Magento\Cms\Setup\ContentConverter::class

app/code/Magento/Widget/Helper/Conditions.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Widget\Helper;
77

8+
use Magento\Widget\Model\Widget\Wysiwyg\Normalizer;
89
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\Serialize\Serializer\Json;
1011

@@ -13,27 +14,25 @@
1314
*/
1415
class Conditions
1516
{
16-
const WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP = [
17-
'{' => '[',
18-
'}' => ']',
19-
'"' => '`',
20-
'\\' => '|',
21-
];
22-
2317
/**
24-
* Instance of serializer interface.
25-
*
2618
* @var Json
2719
*/
2820
private $serializer;
2921

22+
/**
23+
* @var Normalizer
24+
*/
25+
private $normalizer;
26+
3027
/**
3128
* @param Json $serializer
3229
*/
3330
public function __construct(
34-
Json $serializer = null
31+
Json $serializer = null,
32+
Normalizer $normalizer = null
3533
) {
3634
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
35+
$this->normalizer = $normalizer ?: ObjectManager::getInstance()->get(Normalizer::class);
3736
}
3837

3938
/**
@@ -44,12 +43,7 @@ public function __construct(
4443
*/
4544
public function encode(array $value)
4645
{
47-
$value = str_replace(
48-
array_keys(self::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
49-
array_values(self::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
50-
$this->serializer->serialize($value)
51-
);
52-
return $value;
46+
return $this->normalizer->replaceReservedCharaters($this->serializer->serialize($value));
5347
}
5448

5549
/**
@@ -60,12 +54,8 @@ public function encode(array $value)
6054
*/
6155
public function decode($value)
6256
{
63-
$value = str_replace(
64-
array_values(self::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
65-
array_keys(self::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
66-
$value
57+
return $this->serializer->unserialize(
58+
$this->normalizer->restoreReservedCharaters($value)
6759
);
68-
$value = $this->serializer->unserialize($value);
69-
return $value;
7060
}
7161
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Widget\Model\Widget\Wysiwyg;
7+
8+
class Normalizer
9+
{
10+
11+
const WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP = [
12+
'{' => '[',
13+
'}' => ']',
14+
'"' => '`',
15+
'\\' => '|',
16+
];
17+
18+
/**
19+
* Replace the reserved characters in the content
20+
*
21+
* @param string $content
22+
* @return string
23+
*/
24+
public function replaceReservedCharaters($content)
25+
{
26+
return str_replace(
27+
array_keys(Normalizer::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
28+
array_values(Normalizer::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
29+
$content
30+
);
31+
}
32+
33+
/**
34+
* Restore the reserved characters in the content
35+
*
36+
* @param string $content
37+
* @return string
38+
*/
39+
public function restoreReservedCharaters($content)
40+
{
41+
return str_replace(
42+
array_values(Normalizer::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
43+
array_keys(Normalizer::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
44+
$content
45+
);
46+
}
47+
}

app/code/Magento/Widget/Setup/LayoutUpdateConverter.php

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,31 @@
55
*/
66
namespace Magento\Widget\Setup;
77

8+
use Magento\Framework\Serialize\Serializer\Json;
9+
use Magento\Framework\Serialize\Serializer\Serialize;
10+
use Magento\Widget\Model\Widget\Wysiwyg\Normalizer;
811
use Magento\Framework\DB\DataConverter\DataConversionException;
912
use Magento\Framework\DB\DataConverter\SerializedToJson;
10-
use Magento\Widget\Helper\Conditions;
1113

1214
/**
1315
* Convert conditions_encoded part of layout update data from serialized to JSON format
1416
*/
1517
class LayoutUpdateConverter extends SerializedToJson
1618
{
19+
/**
20+
* @var Normalizer
21+
*/
22+
private $normalizer;
23+
24+
public function __construct(
25+
Serialize $serialize,
26+
Json $json,
27+
Normalizer $normalizer
28+
) {
29+
$this->normalizer = $normalizer;
30+
parent::__construct($serialize, $json);
31+
}
32+
1733
/**
1834
* Convert conditions_encoded part of layout update data from serialized to JSON format
1935
*
@@ -30,33 +46,13 @@ public function convert($value)
3046
PREG_SET_ORDER
3147
);
3248
if (isset($matches[0])) {
33-
return $this->convertMatchString($matches[0]);
49+
$matchSegments = $matches[0];
50+
$matchSegments[2] = $this->normalizer->replaceReservedCharaters(
51+
parent::convert($this->normalizer->restoreReservedCharaters($matchSegments[2]))
52+
);
53+
return $matchSegments[1] . $matchSegments[2] . $matchSegments[3];
3454
} else {
3555
return $value;
3656
}
3757
}
38-
39-
/**
40-
* Convert matching string
41-
*
42-
* @param array $matchSegments
43-
* @return string
44-
*/
45-
private function convertMatchString(array $matchSegments)
46-
{
47-
// Restore WYSIWYG reserved characters in string
48-
$value = str_replace(
49-
array_values(Conditions::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
50-
array_keys(Conditions::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
51-
$matchSegments[2]
52-
);
53-
$convertedValue = parent::convert($value);
54-
// Replace WYSIWYG reserved characters in string
55-
$matchSegments[2] = str_replace(
56-
array_keys(Conditions::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
57-
array_values(Conditions::WYSIWYG_RESERVED_CHARCTERS_REPLACEMENT_MAP),
58-
$convertedValue
59-
);
60-
return $matchSegments[1] . $matchSegments[2] . $matchSegments[3];
61-
}
6258
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Widget\Model\Widget\Wysiwyg;
7+
8+
class NormalizerTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Widget\Model\Widget\Wysiwyg\Normalizer
12+
*/
13+
protected $normalizer;
14+
15+
protected function setUp()
16+
{
17+
$this->normalizer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
18+
\Magento\Widget\Model\Widget\Wysiwyg\Normalizer::class
19+
);
20+
}
21+
22+
public function testReplaceReservedCharaters()
23+
{
24+
$content = '{}\\""';
25+
$expected = '[]|``';
26+
$this->assertEquals($expected, $this->normalizer->replaceReservedCharaters($content));
27+
}
28+
29+
public function testRestoreReservedCharaters()
30+
{
31+
$content = '[]|``';
32+
$expected = '{}\\""';
33+
$this->assertEquals($expected, $this->normalizer->restoreReservedCharaters($content));
34+
}
35+
}

lib/internal/Magento/Framework/DB/Select/LikeQueryModifier.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class LikeQueryModifier implements QueryModifierInterface
2020
/**
2121
* Constructor
2222
*
23-
* @param array $values
23+
* @param array $values array of field and pattern pairs of Like Clause,
24+
* for example: [<field1> => <pattern1>, <field2> => <pattern2>, ...]
2425
*/
2526
public function __construct(
2627
$values = []
@@ -33,8 +34,8 @@ public function __construct(
3334
*/
3435
public function modify(Select $select)
3536
{
36-
foreach ($this->values as $field => $values) {
37-
$select->where($field . ' LIKE (?)', $values);
37+
foreach ($this->values as $field => $pattern) {
38+
$select->where($field . ' LIKE (?)', $pattern);
3839
}
3940
}
4041
}

0 commit comments

Comments
 (0)