From 87f5f98775d1d75f0e23bf7d88c6e7199f072fc6 Mon Sep 17 00:00:00 2001 From: Tomas Gerulaitis Date: Tue, 28 Nov 2017 11:24:30 +0000 Subject: [PATCH 1/2] Use Json serializer to process custom strategies This matches serialization done by the \Magento\Config\Model\Config\Backend\Serialized\ArraySerialized class when saving the custom strategies in the admin area in Magento 2.2. --- src/Helper/Config.php | 9 +++++++-- src/Setup/InstallData.php | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Helper/Config.php b/src/Helper/Config.php index a22d8a1..0f1b663 100644 --- a/src/Helper/Config.php +++ b/src/Helper/Config.php @@ -18,13 +18,18 @@ class Config extends \Magento\Framework\App\Helper\AbstractHelper /** @var \Magento\Cms\Helper\Page $cmsPageHelper */ protected $cmsPageHelper; + /** @var \Magento\Framework\Serialize\Serializer\Json $serializer */ + protected $serializer; + public function __construct( \Magento\Framework\App\Helper\Context $context, - \Magento\Cms\Helper\Page $cmsPageHelper + \Magento\Cms\Helper\Page $cmsPageHelper, + \Magento\Framework\Serialize\Serializer\Json $serializer ) { parent::__construct($context); $this->cmsPageHelper = $cmsPageHelper; + $this->serializer = $serializer; } /** @@ -65,7 +70,7 @@ public function getCustomStrategies($store = null) $custom_strategies = $this->scopeConfig->getValue(static::XML_PATH_CUSTOM_STRATEGIES, ScopeInterface::SCOPE_STORE, $store); if (is_string($custom_strategies) && !empty($custom_strategies)) { - $custom_strategies = unserialize($custom_strategies); + $custom_strategies = $this->serializer->unserialize($custom_strategies); } if (!is_array($custom_strategies)) { diff --git a/src/Setup/InstallData.php b/src/Setup/InstallData.php index f87e0d8..8cb982e 100644 --- a/src/Setup/InstallData.php +++ b/src/Setup/InstallData.php @@ -21,14 +21,19 @@ class InstallData implements InstallDataInterface /** @var \Magento\Framework\App\Config\Storage\WriterInterface $configWriter */ protected $configWriter; + /** @var \Magento\Framework\Serialize\Serializer\Json $serializer */ + protected $serializer; + public function __construct( \Magento\Cms\Model\PageFactory $pageFactory, \Magento\Cms\Model\PageRepository $pageRepository, - \Magento\Framework\App\Config\Storage\WriterInterface $configWriter + \Magento\Framework\App\Config\Storage\WriterInterface $configWriter, + \Magento\Framework\Serialize\Serializer\Json $serializer ) { $this->pageFactory = $pageFactory; $this->pageRepository = $pageRepository; $this->configWriter = $configWriter; + $this->serializer = $serializer; } /** @@ -74,7 +79,7 @@ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $this->configWriter->save( "web/serviceworker/custom_strategies", - serialize($strategies) + $this->serializer->serialize($strategies) ); $setup->endSetup(); From d1df91852092912a80bb9f32c7f97d1c0da3828d Mon Sep 17 00:00:00 2001 From: Tomas Gerulaitis Date: Tue, 28 Nov 2017 12:26:15 +0000 Subject: [PATCH 2/2] Use Json serializer in the Upgrade schema Fixes #9 --- src/Setup/UpgradeData.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Setup/UpgradeData.php b/src/Setup/UpgradeData.php index 484fde0..cb11cbe 100644 --- a/src/Setup/UpgradeData.php +++ b/src/Setup/UpgradeData.php @@ -15,12 +15,17 @@ class UpgradeData implements UpgradeDataInterface /** @var \Magento\Framework\App\Config\Storage\WriterInterface $configWriter */ protected $configWriter; + /** @var \Magento\Framework\Serialize\Serializer\Json $serializer */ + protected $serializer; + public function __construct( \Magento\Framework\App\Config\ValueInterface $configReader, - \Magento\Framework\App\Config\Storage\WriterInterface $configWriter + \Magento\Framework\App\Config\Storage\WriterInterface $configWriter, + \Magento\Framework\Serialize\Serializer\Json $serializer ) { $this->configReader = $configReader; $this->configWriter = $configWriter; + $this->serializer = $serializer; } /** @@ -63,7 +68,7 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $this->configWriter->save( "web/serviceworker/custom_strategies", - serialize($value), + $this->serializer->serialize($value), $config->getScope(), $config->getScopeId() ); @@ -87,7 +92,7 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $this->configWriter->save( "web/serviceworker/custom_strategies", - serialize($strategies) + $this->serializer->serialize($strategies) ); } }