Skip to content

Commit 5a39814

Browse files
committed
Merge branch 'MAGETWO-63350' of github.com:magento-troll/magento2ce into MAGETWO-58456
2 parents 5b356d5 + a9e1a0e commit 5a39814

File tree

10 files changed

+376
-48
lines changed

10 files changed

+376
-48
lines changed

app/code/Magento/SalesRule/Model/Converter/ToDataModel.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\SalesRule\Api\Data\RuleInterface;
1010
use Magento\SalesRule\Model\Data\Rule as RuleDataModel;
1111
use Magento\SalesRule\Model\Rule;
12+
use Magento\Framework\Serialize\Serializer\Json;
1213

1314
class ToDataModel
1415
{
@@ -37,25 +38,33 @@ class ToDataModel
3738
*/
3839
protected $ruleLabelFactory;
3940

41+
/**
42+
* @var Json $serializer
43+
*/
44+
private $serializer;
45+
4046
/**
4147
* @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
4248
* @param \Magento\SalesRule\Api\Data\RuleInterfaceFactory $ruleDataFactory
4349
* @param \Magento\SalesRule\Api\Data\ConditionInterfaceFactory $conditionDataFactory
4450
* @param \Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory $ruleLabelFactory
4551
* @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
52+
* @param Json $serializer Optional parameter for backward compatibility
4653
*/
4754
public function __construct(
4855
\Magento\SalesRule\Model\RuleFactory $ruleFactory,
4956
\Magento\SalesRule\Api\Data\RuleInterfaceFactory $ruleDataFactory,
5057
\Magento\SalesRule\Api\Data\ConditionInterfaceFactory $conditionDataFactory,
5158
\Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory $ruleLabelFactory,
52-
\Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
59+
\Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
60+
Json $serializer = null
5361
) {
5462
$this->ruleFactory = $ruleFactory;
5563
$this->ruleDataFactory = $ruleDataFactory;
5664
$this->conditionDataFactory = $conditionDataFactory;
5765
$this->ruleLabelFactory = $ruleLabelFactory;
5866
$this->dataObjectProcessor = $dataObjectProcessor;
67+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
5968
}
6069

6170
/**
@@ -81,7 +90,7 @@ protected function mapConditions(RuleDataModel $dataModel, \Magento\SalesRule\Mo
8190
{
8291
$conditionSerialized = $ruleModel->getConditionsSerialized();
8392
if ($conditionSerialized) {
84-
$conditionArray = unserialize($conditionSerialized);
93+
$conditionArray = $this->serializer->unserialize($conditionSerialized);
8594
$conditionDataModel = $this->arrayToConditionDataModel($conditionArray);
8695
$dataModel->setCondition($conditionDataModel);
8796
} else {
@@ -99,7 +108,7 @@ protected function mapActionConditions(RuleDataModel $dataModel, \Magento\SalesR
99108
{
100109
$actionConditionSerialized = $ruleModel->getActionsSerialized();
101110
if ($actionConditionSerialized) {
102-
$actionConditionArray = unserialize($actionConditionSerialized);
111+
$actionConditionArray = $this->serializer->unserialize($actionConditionSerialized);
103112
$actionConditionDataModel = $this->arrayToConditionDataModel($actionConditionArray);
104113
$dataModel->setActionCondition($actionConditionDataModel);
105114
} else {

app/code/Magento/SalesRule/Model/ResourceModel/Rule.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Model\AbstractModel;
1111
use Magento\Rule\Model\ResourceModel\AbstractResource;
1212
use Magento\Framework\EntityManager\EntityManager;
13+
use Magento\Framework\Serialize\Serializer\Json;
1314

1415
/**
1516
* Sales Rule resource model
@@ -56,20 +57,23 @@ class Rule extends AbstractResource
5657
* @param \Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon
5758
* @param string $connectionName
5859
* @param \Magento\Framework\DataObject|null $associatedEntityMapInstance
60+
* @param Json $serializer Optional parameter for backward compatibility
5961
*/
6062
public function __construct(
6163
\Magento\Framework\Model\ResourceModel\Db\Context $context,
6264
\Magento\Framework\Stdlib\StringUtils $string,
6365
\Magento\SalesRule\Model\ResourceModel\Coupon $resourceCoupon,
6466
$connectionName = null,
65-
\Magento\Framework\DataObject $associatedEntityMapInstance = null
67+
\Magento\Framework\DataObject $associatedEntityMapInstance = null,
68+
Json $serializer = null
6669
) {
6770
$this->string = $string;
6871
$this->_resourceCoupon = $resourceCoupon;
6972
$associatedEntitiesMapInstance = $associatedEntityMapInstance ?: ObjectManager::getInstance()->get(
7073
\Magento\SalesRule\Model\ResourceModel\Rule\AssociatedEntityMap::class
7174
);
7275
$this->_associatedEntitiesMap = $associatedEntitiesMapInstance->getData();
76+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
7377
parent::__construct($context, $connectionName);
7478
}
7579

@@ -157,8 +161,8 @@ protected function _afterSave(AbstractModel $object)
157161

158162
// Save product attributes used in rule
159163
$ruleProductAttributes = array_merge(
160-
$this->getProductAttributes(serialize($object->getConditions()->asArray())),
161-
$this->getProductAttributes(serialize($object->getActions()->asArray()))
164+
$this->getProductAttributes($this->serializer->serialize($object->getConditions()->asArray())),
165+
$this->getProductAttributes($this->serializer->serialize($object->getActions()->asArray()))
162166
);
163167
if (count($ruleProductAttributes)) {
164168
$this->setActualProductAttributes($object, $ruleProductAttributes);
@@ -345,19 +349,14 @@ public function setActualProductAttributes($rule, $attributes)
345349
*/
346350
public function getProductAttributes($serializedString)
347351
{
348-
$result = [];
349-
if (preg_match_all(
350-
'~s:32:"salesrule/rule_condition_product";s:9:"attribute";s:\d+:"(.*?)"~s',
352+
// we need 4 backslashes to match 1 in regexp, see http://www.php.net/manual/en/regexp.reference.escape.php
353+
preg_match_all(
354+
'~"Magento\\\\\\\\SalesRule\\\\\\\\Model\\\\\\\\Rule\\\\\\\\Condition\\\\\\\\Product","attribute":"(.*?)"~',
351355
$serializedString,
352356
$matches
353-
)
354-
) {
355-
foreach ($matches[1] as $attributeCode) {
356-
$result[] = $attributeCode;
357-
}
358-
}
359-
360-
return $result;
357+
);
358+
// we always have $matches like [[],[]]
359+
return array_values($matches[1]);
361360
}
362361

363362
/**

app/code/Magento/SalesRule/Model/ResourceModel/Rule/Collection.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace Magento\SalesRule\Model\ResourceModel\Rule;
88

99
use Magento\Quote\Model\Quote\Address;
10+
use Magento\Framework\Serialize\Serializer\Json;
1011

1112
/**
1213
* Sales Rules resource collection model.
@@ -31,6 +32,11 @@ class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\Abstr
3132
*/
3233
protected $_date;
3334

35+
/**
36+
* @var Json $serializer
37+
*/
38+
private $serializer;
39+
3440
/**
3541
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
3642
* @param \Psr\Log\LoggerInterface $logger
@@ -39,6 +45,7 @@ class Collection extends \Magento\Rule\Model\ResourceModel\Rule\Collection\Abstr
3945
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $date
4046
* @param mixed $connection
4147
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
48+
* @param Json $serializer Optional parameter for backward compatibility
4249
*/
4350
public function __construct(
4451
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
@@ -47,10 +54,12 @@ public function __construct(
4754
\Magento\Framework\Event\ManagerInterface $eventManager,
4855
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $date,
4956
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
50-
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
57+
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
58+
Json $serializer = null
5159
) {
5260
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
5361
$this->_date = $date;
62+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
5463
$this->_associatedEntitiesMap = $this->getAssociatedEntitiesMap();
5564
}
5665

@@ -272,7 +281,7 @@ public function _initSelect()
272281
*/
273282
public function addAttributeInConditionFilter($attributeCode)
274283
{
275-
$match = sprintf('%%%s%%', substr(serialize(['attribute' => $attributeCode]), 5, -1));
284+
$match = sprintf('%%%s%%', substr($this->serializer->serialize(['attribute' => $attributeCode]), 1, -1));
276285
$field = $this->_getMappedField('conditions_serialized');
277286
$cCond = $this->_getConditionSql($field, ['like' => $match]);
278287
$field = $this->_getMappedField('actions_serialized');
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\SalesRule\Setup;
7+
8+
use Magento\Framework\DB\FieldDataConverterFactory;
9+
use Magento\Framework\DB\DataConverter\SerializedToJson;
10+
use Magento\Framework\Setup\ModuleContextInterface;
11+
use Magento\Framework\Setup\ModuleDataSetupInterface;
12+
use Magento\Framework\Setup\UpgradeDataInterface;
13+
use Magento\Framework\EntityManager\MetadataPool;
14+
use Magento\SalesRule\Api\Data\RuleInterface;
15+
16+
class UpgradeData implements UpgradeDataInterface
17+
{
18+
/**
19+
* @var FieldDataConverterFactory
20+
*/
21+
private $fieldDataConverterFactory;
22+
23+
/**
24+
* @var MetadataPool
25+
*/
26+
private $metadataPool;
27+
28+
/**
29+
* UpgradeData constructor.
30+
*
31+
* @param FieldDataConverterFactory $fieldDataConverterFactory
32+
* @param MetadataPool $metadataPool
33+
*/
34+
public function __construct(
35+
FieldDataConverterFactory $fieldDataConverterFactory,
36+
MetadataPool $metadataPool
37+
) {
38+
$this->fieldDataConverterFactory = $fieldDataConverterFactory;
39+
$this->metadataPool = $metadataPool;
40+
}
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
46+
{
47+
$setup->startSetup();
48+
49+
if (version_compare($context->getVersion(), '2.0.2', '<')) {
50+
$this->convertSerializedDataToJson($setup);
51+
}
52+
53+
$setup->endSetup();
54+
}
55+
56+
/**
57+
* Convert metadata from serialized to JSON format:
58+
*
59+
* @param ModuleDataSetupInterface $setup
60+
*
61+
* @return void
62+
*/
63+
public function convertSerializedDataToJson($setup)
64+
{
65+
$fieldDataConverter = $this->fieldDataConverterFactory->create(SerializedToJson::class);
66+
$metadata = $this->metadataPool->getMetadata(RuleInterface::class);
67+
68+
$fieldDataConverter->convert(
69+
$setup->getConnection(),
70+
$setup->getTable('salesrule'),
71+
$metadata->getLinkField(),
72+
'conditions_serialized'
73+
);
74+
$fieldDataConverter->convert(
75+
$setup->getConnection(),
76+
$setup->getTable('salesrule'),
77+
$metadata->getLinkField(),
78+
'actions_serialized'
79+
);
80+
}
81+
}

0 commit comments

Comments
 (0)