![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/shiplab/Helper/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Helper; use Extmag\Shiplab\Helper\Config\Structure\Element\FlyweightFactory; use Extmag\Shiplab\Helper\Config\Structure\Element\Tab; use Extmag\Shiplab\Model\Cache\Type; use Extmag\Shiplab\Model\ConfigurationFactory; use Extmag\Shiplab\Model\ResourceModel\ConfigurationScope\Collection as ConfigurationScope; use Magento\Config\Model\Config\Structure\AbstractElement; use Magento\Config\Model\Config\Structure\ElementInterface; use Magento\Config\Model\Config\StructureElementInterface; use Magento\Framework\App\ObjectManager; use Magento\Framework\App\RequestInterface; use Magento\Framework\Config\Scope; use Magento\Framework\Serialize\SerializerInterface; class Config extends Config\Data { const TYPE_KEY = '_elementType'; protected $_loadedScopes = []; protected $_currentScope = 'default'; protected $_cacheId = 'shiplab_config'; protected $_reader; protected $serializer; protected $_data = []; protected $_scopePriorityScheme = []; protected $_tabIterator; protected $_request; protected $_flyweightFactory; protected $_configScope; protected $_configurationFactory; protected $mappedPaths; protected $_dataConfItems = []; /** * @var ConfigurationScope */ protected $configurationScope; /** * List of cached elements * * @var ElementInterface[] */ protected $_elements; /** * List of config sections * * @var array * @since 100.1.0 */ protected $sectionList; /** * @param Config\Structure\Reader $reader * @param Config\Structure\Element\Iterator\Tab $tabIterator * @param Type $cache * @param RequestInterface $request * @param FlyweightFactory $flyweightFactory * @param Scope $configScope * @param ConfigurationFactory $configurationFactory * @param ConfigurationScope $configurationScope * @param SerializerInterface|null $serializer = null */ public function __construct( Config\Structure\Reader $reader, Config\Structure\Element\Iterator\Tab $tabIterator, Type $cache, RequestInterface $request, FlyweightFactory $flyweightFactory, Scope $configScope, ConfigurationFactory $configurationFactory, ConfigurationScope $configurationScope, SerializerInterface $serializer = null ) { $this->_reader = $reader; $this->_cache = $cache; $this->_tabIterator = $tabIterator; $this->_flyweightFactory = $flyweightFactory; $this->_configScope = $configScope; $this->_configurationFactory = $configurationFactory; $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class); parent::__construct($reader, $cache, $this->_cacheId, $serializer); $this->_request = $request; $this->_data = $this->get(); $this->configurationScope = $configurationScope; } /** * Get config value by key * * @param string $key * @param mixed $default * * @return array|mixed|null */ public function get($key = null, $default = null) { $this->setCurrentScope(); $this->_loadScopedData(); return parent::get($key, $default); } /** * Load data for current scope * * @return void */ protected function _loadScopedData() { $scope = $this->_configScope->getCurrentScope(); if (!isset($this->_loadedScopes[$scope])) { if (!in_array($scope, $this->_scopePriorityScheme)) { $this->_scopePriorityScheme[] = $scope; } foreach ($this->_scopePriorityScheme as $scopeCode) { if (!isset($this->_loadedScopes[$scopeCode])) { if ($scopeCode !== 'primary' && ($data = $this->_cache->load($scopeCode . '::' . $this->_cacheId)) ) { $data = $this->serializer->unserialize($data); } else { $data = $this->_reader->read($scopeCode); if ($scopeCode !== 'primary') { $this->_cache->save( $this->serializer->serialize($data), $scopeCode . '::' . $this->_cacheId ); } } $this->merge($data); $this->_loadedScopes[$scopeCode] = true; } if ($scopeCode == $scope) { break; } } } } /** * @return mixed */ public function getSectionList() { if (empty($this->sectionList)) { foreach ($this->_data['sections'] as $sectionId => $section) { if (isset($section['children']) && is_array($section['children'])) { foreach ($section['children'] as $childId => $child) { $this->sectionList[$sectionId . '_' . $childId] = true; } } } } return $this->sectionList; } /** * @param $path * @return ElementInterface|mixed */ public function getElement($path) { return $this->getElementByPathParts(explode('/', $path)); } /** * @param array $pathParts * @return ElementInterface|mixed */ public function getElementByPathParts(array $pathParts) { $path = implode('_', $pathParts); if (isset($this->_elements[$path])) { return $this->_elements[$path]; } $children = []; if ($this->_data) { $children = $this->_data['sections']; } $child = []; foreach ($pathParts as $pathPart) { if ($children && (isset($children[$pathPart]))) { $child = $children[$pathPart]; $children = $child['children'] ?? []; } else { $child = $this->_createEmptyElement($pathParts); break; } } $this->_elements[$path] = $this->_flyweightFactory->create($child['_elementType']); $this->_elements[$path]->setData($child, $this->getCurrentScope()); return $this->_elements[$path]; } /** * @param array $pathParts * @return array */ protected function _createEmptyElement(array $pathParts) { switch (count($pathParts)) { case 1: $elementType = 'section'; break; case 2: $elementType = 'group'; break; default: $elementType = 'field'; } $elementId = array_pop($pathParts); return ['id' => $elementId, 'path' => implode('/', $pathParts), '_elementType' => $elementType]; } /** * @return string */ public function getCurrentScope() { return $this->_currentScope; } /** * @return void */ public function setCurrentScope() { if ($this->_request->getParam('country', '')) { $this->_currentScope = 'country'; } elseif ($this->_request->getParam('direction', '')) { $this->_currentScope = 'direction'; } elseif ($this->_request->getParam('store', '')) { $this->_currentScope = 'store'; } elseif ($this->_request->getParam('default', 'default')) { $this->_currentScope = 'default'; } } /** * @param $path * @return ElementInterface|mixed */ public function getElementByConfigPath($path) { $allPaths = $this->getFieldPaths(); if (isset($allPaths[$path])) { $path = array_shift($allPaths[$path]); } return $this->getElementByPathParts(explode('/', $path)); } /** * @return array */ public function getFieldPaths() { $sections = !empty($this->_data['sections']) ? $this->_data['sections'] : []; if (!$this->mappedPaths) { $this->mappedPaths = $this->getFieldsRecursively($sections); } return $this->mappedPaths; } /** * @param array $elements * @return array */ protected function getFieldsRecursively(array $elements = []) { $result = []; foreach ($elements as $element) { if (isset($element['children'])) { $result = array_replace_recursive( $result, $this->getFieldsRecursively($element['children']) ); } elseif ($element['_elementType'] === 'field' && isset($element['label'])) { $structurePath = (isset($element['path']) ? $element['path'] . '/' : '') . $element['id']; $configPath = $element['config_path'] ?? $structurePath; if (!isset($result[$configPath])) { $result[$configPath] = []; } $result[$configPath][] = $structurePath; } } return $result; } /** * @return AbstractElement|StructureElementInterface */ public function getFirstSection() { /*$this->get();*/ $tabs = $this->getTabs(); $tabs->rewind(); /** @var $tab Tab */ $tab = $tabs->current(); $tab->getChildren()->rewind(); if (!$tab->getChildren()->current()->isVisible()) { throw new LocalizedException(__('Visible section not found.')); } return $tab->getChildren()->current(); } /** * @return Config\Structure\Element\Iterator\Tab */ public function getTabs() { if (isset($this->_data['sections'])) { foreach ($this->_data['sections'] as $sectionId => $section) { if (isset($section['tab']) && $section['tab']) { $this->_data['tabs'][$section['tab']]['children'][$sectionId] = $section; } } $this->_tabIterator->setElements($this->_data['tabs'], $this->getCurrentScope()); } return $this->_tabIterator; } /** * @param $attributeName * @param $attributeValue * @return array */ public function getFieldPathsByAttribute($attributeName, $attributeValue) { $result = []; if (empty($this->_data['sections'])) { return $result; } foreach ($this->_data['sections'] as $section) { if (!isset($section['children'])) { continue; } foreach ($section['children'] as $group) { if (isset($group['children'])) { $path = $section['id'] . '/' . $group['id']; $result = array_merge( $result, $this->_getGroupFieldPathsByAttribute( $group['children'], $path, $attributeName, $attributeValue ) ); } } } return $result; } /** * @param array $fields * @param $parentPath * @param $attributeName * @param $attributeValue * @return array */ protected function _getGroupFieldPathsByAttribute(array $fields, $parentPath, $attributeName, $attributeValue) { $result = []; foreach ($fields as $field) { if (isset($field['children'])) { $result += $this->_getGroupFieldPathsByAttribute( $field['children'], $parentPath . '/' . $field['id'], $attributeName, $attributeValue ); } elseif (isset($field[$attributeName]) && $field[$attributeName] == $attributeValue) { $result[] = $parentPath . '/' . $field['id']; } } return $result; } /** * @param $path * @param $scope * @param $scopeCode * @return mixed|string */ public function getConfigValue( $path = null, $scope = 'default', $scopeCode = null ) { $configPath = $scope; if ($scope !== 'default' && $scopeCode) { $configPath .= '/' . $scopeCode; } if ($path) { $configPath .= '/' . $path; } $this->collectConfig(); if ($this->_dataConfItems && isset($this->_dataConfItems[$configPath])) { return $this->_dataConfItems[$configPath]; } else { return $this->getCollectConfigValue($scope, $scopeCode, $path); } } /** * @return array|bool|float|int|string|null */ public function collectConfig() { $this->_dataConfItems = $this->_cache->load('collect_data::' . $this->_cacheId); if (!is_array($this->_dataConfItems)) { $this->_dataConfItems = []; } if (empty($this->_dataConfItems)) { $configItems = $this->_configurationFactory->create()->getCollection(); if (count($configItems) > 0) { foreach ($configItems as $data) { $dataValue = $data->getValue(); $path = $data->getScope() . '/' . $data->getScopeId() . '/' . $data->getPath(); if ($data->getScope() == 'default') { $path = $data->getScope() . '/' . $data->getPath(); } $this->_dataConfItems[$path] = $dataValue; } $this->_cache->save( $this->serializer->serialize($this->_dataConfItems), 'collect_data::' . $this->_cacheId ); } } else { $this->_dataConfItems = $this->serializer->unserialize($this->_dataConfItems); } return $this->_dataConfItems; } /** * @param $scope * @param $scopeId * @param $path * @return mixed|string */ protected function getCollectConfigValue($scope, $scopeId, $path) { $scopes = $this->getCollectScopes(); if ($scopes && count($scopes) > 0) { switch ($scope) { case 'store': $parentScope = 'default'; if (isset($scopes[$scope . '/' . $scopeId][$parentScope]) && !empty($this->_dataConfItems[$scopes[$scope . '/' . $scopeId][$parentScope] . '/' . $path]) ) { return $this->_dataConfItems[$scopes[$scope . '/' . $scopeId][$parentScope] . '/' . $path]; } break; case 'direction': $parentScope = 'store'; if (isset($scopes[$scope . '/' . $scopeId][$parentScope]) && !empty($this->_dataConfItems[$scopes[$scope . '/' . $scopeId][$parentScope] . '/' . $path]) ) { return $this->_dataConfItems[$scopes[$scope . '/' . $scopeId][$parentScope] . '/' . $path]; } elseif (isset($scopes[$scope . '/' . $scopeId]['default']) && !empty($this->_dataConfItems[$scopes[$scope . '/' . $scopeId]['default'] . '/' . $path]) ) { return $this->_dataConfItems[$scopes[$scope . '/' . $scopeId]['default'] . '/' . $path]; } break; case 'country': $parentScope = 'direction'; if (isset($scopes[$scope . '/' . $scopeId][$parentScope]) && !empty($this->_dataConfItems[$scopes[$scope . '/' . $scopeId][$parentScope] . '/' . $path]) ) { return $this->_dataConfItems[$scopes[$scope . '/' . $scopeId][$parentScope] . '/' . $path]; } elseif (isset($scopes[$scope . '/' . $scopeId]['store']) && !empty($this->_dataConfItems[$scopes[$scope . '/' . $scopeId]['store'] . '/' . $path]) ) { return $this->_dataConfItems[$scopes[$scope . '/' . $scopeId]['store'] . '/' . $path]; } elseif (isset($scopes[$scope . '/' . $scopeId]['default']) && !empty($this->_dataConfItems[$scopes[$scope . '/' . $scopeId]['default'] . '/' . $path]) ) { return $this->_dataConfItems[$scopes[$scope . '/' . $scopeId]['default'] . '/' . $path]; } break; } } return ""; } /** * @return array|bool|float|int|string|null */ protected function getCollectScopes() { $scopes = $this->_cache->load('collect_scope::' . $this->_cacheId); if (!is_array($scopes)) { $scopes = []; } if (empty($scopes)) { $scopesModel = $this->configurationScope->load(); if (count($scopesModel) > 0) { foreach ($scopesModel as $model) { if ($model->getCountry()) { $scopes['country/' . $model->getCountry()]['direction'] = 'direction/' . $model->getDirection(); $scopes['country/' . $model->getCountry()]['store'] = 'store/' . $model->getStore(); $scopes['country/' . $model->getCountry()]['default'] = 'default'; } if ($model->getDirection()) { $scopes['direction/' . $model->getDirection()]['store'] = 'store/' . $model->getStore(); $scopes['direction/' . $model->getDirection()]['default'] = 'default'; } if ($model->getStore()) { $scopes['store/' . $model->getStore()]['default'] = 'default'; } } } else { return []; } $this->_cache->save($this->serializer->serialize($scopes), 'collect_scope::' . $this->_cacheId); } else { $scopes = $this->serializer->unserialize($scopes); } return $scopes; } /** * @return void */ public function clearCache() { $this->_cache->clean(); } }