ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Extension\Updates;
|
||||
|
||||
use Magento\Framework\Config\DataInterface;
|
||||
use Plumrocket\Base\Api\GetModuleVersionInterface;
|
||||
use Plumrocket\Base\Model\Extension\Updates\Load as LoadUpdates;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Get
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetModuleVersionInterface
|
||||
*/
|
||||
private $getModuleVersion;
|
||||
|
||||
/**
|
||||
* @var LoadUpdates
|
||||
*/
|
||||
private $loadUpdates;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Config\DataInterface
|
||||
*/
|
||||
private $extensionsConfig;
|
||||
|
||||
/**
|
||||
* GetUpdates constructor.
|
||||
*
|
||||
* @param \Plumrocket\Base\Api\GetModuleVersionInterface $getModuleVersion
|
||||
* @param \Plumrocket\Base\Model\Extension\Updates\Load $loadUpdates
|
||||
* @param \Magento\Framework\Config\DataInterface $extensionsConfig
|
||||
*/
|
||||
public function __construct(
|
||||
GetModuleVersionInterface $getModuleVersion,
|
||||
LoadUpdates $loadUpdates,
|
||||
DataInterface $extensionsConfig
|
||||
) {
|
||||
$this->getModuleVersion = $getModuleVersion;
|
||||
$this->loadUpdates = $loadUpdates;
|
||||
$this->extensionsConfig = $extensionsConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available updates with changelogs.
|
||||
*
|
||||
* @param array $extensions
|
||||
* @return array
|
||||
* @throws \Magento\Framework\Exception\NotFoundException if we cannot fetch file with updates
|
||||
*/
|
||||
public function execute(array $extensions): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
$updates = $this->loadUpdates->execute();
|
||||
|
||||
foreach ($extensions as $extensionName) {
|
||||
if (($metapackageName = $this->getMetapackageName($extensionName)) && isset($updates[$metapackageName])) {
|
||||
$changeLogSource = $metapackageName;
|
||||
} else {
|
||||
$changeLogSource = $extensionName;
|
||||
}
|
||||
|
||||
if (isset($updates[$extensionName])) {
|
||||
$extensionUpdates = $this->fixBrokenStructure($updates[$changeLogSource]);
|
||||
|
||||
$result[$extensionName] = $this->extractNewReleases(
|
||||
$this->getModuleVersion->execute("Plumrocket_{$extensionName}"),
|
||||
$extensionUpdates
|
||||
);
|
||||
} else {
|
||||
$result[$extensionName] = [];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter installed versions.
|
||||
*
|
||||
* @param string $currentVersion
|
||||
* @param array $updatesInfo
|
||||
* @return array
|
||||
*/
|
||||
private function extractNewReleases(string $currentVersion, array $updatesInfo): array
|
||||
{
|
||||
return array_filter(
|
||||
$updatesInfo['changelogs'],
|
||||
static function ($changelog) use ($currentVersion) {
|
||||
return version_compare($currentVersion, $changelog['version'], '<');
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix xml structure.
|
||||
*
|
||||
* When new version has only one changelog, xml can be broken during convert from array.
|
||||
* We detect and fix it here.
|
||||
*
|
||||
* @param array $updatesInfo
|
||||
* @return array
|
||||
*/
|
||||
public function fixBrokenStructure(array $updatesInfo): array
|
||||
{
|
||||
if (! isset($updatesInfo['changelogs'])) {
|
||||
$updatesInfo['changelogs'] = [];
|
||||
return $updatesInfo;
|
||||
}
|
||||
|
||||
if (isset($updatesInfo['changelogs']['version'])) {
|
||||
$updatesInfo['changelogs'] = [$updatesInfo['changelogs']];
|
||||
}
|
||||
|
||||
$updatesInfo['changelogs'] = $this->fixChangelogsStructure($updatesInfo['changelogs']);
|
||||
|
||||
return $updatesInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix xml structure.
|
||||
*
|
||||
* @param array $changelogs
|
||||
* @return array
|
||||
*/
|
||||
private function fixChangelogsStructure(array $changelogs): array
|
||||
{
|
||||
return array_map(
|
||||
static function ($changelog) {
|
||||
if (isset($changelog['changes']['type'])) {
|
||||
$changelog['changes'] = [$changelog['changes']];
|
||||
}
|
||||
return $changelog;
|
||||
},
|
||||
$changelogs
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get metapackage name.
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return string
|
||||
*/
|
||||
private function getMetapackageName(string $moduleName): string
|
||||
{
|
||||
return (string) $this->extensionsConfig->get("$moduleName/metapackage/directory");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2021 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Extension\Updates;
|
||||
|
||||
use Plumrocket\Base\Model\Extension\GetModuleName;
|
||||
use Plumrocket\Base\Model\External\Urls;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
* @deprecated since 2.8.0 - no more used
|
||||
*/
|
||||
class GetLastVersionMessage
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetModuleName
|
||||
*/
|
||||
private $getModuleName;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\GetModuleName $getModuleName
|
||||
*/
|
||||
public function __construct(GetModuleName $getModuleName)
|
||||
{
|
||||
$this->getModuleName = $getModuleName;
|
||||
}
|
||||
|
||||
public function execute(string $moduleName): array
|
||||
{
|
||||
$moduleName = $this->getModuleName->execute($moduleName);
|
||||
|
||||
$xmlPath = 'https://' . Urls::VERSIONS_URL;
|
||||
$message = '';
|
||||
$version = '';
|
||||
|
||||
try {
|
||||
$context = stream_context_create(
|
||||
[
|
||||
'http' => [
|
||||
'timeout' => 2,
|
||||
'ignore_errors' => true,
|
||||
],
|
||||
]
|
||||
);
|
||||
$string = file_get_contents($xmlPath, false, $context);
|
||||
|
||||
if ($string && $moduleName) {
|
||||
$xml = simplexml_load_string($string);
|
||||
if ($xml && isset($xml->Magento2->{$moduleName})) {
|
||||
$extData = $xml->Magento2->{$moduleName} ?? null;
|
||||
|
||||
if ($extData !== null && isset($extData->message, $extData->version)) {
|
||||
$message = (string)$extData->message;
|
||||
$version = (string)$extData->version;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {}
|
||||
|
||||
return ['message' => $message, 'newv' => $version];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Extension\Updates;
|
||||
|
||||
use Magento\Framework\Exception\FileSystemException;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NotFoundException;
|
||||
use Magento\Framework\Filesystem\DriverInterface;
|
||||
use Magento\Framework\Xml\Parser;
|
||||
use Plumrocket\Base\Model\External\Urls;
|
||||
|
||||
/**
|
||||
* Retrieve information in format
|
||||
* [moduleName => data]
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Load
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Filesystem\DriverInterface
|
||||
*/
|
||||
private $httpsDriver;
|
||||
|
||||
/**
|
||||
* @var array[]
|
||||
*/
|
||||
private $updates;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Xml\Parser
|
||||
*/
|
||||
private $xmlParser;
|
||||
|
||||
/**
|
||||
* Load constructor.
|
||||
*
|
||||
* @param \Magento\Framework\Filesystem\DriverInterface $httpsDriver
|
||||
* @param \Magento\Framework\Xml\Parser $xmlParser
|
||||
*/
|
||||
public function __construct(DriverInterface $httpsDriver, Parser $xmlParser)
|
||||
{
|
||||
$this->httpsDriver = $httpsDriver;
|
||||
$this->xmlParser = $xmlParser;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|array[]|string
|
||||
* @throws \Magento\Framework\Exception\NotFoundException
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
if (null === $this->updates) {
|
||||
$this->updates = [];
|
||||
|
||||
if ($fileContent = $this->readInternalFile()) {
|
||||
try {
|
||||
$updates = $this->xmlParser->loadXML($fileContent)->xmlToArray();
|
||||
if (isset($updates['modules'])) {
|
||||
$this->updates = $updates['modules'];
|
||||
}
|
||||
} catch (LocalizedException $e) {
|
||||
$this->updates = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->updates;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws \Magento\Framework\Exception\NotFoundException
|
||||
*/
|
||||
private function readInternalFile() : string
|
||||
{
|
||||
try {
|
||||
$fileContent = $this->httpsDriver->fileGetContents(Urls::CHANGELOGS_URL, null, stream_context_create(['ssl'=>['verify_peer'=>false,'verify_peer_name'=>false]]));
|
||||
} catch (FileSystemException $e) {
|
||||
throw new NotFoundException(__('Fail to fetch latest versions of Plumrocket extensions.'));
|
||||
}
|
||||
|
||||
return $fileContent;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user