b374k
m1n1 1.01
Apache/2.4.41 (Ubuntu)
Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64
uid=33(www-data) gid=33(www-data) groups=33(www-data)
server ip : 62.171.164.128 | your ip : 127.0.0.1
safemode OFF
 >  / home / dev2.destoffenstraat.com / app / code / Glace / Lib01 / Helper /
Filename/home/dev2.destoffenstraat.com/app/code/Glace/Lib01/Helper/Module.php
Size5.88 kb
Permissionrwxrwxrwx
Ownerroot : root
Create time17-Aug-2025 10:26
Last modified06-Apr-2021 18:06
Last accessed23-Aug-2025 02:07
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
<?php
/**
* @author Glace Team
* @copyright Copyright (c) 2019 Glace (https://www.glace.com)
* @package Glace_Lib01
*/


namespace Glace\Lib01\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use SimpleXMLElement;
use Zend\Http\Client\Adapter\Curl as CurlClient;
use Zend\Http\Response as HttpResponse;
use Zend\Uri\Http as HttpUri;
use Magento\Framework\Json\DecoderInterface;

class Module extends AbstractHelper
{
const EXTENSIONS_PATH = 'glacelib01_extensions';

const URL_EXTENSIONS = 'http://glace.com/feed-extensions-m2.xml';

/**
* @var \Glace\Lib01\Model\Serializer
*/
protected $serializer;

/**
* @var CurlClient
*/
protected $curlClient;

/**
* @var \Magento\Framework\App\CacheInterface
*/
protected $cache;

/**
* @var array|null
*/
private $modulesData = null;

/**
* @var array
*/
protected $restrictedModules = [
'Glace_CommonRules',
'Glace_Router'
];

/**
* @var \Magento\Framework\Module\Dir\Reader
*/
private $moduleReader;

/**
* @var \Magento\Framework\Filesystem\Driver\File
*/
private $filesystem;

/**
* @var DecoderInterface
*/
private $jsonDecoder;

public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Glace\Lib01\Model\Serializer $serializer,
\Magento\Framework\App\CacheInterface $cache,
\Magento\Framework\Module\Dir\Reader $moduleReader,
\Magento\Framework\Filesystem\Driver\File $filesystem,
DecoderInterface $jsonDecoder,
CurlClient $curl
) {
parent::__construct($context);

$this->cache = $cache;
$this->serializer = $serializer;
$this->curlClient = $curl;
$this->moduleReader = $moduleReader;
$this->filesystem = $filesystem;
$this->jsonDecoder = $jsonDecoder;
}

/**
* Get array with info about all Glace Magento2 Extensions
*
* @return bool|mixed
*/
public function getAllExtensions()
{
$serialized = $this->cache->load(self::EXTENSIONS_PATH);
if ($serialized === false) {
$this->reload();
$serialized = $this->cache->load(self::EXTENSIONS_PATH);
}
$result = $this->serializer->unserialize($serialized);

return $result;
}

/**
* Save extensions data to magento cache
*/
public function reload()
{
$feedData = [];
$feedXml = $this->getFeedData();
if ($feedXml && $feedXml->channel && $feedXml->channel->item) {
foreach ($feedXml->channel->item as $item) {
$code = (string)$item->code;

if (!isset($feedData[$code])) {
$feedData[$code] = [];
}

$feedData[$code][(string)$item->title] = [
'name' => (string)$item->title,
'url' => (string)$item->link,
'version' => (string)$item->version,
'conflictExtensions' => (string)$item->conflictExtensions,
'guide' => (string)$item->guide,
];
}

if ($feedData) {
$this->cache->save($this->serialize($feedData), self::EXTENSIONS_PATH);
}
}
}

/**
* Read data from xml file with curl
*
* @return bool|SimpleXMLElement
*/
protected function getFeedData()
{
try {
$curlClient = $this->getCurlClient();

$location = self::URL_EXTENSIONS;
$uri = new HttpUri($location);

$curlClient->setOptions(
[
'timeout' => 8
]
);

$curlClient->connect($uri->getHost(), $uri->getPort());
$curlClient->write('GET', $uri, 1.0);
$data = HttpResponse::fromString($curlClient->read());

$curlClient->close();

$xml = new SimpleXMLElement($data->getContent());
} catch (\Exception $e) {
return false;
}

return $xml;
}

/**
* Returns the cURL client that is being used.
*
* @return CurlClient
*/
public function getCurlClient()
{
if ($this->curlClient === null) {
$this->curlClient = new CurlClient();
}

return $this->curlClient;
}

public function serialize($data)
{
return $this->serializer->serialize($data);
}

/**
* @return array
*/
public function getRestrictedModules()
{
return $this->restrictedModules;
}

/**
* Read info about extension from composer json file
*
* @param $moduleCode
*
* @return mixed
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function getModuleInfo($moduleCode)
{
try {
$dir = $this->moduleReader->getModuleDir('', $moduleCode);
$file = $dir . '/composer.json';

$string = $this->filesystem->fileGetContents($file);
$json = $this->jsonDecoder->decode($string);
} catch (\Magento\Framework\Exception\FileSystemException $e) {
$json = [];
}

return $json;
}

/**
* @param $moduleCode
*
* @return array
*/
public function getFeedModuleData($moduleCode)
{
$moduleData = [];
if ($this->modulesData === null || $this->modulesData === false) {
$this->modulesData = $this->getAllExtensions();
}

if ($this->modulesData && isset($this->modulesData[$moduleCode])) {
$module = $this->modulesData[$moduleCode];
if ($module && is_array($module)) {
$module = array_shift($module);
}

$moduleData = $module;
}

return $moduleData;
}
}