|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magefan ([email protected]). All rights reserved. |
| 4 | + * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement). |
| 5 | + * |
| 6 | + * Glory to Ukraine! Glory to the heroes! |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types = 1); |
| 10 | + |
| 11 | +namespace Magefan\LazyLoad\Setup\Patch\Data; |
| 12 | + |
| 13 | +use Magento\Framework\Setup\Patch\DataPatchInterface; |
| 14 | +use Magento\Framework\Setup\ModuleDataSetupInterface; |
| 15 | +use Magefan\LazyLoad\Model\Config as Config; |
| 16 | +use Magento\Framework\App\Config\Storage\WriterInterface; |
| 17 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 18 | +use Magefan\LazyLoad\Model\Config\Source\Method as LazyLoadMethod; |
| 19 | +use Magefan\LazyLoad\Model\Config\Source\BlocksToLazyLoad; |
| 20 | + |
| 21 | +class SaveDefaultLazyLoadMethod implements DataPatchInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var ModuleDataSetupInterface |
| 25 | + */ |
| 26 | + private $moduleDataSetup; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var Config |
| 30 | + */ |
| 31 | + private $config; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var WriterInterface |
| 35 | + */ |
| 36 | + private $configWriter; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param ModuleDataSetupInterface $moduleDataSetup |
| 40 | + * @param Config $config |
| 41 | + * @param WriterInterface $configWriter |
| 42 | + */ |
| 43 | + public function __construct( |
| 44 | + ModuleDataSetupInterface $moduleDataSetup, |
| 45 | + Config $config, |
| 46 | + WriterInterface $configWriter |
| 47 | + ) { |
| 48 | + $this->moduleDataSetup = $moduleDataSetup; |
| 49 | + $this->config = $config; |
| 50 | + $this->configWriter = $configWriter; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Set Default Lazy Load Method as JS to keep existing lazy load config for customers that configured lazy laod. |
| 55 | + * |
| 56 | + * @return void |
| 57 | + */ |
| 58 | + public function apply() |
| 59 | + { |
| 60 | + $this->moduleDataSetup->startSetup(); |
| 61 | + |
| 62 | + $connection = $this->moduleDataSetup->getConnection(); |
| 63 | + $tableName = $this->moduleDataSetup->getTable('core_config_data'); |
| 64 | + |
| 65 | + if ($this->config->getBlocksInfo()) { |
| 66 | + $query = $connection->select() |
| 67 | + ->from($tableName, ['config_id', 'value']) |
| 68 | + ->where('path = ?', Config::XML_PATH_LAZY_METHOD); |
| 69 | + |
| 70 | + // if lazy load was installed previusly and lazy laod method not set yet, set js lazy load as a default method |
| 71 | + if (!$connection->fetchAll($query)) { |
| 72 | + $this->configWriter->save(Config::XML_PATH_LAZY_METHOD, LazyLoadMethod::JAVASCRIPT); |
| 73 | + $this->configWriter->save(Config::XML_PATH_LAZY_BLOCKS_TO_LAZY_LOAD, BlocksToLazyLoad::SELECTED); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + $this->moduleDataSetup->endSetup(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return array|string[] |
| 82 | + */ |
| 83 | + public static function getDependencies() |
| 84 | + { |
| 85 | + return []; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @return array|string[] |
| 90 | + */ |
| 91 | + public function getAliases() |
| 92 | + { |
| 93 | + return []; |
| 94 | + } |
| 95 | +} |
0 commit comments