ltsecurityinc安防产品在线商城代码
This commit is contained in:
+233
@@ -0,0 +1,233 @@
|
||||
<?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;
|
||||
|
||||
use Magento\AdminNotification\Model\Feed;
|
||||
use Magento\Framework\Module\ModuleListInterface;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Plumrocket\Base\Api\ExtensionInformationListInterface;
|
||||
use Plumrocket\Base\Helper\Config;
|
||||
use Plumrocket\Base\Model\External\Urls;
|
||||
|
||||
/**
|
||||
* Plumrocket Base admin notification feed model
|
||||
*/
|
||||
class AdminNotificationFeed extends Feed
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Backend\Model\Auth\Session
|
||||
*/
|
||||
private $backendAuthSession;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\ProductMetadataInterface
|
||||
*/
|
||||
protected $productMetadata;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Helper\Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\ExtensionInformationListInterface
|
||||
*/
|
||||
private $extensionInformationList;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey
|
||||
*/
|
||||
private $getTrueCustomerKey;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Key
|
||||
*/
|
||||
private $authorizationKey;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Module\ModuleListInterface
|
||||
*/
|
||||
private $moduleList;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\Model\Context $context
|
||||
* @param \Magento\Framework\Registry $registry
|
||||
* @param \Magento\Backend\App\ConfigInterface $backendConfig
|
||||
* @param \Magento\AdminNotification\Model\InboxFactory $inboxFactory
|
||||
* @param \Magento\Backend\Model\Auth\Session $backendAuthSession
|
||||
* @param \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory
|
||||
* @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
|
||||
* @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
|
||||
* @param \Magento\Framework\UrlInterface $urlBuilder
|
||||
* @param \Plumrocket\Base\Helper\Config $config
|
||||
* @param \Plumrocket\Base\Api\ExtensionInformationListInterface $extensionInformationList
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey $getTrueCustomerKey
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Key $authorizationKey
|
||||
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
|
||||
* @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
|
||||
* @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
\Magento\Framework\Model\Context $context,
|
||||
\Magento\Framework\Registry $registry,
|
||||
\Magento\Backend\App\ConfigInterface $backendConfig,
|
||||
\Magento\AdminNotification\Model\InboxFactory $inboxFactory,
|
||||
\Magento\Backend\Model\Auth\Session $backendAuthSession,
|
||||
\Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
|
||||
\Magento\Framework\App\DeploymentConfig $deploymentConfig,
|
||||
\Magento\Framework\App\ProductMetadataInterface $productMetadata,
|
||||
UrlInterface $urlBuilder,
|
||||
Config $config,
|
||||
ExtensionInformationListInterface $extensionInformationList,
|
||||
\Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey $getTrueCustomerKey,
|
||||
\Plumrocket\Base\Model\Extension\Authorization\Key $authorizationKey,
|
||||
ModuleListInterface $moduleList,
|
||||
?\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
|
||||
?\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct(
|
||||
$context,
|
||||
$registry,
|
||||
$backendConfig,
|
||||
$inboxFactory,
|
||||
$curlFactory,
|
||||
$deploymentConfig,
|
||||
$productMetadata,
|
||||
$urlBuilder,
|
||||
$resource,
|
||||
$resourceCollection,
|
||||
$data
|
||||
);
|
||||
$this->backendAuthSession = $backendAuthSession;
|
||||
$this->productMetadata = $productMetadata;
|
||||
$this->config = $config;
|
||||
$this->extensionInformationList = $extensionInformationList;
|
||||
$this->getTrueCustomerKey = $getTrueCustomerKey;
|
||||
$this->authorizationKey = $authorizationKey;
|
||||
$this->moduleList = $moduleList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve feed url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFeedUrl(): string
|
||||
{
|
||||
if ($this->_feedUrl === null) {
|
||||
$this->_feedUrl = 'https://' . Urls::NOTIFICATIONS_URL . '/';
|
||||
}
|
||||
|
||||
// phpcs:ignore Magento2.Functions.DiscouragedFunction
|
||||
$domain = parse_url($this->urlBuilder->getBaseUrl(), PHP_URL_HOST) ?: '';
|
||||
|
||||
$url = $this->_feedUrl . 'domain/' . urlencode($domain);
|
||||
|
||||
$modulesParams = [];
|
||||
foreach ($this->extensionInformationList->getList()->getItems() as $extensionInformation) {
|
||||
$modulesParams[] = implode(',', [
|
||||
$extensionInformation->getModuleName(),
|
||||
$extensionInformation->getInstalledVersion(),
|
||||
$this->getNotificationKey($extensionInformation->getModuleName())
|
||||
]);
|
||||
}
|
||||
|
||||
if (count($modulesParams)) {
|
||||
$url .= '/modules/' . base64_encode(implode(';', $modulesParams));
|
||||
}
|
||||
|
||||
if ($this->config->isEnabledNotifications() && $this->config->getEnabledNotificationLists()) {
|
||||
$url .= '/lists/' . implode('|', $this->config->getEnabledNotificationLists());
|
||||
} else {
|
||||
$url .= '/lists/none';
|
||||
}
|
||||
|
||||
if ($this->config->isEnabledStatistic()) {
|
||||
$ed = $this->productMetadata->getEdition();
|
||||
$url .= '/platform/' . ($ed === 'Community' ? 'm2ce' : 'm2ee');
|
||||
$url .= '/edition/' . $ed;
|
||||
$url .= '/magento_version/' . $this->productMetadata->getVersion();
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check feed for modification
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function checkUpdate()
|
||||
{
|
||||
if (! $this->moduleList->has('Magento_AdminNotification')) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$session = $this->backendAuthSession;
|
||||
$time = time();
|
||||
$frequency = $this->getFrequency();
|
||||
if (($frequency + $session->getMfBaseNoticeLastUpdate() > $time)
|
||||
|| ($frequency + $this->getLastUpdate() > $time)
|
||||
) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
$session->setPANLastUpdate($time);
|
||||
parent::checkUpdate();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve update frequency
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getFrequency(): int
|
||||
{
|
||||
return 86400;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve last update time
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getLastUpdate(): int
|
||||
{
|
||||
return (int) $this->_cacheManager->load('plumrocket_admin_notifications_lastcheck');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set last update time (now)
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLastUpdate()
|
||||
{
|
||||
$this->_cacheManager->save(time(), 'plumrocket_admin_notifications_lastcheck');
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive key
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return string
|
||||
*/
|
||||
public function getNotificationKey($moduleName): string
|
||||
{
|
||||
return implode(',', [
|
||||
$this->getTrueCustomerKey->execute($moduleName),
|
||||
$this->authorizationKey->get($moduleName)
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2023 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
|
||||
/**
|
||||
* @since 2.10.0
|
||||
* Using interface can cause "Cannot instantiate interface"
|
||||
* because <preference> applies only after cache cleaning.
|
||||
* To avoid helpdesk tickets it's better to use model instead of interface.
|
||||
*/
|
||||
class ConfigUtils
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
||||
*/
|
||||
public function __construct(ScopeConfigInterface $scopeConfig)
|
||||
{
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive magento config value by store
|
||||
*
|
||||
* @param string $path full path, eg: "pr_base/general/enabled"
|
||||
* @param string|int|\Magento\Framework\App\ScopeInterface $store store model, store view code, or website code
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStoreConfig(string $path, $store = null)
|
||||
{
|
||||
return $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $store);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive magento global config value.
|
||||
*
|
||||
* @param string $path full path, eg: "pr_base/general/enabled"
|
||||
* @return mixed
|
||||
* @since 2.11.4
|
||||
*/
|
||||
public function getGlobalConfig(string $path)
|
||||
{
|
||||
return $this->scopeConfig->getValue($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive magento config value by store or by other scope type
|
||||
*
|
||||
* @param string $path full path, eg: "pr_base/general/enabled"
|
||||
* @param string|int $scopeCode store view code or website code
|
||||
* @param string|null $scopeType
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConfig(string $path, $scopeCode = null, $scopeType = null)
|
||||
{
|
||||
if ($scopeType === null) {
|
||||
$scopeType = ScopeInterface::SCOPE_STORE;
|
||||
}
|
||||
return $this->scopeConfig->getValue($path, $scopeType, $scopeCode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert multiline text into array.
|
||||
*
|
||||
* @param string $fieldValue
|
||||
* @return array
|
||||
*/
|
||||
public function splitTextareaValueByLine(string $fieldValue): array
|
||||
{
|
||||
$lines = explode(PHP_EOL, $fieldValue);
|
||||
|
||||
if (empty($lines)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_filter(array_map('trim', $lines));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert multiselect value into array.
|
||||
*
|
||||
* @param string $value
|
||||
* @param bool $clearEmpty
|
||||
* @return array
|
||||
*/
|
||||
public function prepareMultiselectValue(string $value, bool $clearEmpty = true): array
|
||||
{
|
||||
$values = explode(',', $value);
|
||||
return $clearEmpty ? array_filter($values) : $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is flag set.
|
||||
*
|
||||
* @param string $path
|
||||
* @param null $scopeCode
|
||||
* @param null $scopeType
|
||||
* @return bool
|
||||
*/
|
||||
public function isSetFlag(string $path, $scopeCode = null, $scopeType = null): bool
|
||||
{
|
||||
if ($scopeType === null) {
|
||||
$scopeType = ScopeInterface::SCOPE_STORE;
|
||||
}
|
||||
return $this->scopeConfig->isSetFlag($path, $scopeType, $scopeCode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
<?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\Debug;
|
||||
|
||||
use Magento\Framework\App\ProductMetadataInterface;
|
||||
use Magento\Framework\App\State;
|
||||
use Magento\Framework\Encryption\EncryptorInterface;
|
||||
use Magento\Framework\Module\ModuleListInterface;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
use Magento\Framework\Util;
|
||||
use Magento\Framework\View\Asset\ConfigInterface;
|
||||
use Magento\Framework\View\Design\Theme\ThemeList;
|
||||
use Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class CollectInfo
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $notConflictingVendors = [
|
||||
'Magento',
|
||||
'Amazon',
|
||||
'Klarna',
|
||||
'MSP',
|
||||
'Temando',
|
||||
'Vertex',
|
||||
'Dotdigitalgroup',
|
||||
'Yotpo',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $notConflictingExtensions = [];
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\State
|
||||
*/
|
||||
private $appState;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Stdlib\DateTime\DateTime
|
||||
*/
|
||||
private $dateTime;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\ProductMetadataInterface
|
||||
*/
|
||||
private $productMetadata;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Module\ModuleListInterface
|
||||
*/
|
||||
private $moduleList;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Encryption\EncryptorInterface
|
||||
*/
|
||||
private $encryptor;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\View\Design\Theme\ThemeList
|
||||
*/
|
||||
private $themeList;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Util
|
||||
*/
|
||||
private $util;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\View\Asset\ConfigInterface
|
||||
*/
|
||||
private $assetConfig;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey
|
||||
*/
|
||||
private $getTrueCustomerKey;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\State $appState
|
||||
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
|
||||
* @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
|
||||
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
|
||||
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
|
||||
* @param \Magento\Framework\View\Design\Theme\ThemeList $themeList
|
||||
* @param \Magento\Framework\Util $util
|
||||
* @param \Magento\Framework\View\Asset\ConfigInterface $assetConfig
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey $getTrueCustomerKey
|
||||
*/
|
||||
public function __construct(
|
||||
State $appState,
|
||||
DateTime $dateTime,
|
||||
ProductMetadataInterface $productMetadata,
|
||||
ModuleListInterface $moduleList,
|
||||
EncryptorInterface $encryptor,
|
||||
ThemeList $themeList,
|
||||
Util $util,
|
||||
ConfigInterface $assetConfig,
|
||||
GetTrueCustomerKey $getTrueCustomerKey
|
||||
) {
|
||||
$this->appState = $appState;
|
||||
$this->dateTime = $dateTime;
|
||||
$this->productMetadata = $productMetadata;
|
||||
$this->moduleList = $moduleList;
|
||||
$this->encryptor = $encryptor;
|
||||
$this->themeList = $themeList;
|
||||
$this->util = $util;
|
||||
$this->assetConfig = $assetConfig;
|
||||
$this->getTrueCustomerKey = $getTrueCustomerKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get debug information.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function execute() : array
|
||||
{
|
||||
return [
|
||||
'core' => $this->getCoreMagentoInfo(),
|
||||
'phpVersion' => $this->util->getTrimmedPhpVersion(),
|
||||
'additional' => [
|
||||
'time' => $this->dateTime->date(),
|
||||
'cssMinification' => $this->assetConfig->isMergeCssFiles(),
|
||||
'jsMinification' => $this->assetConfig->isMergeJsFiles(),
|
||||
'jsBundling' => $this->assetConfig->isBundlingJsFiles(),
|
||||
'minifyHtml' => $this->assetConfig->isMinifyHtml(),
|
||||
'potential_conflicts' => $this->getPotentialConflicts(),
|
||||
'signature' => $this->encryptor->hash($this->getTrueCustomerKey->execute('Base')),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get magento details.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getCoreMagentoInfo() : array
|
||||
{
|
||||
return [
|
||||
'mode' => $this->appState->getMode(),
|
||||
'version' => $this->productMetadata->getVersion(),
|
||||
'name' => $this->productMetadata->getName(),
|
||||
'edition' => $this->productMetadata->getEdition(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get third party extensions and themes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getPotentialConflicts() : array
|
||||
{
|
||||
return [
|
||||
'modules' => $this->getThirdPartyExtensions(),
|
||||
'themes' => $this->getThirdPartyThemes(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of third party extensions.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getThirdPartyExtensions() : array
|
||||
{
|
||||
$notConflictingVendors = $this->notConflictingVendors;
|
||||
$notConflictingExtensions = $this->notConflictingExtensions;
|
||||
|
||||
return array_values(
|
||||
array_filter(
|
||||
$this->moduleList->getNames(),
|
||||
static function ($moduleFullName) use ($notConflictingVendors, $notConflictingExtensions) {
|
||||
$vendor = explode('_', $moduleFullName)[0];
|
||||
if (in_array($vendor, $notConflictingVendors, true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (in_array($moduleFullName, $notConflictingExtensions, true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of third party themes.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getThirdPartyThemes() : array
|
||||
{
|
||||
return array_filter(
|
||||
$this->themeList->getColumnValues('code'),
|
||||
static function ($theme) {
|
||||
return strpos($theme, 'Magento/') !== 0;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?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\Debug;
|
||||
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\App\Response\Http\FileFactory;
|
||||
use Magento\Framework\Serialize\SerializerInterface;
|
||||
use Magento\Framework\Stdlib\DateTime\DateTime;
|
||||
|
||||
/**
|
||||
* Export data for quick debug
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Export
|
||||
{
|
||||
/**
|
||||
* @var DateTime
|
||||
*/
|
||||
private $dateTime;
|
||||
|
||||
/**
|
||||
* @var FileFactory
|
||||
*/
|
||||
private $fileFactory;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Debug\CollectInfo
|
||||
*/
|
||||
private $collectInfo;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Serialize\SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
/**
|
||||
* Export constructor.
|
||||
*
|
||||
* @param \Plumrocket\Base\Model\Debug\CollectInfo $collectInfo
|
||||
* @param \Magento\Framework\Stdlib\DateTime\DateTime $dateTime
|
||||
* @param \Magento\Framework\App\Response\Http\FileFactory $fileFactory
|
||||
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
|
||||
*/
|
||||
public function __construct(
|
||||
CollectInfo $collectInfo,
|
||||
DateTime $dateTime,
|
||||
FileFactory $fileFactory,
|
||||
SerializerInterface $serializer
|
||||
) {
|
||||
$this->dateTime = $dateTime;
|
||||
$this->fileFactory = $fileFactory;
|
||||
$this->collectInfo = $collectInfo;
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create json file with debug info.
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
$date = $this->dateTime->date('Y-m-d_H-i-s');
|
||||
$zipFileName = 'debug_info_' . $date . '.json';
|
||||
|
||||
$debugInfo = $this->collectInfo->execute();
|
||||
|
||||
$this->fileFactory->create(
|
||||
$zipFileName,
|
||||
[
|
||||
'type' => 'string',
|
||||
'value' => $this->serializer->serialize($debugInfo),
|
||||
'rm' => true
|
||||
],
|
||||
DirectoryList::VAR_EXPORT,
|
||||
'json',
|
||||
null
|
||||
);
|
||||
}
|
||||
}
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
<?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\Authorization\Data;
|
||||
|
||||
use Magento\Framework\Model\AbstractModel;
|
||||
use Magento\Framework\Model\Context;
|
||||
use Magento\Framework\Registry;
|
||||
use Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface;
|
||||
use Plumrocket\Base\Helper\Config;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class ExtensionAuthorization extends AbstractModel implements ExtensionAuthorizationInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Helper\Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\Model\Context $context
|
||||
* @param \Magento\Framework\Registry $registry
|
||||
* @param \Plumrocket\Base\Helper\Config $config
|
||||
* @param string $name
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Registry $registry,
|
||||
Config $config,
|
||||
string $name,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $registry, null, null, $data);
|
||||
$this->name = $name;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(\Plumrocket\Base\Model\ResourceModel\ExtensionAuthorization::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getModuleName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isAuthorized(): bool
|
||||
{
|
||||
return $this->getStatus() && ($this->getStatus() % 100 === 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getStatus(): int
|
||||
{
|
||||
return (int) $this->_getData(self::STATUS);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setStatus(int $status): ExtensionAuthorizationInterface
|
||||
{
|
||||
return $this->setData(self::STATUS, $status);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setSignature(string $signature): ExtensionAuthorizationInterface
|
||||
{
|
||||
return $this->setData(self::SIGNATURE, $signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDate(): string
|
||||
{
|
||||
return (string) $this->_getData(self::DATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setDate(string $date): ExtensionAuthorizationInterface
|
||||
{
|
||||
return $this->setData(self::DATE, $date);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isCached(): bool
|
||||
{
|
||||
if ($this->config->isDebugMode()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->getDate() > date('Y-m-d H:i:s')
|
||||
&& $this->getDate() < date('Y-m-d H:i:s', time() + 30 * 86400);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?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\Authorization;
|
||||
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Factory
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\ObjectManagerInterface $objectManager
|
||||
*/
|
||||
public function __construct(ObjectManagerInterface $objectManager)
|
||||
{
|
||||
$this->objectManager = $objectManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $moduleName
|
||||
* @return \Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface|Data\ExtensionAuthorization
|
||||
*/
|
||||
public function create(string $moduleName): ExtensionAuthorizationInterface
|
||||
{
|
||||
return $this->objectManager->create(ExtensionAuthorizationInterface::class, ['name' => $moduleName]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?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\Authorization;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Key
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $keyCache = [];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $keyPathCache = [];
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetExtensionInformationInterface
|
||||
*/
|
||||
private $getExtensionInformation;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Api\GetExtensionInformationInterface $getExtensionInformation
|
||||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
||||
*/
|
||||
public function __construct(
|
||||
GetExtensionInformationInterface $getExtensionInformation,
|
||||
ScopeConfigInterface $scopeConfig
|
||||
) {
|
||||
$this->getExtensionInformation = $getExtensionInformation;
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve key from config
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return string
|
||||
*/
|
||||
public function get(string $moduleName): string
|
||||
{
|
||||
if (! isset($this->keyCache[$moduleName]) || ! $this->keyCache[$moduleName]) {
|
||||
$this->keyCache[$moduleName] = (string) $this->scopeConfig->getValue(
|
||||
$this->getPath($moduleName),
|
||||
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
|
||||
0
|
||||
);
|
||||
}
|
||||
|
||||
return $this->keyCache[$moduleName];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set key to cache.
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @param string $key
|
||||
* @return $this
|
||||
*/
|
||||
public function set(string $moduleName, string $key): Key
|
||||
{
|
||||
$this->keyCache[$moduleName] = $key;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve config path to key
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return string
|
||||
*/
|
||||
public function getPath(string $moduleName): string
|
||||
{
|
||||
if (! isset($this->keyPathCache[$moduleName])) {
|
||||
$info = $this->getExtensionInformation->execute($moduleName);
|
||||
if ($configSection = $info->getConfigSection()) {
|
||||
$path = "$configSection/general/serial";
|
||||
} else {
|
||||
$path = 'custom/general/serial';
|
||||
}
|
||||
$this->keyPathCache[$moduleName] = $path;
|
||||
}
|
||||
|
||||
return $this->keyPathCache[$moduleName];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?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\Authorization\Key;
|
||||
|
||||
use Exception;
|
||||
use Magento\Framework\App\ProductMetadataInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface;
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
use Plumrocket\Base\Helper\Config;
|
||||
use Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey;
|
||||
use Plumrocket\Base\Model\External\Connector;
|
||||
use Plumrocket\Base\Model\External\Urls;
|
||||
use Plumrocket\Base\Model\Utils\GetEnabledStoresUrls;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Load
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Utils\GetEnabledStoresUrls
|
||||
*/
|
||||
private $getEnabledStoresUrls;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetExtensionInformationInterface
|
||||
*/
|
||||
private $getExtensionInformation;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey
|
||||
*/
|
||||
private $getTrueCustomerKey;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\ProductMetadataInterface
|
||||
*/
|
||||
private $productMetadata;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\External\Connector
|
||||
*/
|
||||
private $externalConnector;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Helper\Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Utils\GetEnabledStoresUrls $getEnabledStoresUrls
|
||||
* @param \Plumrocket\Base\Api\GetExtensionInformationInterface $getExtensionInformation
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey $getTrueCustomerKey
|
||||
* @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
|
||||
* @param \Plumrocket\Base\Model\External\Connector $externalConnector
|
||||
* @param \Plumrocket\Base\Helper\Config $config
|
||||
*/
|
||||
public function __construct(
|
||||
GetEnabledStoresUrls $getEnabledStoresUrls,
|
||||
GetExtensionInformationInterface $getExtensionInformation,
|
||||
GetTrueCustomerKey $getTrueCustomerKey,
|
||||
ProductMetadataInterface $productMetadata,
|
||||
Connector $externalConnector,
|
||||
Config $config
|
||||
) {
|
||||
$this->getEnabledStoresUrls = $getEnabledStoresUrls;
|
||||
$this->getExtensionInformation = $getExtensionInformation;
|
||||
$this->getTrueCustomerKey = $getTrueCustomerKey;
|
||||
$this->productMetadata = $productMetadata;
|
||||
$this->externalConnector = $externalConnector;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load extension key.
|
||||
*
|
||||
* @param \Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface $extension
|
||||
* @return array
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function execute(ExtensionAuthorizationInterface $extension): array
|
||||
{
|
||||
$extensionInformation = $this->getExtensionInformation->execute($extension->getModuleName());
|
||||
$key = '';
|
||||
$expiration = 0;
|
||||
try {
|
||||
$data = [
|
||||
'edition' => $this->productMetadata->getEdition(),
|
||||
'base_urls' => $this->getEnabledStoresUrls->execute(),
|
||||
'name' => $extensionInformation->getModuleName(),
|
||||
'name_version' => $extensionInformation->getInstalledVersion(),
|
||||
'customer' => $this->getTrueCustomerKey->execute($extension->getModuleName()),
|
||||
'title' => $extensionInformation->getTitle(),
|
||||
'platform' => 'm2',
|
||||
];
|
||||
|
||||
$xml = $this->externalConnector->connect('https://' . Urls::PINGBACK_URL . '/session/', $data);
|
||||
$cacheTime = (int) ($res['cache_time'] ?? 0);
|
||||
if ($cacheTime > 0) {
|
||||
$expiration = $cacheTime;
|
||||
}
|
||||
$key = $xml['data'] ?? '';
|
||||
} catch (Exception $e) {
|
||||
if ($this->config->isDebugMode()) {
|
||||
throw new LocalizedException(__($e->getMessage()), null, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return [$key, $expiration];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
<?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\Authorization\Key;
|
||||
|
||||
use Magento\Config\Model\ResourceModel\Config;
|
||||
use Magento\Framework\App\Cache\TypeListInterface;
|
||||
use Magento\Framework\Event\ManagerInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface;
|
||||
use Plumrocket\Base\Model\Extension\Authorization\Key;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Update
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Key\Load
|
||||
*/
|
||||
private $loadKey;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Status\Update
|
||||
*/
|
||||
private $updateStatus;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Key
|
||||
*/
|
||||
private $key;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Cache\TypeListInterface
|
||||
*/
|
||||
private $cacheTypeList;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Event\ManagerInterface
|
||||
*/
|
||||
private $eventManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Config\Model\ResourceModel\Config
|
||||
*/
|
||||
private $resourceModelConfig;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Helper\Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Key\Load $loadKey
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Status\Update $updateStatus
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Key $key
|
||||
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
|
||||
* @param \Magento\Framework\Event\ManagerInterface $eventManager
|
||||
* @param \Magento\Config\Model\ResourceModel\Config $resourceModelConfig
|
||||
* @param \Plumrocket\Base\Helper\Config $config\
|
||||
*/
|
||||
public function __construct(
|
||||
Load $loadKey,
|
||||
\Plumrocket\Base\Model\Extension\Authorization\Status\Update $updateStatus,
|
||||
Key $key,
|
||||
TypeListInterface $cacheTypeList,
|
||||
ManagerInterface $eventManager,
|
||||
Config $resourceModelConfig,
|
||||
\Plumrocket\Base\Helper\Config $config
|
||||
) {
|
||||
$this->loadKey = $loadKey;
|
||||
$this->updateStatus = $updateStatus;
|
||||
$this->key = $key;
|
||||
$this->cacheTypeList = $cacheTypeList;
|
||||
$this->eventManager = $eventManager;
|
||||
$this->resourceModelConfig = $resourceModelConfig;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load new key and update status
|
||||
*
|
||||
* @param \Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface $extension
|
||||
* @return \Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface
|
||||
*/
|
||||
public function execute(ExtensionAuthorizationInterface $extension): ExtensionAuthorizationInterface
|
||||
{
|
||||
try {
|
||||
list($key, $expirationDays) = $this->loadKey->execute($extension);
|
||||
$this->key->set($extension->getModuleName(), $key);
|
||||
|
||||
$this->resourceModelConfig->saveConfig(
|
||||
$this->key->getPath($extension->getModuleName()),
|
||||
$key,
|
||||
'default',
|
||||
0
|
||||
);
|
||||
|
||||
// clear the config cache
|
||||
$this->cacheTypeList->cleanType('config');
|
||||
$this->eventManager->dispatch('adminhtml_cache_refresh_type', ['type' => 'config']);
|
||||
|
||||
return $this->updateStatus->execute($extension, null, $expirationDays);
|
||||
} catch (LocalizedException $localizedException) {
|
||||
if ($this->config->isDebugMode()) {
|
||||
throw $localizedException;
|
||||
}
|
||||
}
|
||||
|
||||
return $extension;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?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\Authorization;
|
||||
|
||||
use Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Message
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Key
|
||||
*/
|
||||
private $authorizationKey;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Key $authorizationKey
|
||||
*/
|
||||
public function __construct(Key $authorizationKey)
|
||||
{
|
||||
$this->authorizationKey = $authorizationKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive product description
|
||||
*
|
||||
* @param \Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface $authorization
|
||||
* @return string
|
||||
*/
|
||||
public function get(ExtensionAuthorizationInterface $authorization): string
|
||||
{
|
||||
if ($authorization->isAuthorized()) {
|
||||
return 'Congratulations! Your serial key is now activated. ' .
|
||||
'Thank you for choosing Plumrocket Inc as your Magento extension provider!';
|
||||
}
|
||||
|
||||
if (! $this->authorizationKey->get($authorization->getModuleName())) {
|
||||
return 'Serial key is missing. Please login to your account at ' .
|
||||
'<a target="_blank" href="https://plumrocket.com/downloadable/customer/products">' .
|
||||
'https://plumrocket.com' .
|
||||
'</a> to copy your serial key for this product. ' .
|
||||
'Read this <a target="_blank" href="https://plumrocket.com/docs/general/magento-license-installation">' .
|
||||
'wiki article</a> for more info.';
|
||||
}
|
||||
|
||||
if (! $authorization->isAuthorized()) {
|
||||
if ($authorization->getStatus() === 503) {
|
||||
return 'Your serial key is not valid for Magento Enterprise Edition. ' .
|
||||
'Please purchase Magento Enterprise Edition license for this product at ' .
|
||||
'<a href="https://plumrocket.com" target="_blank">https://plumrocket.com</a>';
|
||||
}
|
||||
|
||||
return 'Serial key is not valid for this domain. Please go to ' .
|
||||
'<a href="https://plumrocket.com" target="_blank">https://plumrocket.com</a> ' .
|
||||
'to purchase new license for live site. ' .
|
||||
'Testing or development subdomains can be added to your license free of charge. Read this ' .
|
||||
'<a href="https://plumrocket.com/docs/general/updating-license-domains" target="_blank">wiki ' .
|
||||
'article</a> for more info.';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
<?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\Authorization;
|
||||
|
||||
use Exception;
|
||||
use Magento\Backend\Model\Auth\Session;
|
||||
use Magento\Framework\App\CacheInterface;
|
||||
use Magento\Framework\App\ProductMetadataInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Util;
|
||||
use Plumrocket\Base\Model\Extension\GetInstallationType;
|
||||
use Plumrocket\Base\Api\ExtensionAuthorizationRepositoryInterface;
|
||||
use Plumrocket\Base\Api\ExtensionInformationListInterface;
|
||||
use Plumrocket\Base\Helper\Config;
|
||||
use Plumrocket\Base\Model\Extension\Authorization\Status\Calculate;
|
||||
use Plumrocket\Base\Model\Extension\Authorization\Status\Update;
|
||||
use Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey;
|
||||
use Plumrocket\Base\Model\Extension\Status\Disable;
|
||||
use Plumrocket\Base\Model\Extension\Status\IsEnabledOnAnyStoreView;
|
||||
use Plumrocket\Base\Model\External\Connector;
|
||||
use Plumrocket\Base\Model\External\Urls;
|
||||
use Plumrocket\Base\Model\Utils\GetEnabledStoresUrls;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Reindex
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Backend\Model\Auth\Session
|
||||
*/
|
||||
private $backendAuthSession;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Helper\Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\CacheInterface
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\ProductMetadataInterface
|
||||
*/
|
||||
private $productMetadata;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Utils\GetEnabledStoresUrls
|
||||
*/
|
||||
private $getEnabledStoresUrls;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\ExtensionInformationListInterface
|
||||
*/
|
||||
private $extensionInformationList;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\ExtensionAuthorizationRepositoryInterface
|
||||
*/
|
||||
private $extensionAuthorizationRepository;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Status\IsEnabledOnAnyStoreView
|
||||
*/
|
||||
private $isEnabledOnAnyStoreView;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey
|
||||
*/
|
||||
private $getTrueCustomerKey;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Key
|
||||
*/
|
||||
private $authorizationKey;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\External\Connector
|
||||
*/
|
||||
private $externalConnector;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Status\Calculate
|
||||
*/
|
||||
private $calculateStatus;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Factory
|
||||
*/
|
||||
private $extensionAuthorizationFactory;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Status\Update
|
||||
*/
|
||||
private $updateStatus;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Status\Disable
|
||||
*/
|
||||
private $disableExtension;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetInstallationType
|
||||
*/
|
||||
private $getInstallType;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Util
|
||||
*/
|
||||
private $magentoUtil;
|
||||
|
||||
/**
|
||||
* @param \Magento\Backend\Model\Auth\Session $backendAuthSession
|
||||
* @param \Plumrocket\Base\Helper\Config $config
|
||||
* @param \Magento\Framework\App\CacheInterface $cache
|
||||
* @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
|
||||
* @param \Plumrocket\Base\Model\Utils\GetEnabledStoresUrls $getEnabledStoresUrls
|
||||
* @param \Plumrocket\Base\Api\ExtensionInformationListInterface $extensionInformationList
|
||||
* @param \Plumrocket\Base\Api\ExtensionAuthorizationRepositoryInterface $extensionAuthorizationRepository
|
||||
* @param \Plumrocket\Base\Model\Extension\Status\IsEnabledOnAnyStoreView $isEnabledOnAnyStoreView
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey $getTrueCustomerKey
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Key $authorizationKey
|
||||
* @param \Plumrocket\Base\Model\External\Connector $externalConnector
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Status\Calculate $calculateStatus
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Factory $extensionAuthorizationFactory
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Status\Update $updateStatus
|
||||
* @param \Plumrocket\Base\Model\Extension\Status\Disable $disableExtension
|
||||
* @param \Plumrocket\Base\Model\Extension\GetInstallationType $getInstallType
|
||||
* @param \Magento\Framework\Util $magentoUtil
|
||||
*/
|
||||
public function __construct(
|
||||
Session $backendAuthSession,
|
||||
Config $config,
|
||||
CacheInterface $cache,
|
||||
ProductMetadataInterface $productMetadata,
|
||||
GetEnabledStoresUrls $getEnabledStoresUrls,
|
||||
ExtensionInformationListInterface $extensionInformationList,
|
||||
ExtensionAuthorizationRepositoryInterface $extensionAuthorizationRepository,
|
||||
IsEnabledOnAnyStoreView $isEnabledOnAnyStoreView,
|
||||
GetTrueCustomerKey $getTrueCustomerKey,
|
||||
Key $authorizationKey,
|
||||
Connector $externalConnector,
|
||||
Calculate $calculateStatus,
|
||||
Factory $extensionAuthorizationFactory,
|
||||
Update $updateStatus,
|
||||
Disable $disableExtension,
|
||||
GetInstallationType $getInstallType,
|
||||
Util $magentoUtil
|
||||
) {
|
||||
$this->backendAuthSession = $backendAuthSession;
|
||||
$this->config = $config;
|
||||
$this->cache = $cache;
|
||||
$this->productMetadata = $productMetadata;
|
||||
$this->getEnabledStoresUrls = $getEnabledStoresUrls;
|
||||
$this->extensionInformationList = $extensionInformationList;
|
||||
$this->extensionAuthorizationRepository = $extensionAuthorizationRepository;
|
||||
$this->isEnabledOnAnyStoreView = $isEnabledOnAnyStoreView;
|
||||
$this->getTrueCustomerKey = $getTrueCustomerKey;
|
||||
$this->authorizationKey = $authorizationKey;
|
||||
$this->externalConnector = $externalConnector;
|
||||
$this->calculateStatus = $calculateStatus;
|
||||
$this->extensionAuthorizationFactory = $extensionAuthorizationFactory;
|
||||
$this->updateStatus = $updateStatus;
|
||||
$this->disableExtension = $disableExtension;
|
||||
$this->getInstallType = $getInstallType;
|
||||
$this->magentoUtil = $magentoUtil;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reindex installed extensions.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function execute(): void
|
||||
{
|
||||
$time = time();
|
||||
$session = $this->backendAuthSession;
|
||||
|
||||
$cacheKey = 'Plumrocket_base_reindex';
|
||||
|
||||
if (! $session->isLoggedIn()
|
||||
|| (86400 + $session->getPBProductReindex() > $time)
|
||||
|| (86400 + ((int) $this->cache->load($cacheKey)) > $time)
|
||||
) {
|
||||
if (! $this->config->isDebugMode()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$data = [
|
||||
'edition' => $this->productMetadata->getEdition(),
|
||||
'products' => [],
|
||||
'base_urls' => $this->getEnabledStoresUrls->execute(),
|
||||
'platform' => 'm2',
|
||||
'magento_version' => $this->productMetadata->getVersion(),
|
||||
'php_version' => $this->magentoUtil->getTrimmedPhpVersion(),
|
||||
];
|
||||
$activeModules = [];
|
||||
foreach ($this->extensionInformationList->getList()->getItems() as $extensionInformation) {
|
||||
$moduleName = $extensionInformation->getModuleName();
|
||||
try {
|
||||
$isCached = $this->extensionAuthorizationRepository->get($moduleName)->isCached();
|
||||
} catch (NoSuchEntityException $e) {
|
||||
$isCached = false;
|
||||
}
|
||||
|
||||
if ($isCached || ! $this->isEnabledOnAnyStoreView->execute($moduleName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$activeModules[$moduleName] = $moduleName;
|
||||
$data['products'][$moduleName] = [
|
||||
$moduleName,
|
||||
$extensionInformation->getInstalledVersion(),
|
||||
$this->getTrueCustomerKey->execute($moduleName) ?: 0,
|
||||
$this->authorizationKey->get($moduleName) ?: 0,
|
||||
$extensionInformation->getTitle(),
|
||||
$this->getInstallType->execute($moduleName),
|
||||
];
|
||||
}
|
||||
|
||||
if (count($activeModules)) {
|
||||
try {
|
||||
$xml = $this->externalConnector->connect('https://' . Urls::PINGBACK_URL . '/extensions/', $data);
|
||||
if (! isset($xml['statuses'])) {
|
||||
throw new LocalizedException(__('Statuses are missing.'), null, 1);
|
||||
}
|
||||
$statuses = $xml['statuses'];
|
||||
} catch (Exception $e) {
|
||||
if ($this->config->isDebugMode()) {
|
||||
throw new LocalizedException(__($e->getMessage()), null, 1);
|
||||
}
|
||||
$statuses = [];
|
||||
foreach ($activeModules as $moduleName) {
|
||||
$statuses[$moduleName] = $this->calculateStatus->execute($moduleName);
|
||||
}
|
||||
}
|
||||
foreach ($activeModules as $moduleName) {
|
||||
try {
|
||||
$authorization = $this->extensionAuthorizationRepository->get($moduleName);
|
||||
} catch (NoSuchEntityException $e) {
|
||||
$authorization = $this->extensionAuthorizationFactory->create($moduleName);
|
||||
}
|
||||
|
||||
$status = $statuses[$moduleName] ?? 301;
|
||||
$authorization = $this->updateStatus->execute($authorization, $status);
|
||||
if (! $authorization->isAuthorized()) {
|
||||
$this->disableExtension->execute($authorization->getModuleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->cache->save($time, $cacheKey);
|
||||
$session->setPBProductReindex($time);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?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\Authorization;
|
||||
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface;
|
||||
use Plumrocket\Base\Api\ExtensionAuthorizationRepositoryInterface;
|
||||
use Plumrocket\Base\Model\ResourceModel\ExtensionAuthorization;
|
||||
|
||||
class Repository implements ExtensionAuthorizationRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @var ExtensionAuthorizationInterface[]
|
||||
*/
|
||||
private $cache = [];
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Signature
|
||||
*/
|
||||
private $signature;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Factory
|
||||
*/
|
||||
private $extensionAuthorizationFactory;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\ResourceModel\ExtensionAuthorization
|
||||
*/
|
||||
private $resourceModel;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Signature $signature
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Factory $extensionAuthorizationFactory
|
||||
* @param \Plumrocket\Base\Model\ResourceModel\ExtensionAuthorization $resourceModel
|
||||
*/
|
||||
public function __construct(
|
||||
Signature $signature,
|
||||
Factory $extensionAuthorizationFactory,
|
||||
ExtensionAuthorization $resourceModel
|
||||
) {
|
||||
$this->signature = $signature;
|
||||
$this->extensionAuthorizationFactory = $extensionAuthorizationFactory;
|
||||
$this->resourceModel = $resourceModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function get(string $moduleName): ExtensionAuthorizationInterface
|
||||
{
|
||||
if (! isset($this->cache[$moduleName])) {
|
||||
$this->resourceModel->deleteOld(); // remove old records before loading
|
||||
$signature = $this->signature->create($moduleName);
|
||||
$authorization = $this->extensionAuthorizationFactory->create($moduleName);
|
||||
$this->resourceModel->load($authorization, $signature, ExtensionAuthorizationInterface::SIGNATURE);
|
||||
if (! $authorization->getId()) {
|
||||
throw new NoSuchEntityException(
|
||||
__("The extension authorization that was requested doesn't exist. Verify the module and try again.")
|
||||
);
|
||||
}
|
||||
|
||||
$this->cache[$moduleName] = $authorization;
|
||||
}
|
||||
return $this->cache[$moduleName];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface|Data\ExtensionAuthorization $extension
|
||||
* @return \Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface
|
||||
* @throws \Magento\Framework\Exception\AlreadyExistsException
|
||||
* @throws \Magento\Framework\Exception\NoSuchEntityException
|
||||
*/
|
||||
public function save(ExtensionAuthorizationInterface $extension): ExtensionAuthorizationInterface
|
||||
{
|
||||
unset($this->cache[$extension->getModuleName()]);
|
||||
$this->resourceModel->save($extension);
|
||||
return $this->get($extension->getModuleName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?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\Authorization;
|
||||
|
||||
use Magento\Framework\Encryption\Encryptor;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Signature
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Key
|
||||
*/
|
||||
private $key;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Encryption\Encryptor
|
||||
*/
|
||||
private $encryptor;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Key $key
|
||||
* @param \Magento\Framework\Encryption\Encryptor $encryptor
|
||||
*/
|
||||
public function __construct(Key $key, Encryptor $encryptor)
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->encryptor = $encryptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $moduleName
|
||||
* @return string
|
||||
*/
|
||||
public function create(string $moduleName): string
|
||||
{
|
||||
return $this->encryptor->hash(
|
||||
$moduleName . $this->key->get($moduleName),
|
||||
Encryptor::HASH_VERSION_MD5
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?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\Authorization\Status;
|
||||
|
||||
use Plumrocket\Base\Model\Extension\Authorization\Key;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Calculate
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Key
|
||||
*/
|
||||
private $key;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Key $key
|
||||
*/
|
||||
public function __construct(Key $key)
|
||||
{
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check key.
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return int
|
||||
*/
|
||||
public function execute(string $moduleName): int
|
||||
{
|
||||
return strlen($this->key->get($moduleName)) === 32 ? 500 : 201;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
<?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\Authorization\Status;
|
||||
|
||||
use Exception;
|
||||
use Magento\Framework\App\ProductMetadataInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Util;
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
use Plumrocket\Base\Helper\Config;
|
||||
use Plumrocket\Base\Model\Extension\Authorization\Key;
|
||||
use Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey;
|
||||
use Plumrocket\Base\Model\External\Connector;
|
||||
use Plumrocket\Base\Model\External\Urls;
|
||||
use Plumrocket\Base\Model\Utils\GetEnabledStoresUrls;
|
||||
use Plumrocket\Base\Model\Extension\GetInstallationType;
|
||||
|
||||
/**
|
||||
* Get status from store
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Load
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Utils\GetEnabledStoresUrls
|
||||
*/
|
||||
private $getEnabledStoresUrls;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetExtensionInformationInterface
|
||||
*/
|
||||
private $getExtensionInformation;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey
|
||||
*/
|
||||
private $getTrueCustomerKey;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\ProductMetadataInterface
|
||||
*/
|
||||
private $productMetadata;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\External\Connector
|
||||
*/
|
||||
private $externalConnector;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Helper\Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Key
|
||||
*/
|
||||
private $authorizationKey;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Status\Calculate
|
||||
*/
|
||||
private $calculateStatus;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetInstallationType
|
||||
*/
|
||||
private $getInstallType;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Util
|
||||
*/
|
||||
private $magentoUtil;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Utils\GetEnabledStoresUrls $getEnabledStoresUrls
|
||||
* @param \Plumrocket\Base\Api\GetExtensionInformationInterface $getExtensionInformation
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey $getTrueCustomerKey
|
||||
* @param \Magento\Framework\App\ProductMetadataInterface $productMetadata
|
||||
* @param \Plumrocket\Base\Model\External\Connector $externalConnector
|
||||
* @param \Plumrocket\Base\Helper\Config $config
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Key $authorizationKey
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Status\Calculate $calculateStatus
|
||||
* @param \Plumrocket\Base\Model\Extension\GetInstallationType $getInstallType
|
||||
* @param \Magento\Framework\Util $magentoUtil
|
||||
*/
|
||||
public function __construct(
|
||||
GetEnabledStoresUrls $getEnabledStoresUrls,
|
||||
GetExtensionInformationInterface $getExtensionInformation,
|
||||
GetTrueCustomerKey $getTrueCustomerKey,
|
||||
ProductMetadataInterface $productMetadata,
|
||||
Connector $externalConnector,
|
||||
Config $config,
|
||||
Key $authorizationKey,
|
||||
Calculate $calculateStatus,
|
||||
GetInstallationType $getInstallType,
|
||||
Util $magentoUtil
|
||||
) {
|
||||
$this->getEnabledStoresUrls = $getEnabledStoresUrls;
|
||||
$this->getExtensionInformation = $getExtensionInformation;
|
||||
$this->getTrueCustomerKey = $getTrueCustomerKey;
|
||||
$this->productMetadata = $productMetadata;
|
||||
$this->externalConnector = $externalConnector;
|
||||
$this->config = $config;
|
||||
$this->authorizationKey = $authorizationKey;
|
||||
$this->calculateStatus = $calculateStatus;
|
||||
$this->getInstallType = $getInstallType;
|
||||
$this->magentoUtil = $magentoUtil;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load extension status.
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return int
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function execute(string $moduleName): int
|
||||
{
|
||||
$extensionInformation = $this->getExtensionInformation->execute($moduleName);
|
||||
try {
|
||||
$params = [
|
||||
'edition' => $this->productMetadata->getEdition(),
|
||||
'session' => $this->authorizationKey->get($moduleName),
|
||||
'base_urls' => $this->getEnabledStoresUrls->execute(),
|
||||
'name' => $extensionInformation->getModuleName(),
|
||||
'name_version' => $extensionInformation->getInstalledVersion(),
|
||||
'customer' => $this->getTrueCustomerKey->execute($moduleName),
|
||||
'title' => $extensionInformation->getTitle(),
|
||||
'platform' => 'm2',
|
||||
'magento_version' => $this->productMetadata->getVersion(),
|
||||
'installation' => $this->getInstallType->execute($moduleName),
|
||||
'php_version' => $this->magentoUtil->getTrimmedPhpVersion(),
|
||||
];
|
||||
|
||||
$xml = $this->externalConnector->connect('https://' . Urls::PINGBACK_URL . '/extension/', $params);
|
||||
if (empty($xml['status'])) {
|
||||
throw new LocalizedException(__('Status is missing.'), null, 1);
|
||||
}
|
||||
$status = (int) $xml['status'];
|
||||
} catch (Exception $e) {
|
||||
if ($this->config->isDebugMode()) {
|
||||
throw new LocalizedException(__($e->getMessage()), null, 1);
|
||||
}
|
||||
$status = $this->calculateStatus->execute($moduleName);
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?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\Authorization\Status;
|
||||
|
||||
use Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface;
|
||||
|
||||
/**
|
||||
* Get new status from store
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Refresh
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Status\Load
|
||||
*/
|
||||
private $loadStatus;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Status\Update
|
||||
*/
|
||||
private $updateStatus;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Status\Load $loadStatus
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Status\Update $updateStatus
|
||||
*/
|
||||
public function __construct(
|
||||
Load $loadStatus,
|
||||
Update $updateStatus
|
||||
) {
|
||||
$this->loadStatus = $loadStatus;
|
||||
$this->updateStatus = $updateStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface $extensionAuthorization
|
||||
* @return mixed|string
|
||||
*/
|
||||
public function execute(ExtensionAuthorizationInterface $extensionAuthorization)
|
||||
{
|
||||
$status = $this->loadStatus->execute($extensionAuthorization->getModuleName());
|
||||
if ($extensionAuthorization->getStatus() !== $status) {
|
||||
$this->updateStatus->execute($extensionAuthorization, $status);
|
||||
}
|
||||
|
||||
return $extensionAuthorization;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?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\Authorization\Status;
|
||||
|
||||
use Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface;
|
||||
use Plumrocket\Base\Api\ExtensionAuthorizationRepositoryInterface;
|
||||
use Plumrocket\Base\Model\Extension\Authorization\Signature;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Update
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Signature
|
||||
*/
|
||||
private $signature;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\ExtensionAuthorizationRepositoryInterface
|
||||
*/
|
||||
private $extensionAuthorizationRepository;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Authorization\Status\Calculate
|
||||
*/
|
||||
private $calculateStatus;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Signature $signature
|
||||
* @param \Plumrocket\Base\Api\ExtensionAuthorizationRepositoryInterface $extensionAuthorizationRepository
|
||||
* @param \Plumrocket\Base\Model\Extension\Authorization\Status\Calculate $calculateStatus
|
||||
*/
|
||||
public function __construct(
|
||||
Signature $signature,
|
||||
ExtensionAuthorizationRepositoryInterface $extensionAuthorizationRepository,
|
||||
Calculate $calculateStatus
|
||||
) {
|
||||
$this->signature = $signature;
|
||||
$this->extensionAuthorizationRepository = $extensionAuthorizationRepository;
|
||||
$this->calculateStatus = $calculateStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface $authorization
|
||||
* @param int $cacheLifetime
|
||||
* @param int|null $status
|
||||
* @return \Plumrocket\Base\Api\Data\ExtensionAuthorizationInterface
|
||||
*/
|
||||
public function execute(
|
||||
ExtensionAuthorizationInterface $authorization,
|
||||
?int $status = null,
|
||||
int $cacheLifetime = 3
|
||||
): ExtensionAuthorizationInterface {
|
||||
$authorization->setSignature($this->signature->create($authorization->getModuleName()));
|
||||
$authorization->setStatus($status ?? $this->calculateStatus->execute($authorization->getModuleName()));
|
||||
$authorization->setDate(date('Y-m-d H:i:s', time() + $cacheLifetime * 86400));
|
||||
return $this->extensionAuthorizationRepository->save($authorization);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?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\Config;
|
||||
|
||||
use Magento\Framework\Config\ConverterInterface;
|
||||
use Magento\Framework\Data\Argument\Interpreter\Boolean as BooleanInterpreter;
|
||||
use Plumrocket\Base\Api\Data\ExtensionInformationInterface;
|
||||
|
||||
class Converter implements ConverterInterface
|
||||
{
|
||||
/**
|
||||
* @var BooleanInterpreter
|
||||
*/
|
||||
private $booleanInterpreter;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\Data\Argument\Interpreter\Boolean $booleanInterpreter
|
||||
*/
|
||||
public function __construct(BooleanInterpreter $booleanInterpreter)
|
||||
{
|
||||
$this->booleanInterpreter = $booleanInterpreter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert dom node tree to array
|
||||
*
|
||||
* @param \DOMDocument $source
|
||||
* @return array
|
||||
*/
|
||||
public function convert($source)
|
||||
{
|
||||
$output = [];
|
||||
/** @var \DOMNodeList $extensions */
|
||||
$extensions = $source->getElementsByTagName('extension');
|
||||
/** @var \DOMNode $extension */
|
||||
foreach ($extensions as $extension) {
|
||||
$extensionConfig = [
|
||||
ExtensionInformationInterface::FIELD_CONFIG_SECTION => '',
|
||||
ExtensionInformationInterface::FIELD_IS_ENABLED_PATH => '',
|
||||
ExtensionInformationInterface::FIELD_DOCUMENTATION => '',
|
||||
ExtensionInformationInterface::FIELD_WIKI => '',
|
||||
ExtensionInformationInterface::FIELD_IS_SERVICE => false,
|
||||
ExtensionInformationInterface::FIELD_URL => '',
|
||||
ExtensionInformationInterface::FIELD_MARKETPLACE_URL => '',
|
||||
'customer' => ['key' => ''],
|
||||
'metapackage' => ['composer_name' => '', 'directory' => ''],
|
||||
];
|
||||
/** @var \DOMAttr $attribute */
|
||||
foreach ($extension->attributes as $attribute) {
|
||||
$value = $attribute->nodeValue;
|
||||
if ($attribute->nodeName === ExtensionInformationInterface::FIELD_IS_SERVICE) {
|
||||
$value = $this->booleanInterpreter->evaluate(['value' => $value]);
|
||||
}
|
||||
$extensionConfig[$attribute->nodeName] = $value;
|
||||
}
|
||||
|
||||
$extensionConfig = $this->addCustomerData($extension, $extensionConfig);
|
||||
|
||||
$output[$extension->attributes->getNamedItem('name')->nodeValue] = $extensionConfig;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
private function addCustomerData(\DOMNode $extension, array $extensionConfig): array
|
||||
{
|
||||
if ($extension->hasChildNodes()) {
|
||||
/** @var \DOMNode $childNode */
|
||||
foreach ($extension->childNodes as $childNode) {
|
||||
if ($childNode->nodeType === XML_ELEMENT_NODE) {
|
||||
/** @var \DOMAttr $attribute */
|
||||
foreach ($childNode->attributes as $attribute) {
|
||||
$extensionConfig[$childNode->nodeName][$attribute->nodeName] = $attribute->nodeValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $extensionConfig;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?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\Customer;
|
||||
|
||||
use Magento\Framework\Config\DataInterface;
|
||||
|
||||
/**
|
||||
* Reads customer key from pr_extensions.xml
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class GetKey
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Config\DataInterface
|
||||
*/
|
||||
private $extensionsConfig;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\Config\DataInterface $extensionsConfig
|
||||
*/
|
||||
public function __construct(
|
||||
DataInterface $extensionsConfig
|
||||
) {
|
||||
$this->extensionsConfig = $extensionsConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get customer key.
|
||||
*
|
||||
* @param string $moduleName e.g. SocialLoginFree
|
||||
* @return string
|
||||
*/
|
||||
public function execute(string $moduleName): string
|
||||
{
|
||||
return (string) $this->extensionsConfig->get("$moduleName/customer/key");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?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\Customer;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Plumrocket\Base\Model\Extension\GetModuleName;
|
||||
|
||||
/**
|
||||
* Retrieve true customer key when customer had bought extension from marketplace
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class GetTrueCustomerKey
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\GetKey
|
||||
*/
|
||||
private $getKey;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\IsMarketplaceKey
|
||||
*/
|
||||
private $isMarketplaceKey;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetModuleName
|
||||
*/
|
||||
private $getModuleName;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\GetKey $getKey
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\IsMarketplaceKey $isMarketplaceKey
|
||||
* @param \Plumrocket\Base\Model\Extension\GetModuleName $getModuleName
|
||||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
||||
*/
|
||||
public function __construct(
|
||||
GetKey $getKey,
|
||||
IsMarketplaceKey $isMarketplaceKey,
|
||||
GetModuleName $getModuleName,
|
||||
ScopeConfigInterface $scopeConfig
|
||||
) {
|
||||
$this->getKey = $getKey;
|
||||
$this->isMarketplaceKey = $isMarketplaceKey;
|
||||
$this->getModuleName = $getModuleName;
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $moduleName e.g. SocialLoginFree or Plumrocket_SocialLoginFree
|
||||
* @return string
|
||||
*/
|
||||
public function execute(string $moduleName): string
|
||||
{
|
||||
$moduleName = $this->getModuleName->execute($moduleName);
|
||||
$customerKey = $this->getKey->execute($moduleName);
|
||||
if ($this->isMarketplaceKey->execute($customerKey)) {
|
||||
$customerKey = $this->getFromConfig($moduleName) ?: $customerKey;
|
||||
}
|
||||
|
||||
return $customerKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $moduleName e.g. SocialLoginFree
|
||||
* @return string
|
||||
*/
|
||||
private function getFromConfig(string $moduleName): string
|
||||
{
|
||||
return (string) $this->scopeConfig->getValue("$moduleName/module/data");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?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\Customer;
|
||||
|
||||
/**
|
||||
* Check whether customer key is marketplace
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class IsMarketplaceKey
|
||||
{
|
||||
const MARKETPLACE_CUSTOMER_KEY = '532416486b540ea2a1e50c4070b671611b44f52718';
|
||||
|
||||
public function execute(string $customerKey): bool
|
||||
{
|
||||
return self::MARKETPLACE_CUSTOMER_KEY === $customerKey;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2022 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Extension;
|
||||
|
||||
use Magento\Framework\Module\Dir\Reader;
|
||||
use Magento\Framework\Module\Dir;
|
||||
|
||||
/**
|
||||
* @since 2.8.4
|
||||
*/
|
||||
class GetInstallationType
|
||||
{
|
||||
public const NON_COMPOSER_EXTENSION_PATH = 'app/code/Plumrocket';
|
||||
|
||||
public const NON_COMPOSER_INSTALL_TYPE = 'non-composer';
|
||||
|
||||
public const COMPOSER_INSTALL_TYPE = 'composer';
|
||||
|
||||
/**
|
||||
* @var Reader
|
||||
*/
|
||||
private $moduleDirReader;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetModuleName
|
||||
*/
|
||||
private $getModuleName;
|
||||
|
||||
/**
|
||||
* @param Reader $moduleDirReader
|
||||
* @param \Plumrocket\Base\Model\Extension\GetModuleName $getModuleName
|
||||
*/
|
||||
public function __construct(
|
||||
Reader $moduleDirReader,
|
||||
GetModuleName $getModuleName
|
||||
) {
|
||||
$this->moduleDirReader = $moduleDirReader;
|
||||
$this->getModuleName = $getModuleName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect installation type of the Plumrocket extensions
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return string
|
||||
*/
|
||||
public function execute(string $moduleName): string
|
||||
{
|
||||
$moduleName = 'Plumrocket_' . $this->getModuleName->execute($moduleName);
|
||||
$etcDirPath = $this->moduleDirReader->getModuleDir(Dir::MODULE_ETC_DIR, $moduleName);
|
||||
|
||||
return strpos($etcDirPath, self::NON_COMPOSER_EXTENSION_PATH) !== false
|
||||
? self::NON_COMPOSER_INSTALL_TYPE : self::COMPOSER_INSTALL_TYPE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?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;
|
||||
|
||||
use Magento\Framework\Module\ModuleListInterface;
|
||||
|
||||
/**
|
||||
* Retrieve names of installed plumrocket extensions
|
||||
* For example ['Base', 'SocialLoginFree']
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class GetListOfInstalled
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Module\ModuleListInterface
|
||||
*/
|
||||
private $moduleList;
|
||||
|
||||
/**
|
||||
* GetPlumrocketInstalledExtensions constructor.
|
||||
*
|
||||
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
|
||||
*/
|
||||
public function __construct(ModuleListInterface $moduleList)
|
||||
{
|
||||
$this->moduleList = $moduleList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of installed plumrocket extensions.
|
||||
*
|
||||
* @return string[] only module name, without vendor name
|
||||
*/
|
||||
public function execute(): array
|
||||
{
|
||||
$plumrocketModuleNames = [];
|
||||
foreach ($this->moduleList->getNames() as $moduleFullName) {
|
||||
[$vendor, $module] = explode('_', $moduleFullName);
|
||||
if ('Plumrocket' !== $vendor) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$plumrocketModuleNames[] = $module;
|
||||
}
|
||||
|
||||
return $plumrocketModuleNames;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* Get name "SocialLoginFree" from "Plumrocket_SocialLoginFree" or "SocialLoginFree"
|
||||
*
|
||||
* @since 2.3.7
|
||||
*/
|
||||
class GetModuleName
|
||||
{
|
||||
|
||||
/**
|
||||
* Remove Plumrocket_ from Plumrocket_ModuleName.
|
||||
*
|
||||
* @param string $maybeModuleFullName
|
||||
* @return string
|
||||
*/
|
||||
public function execute(string $maybeModuleFullName): string
|
||||
{
|
||||
if (false === strpos($maybeModuleFullName, '_')) {
|
||||
return $maybeModuleFullName;
|
||||
}
|
||||
return explode('_', $maybeModuleFullName)[1];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
<?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\Information;
|
||||
|
||||
use Magento\Framework\DataObject;
|
||||
use Plumrocket\Base\Api\Data\ExtensionInformationInterface;
|
||||
use Plumrocket\Base\Api\GetModuleVersionInterface;
|
||||
|
||||
/**
|
||||
* Container for information about extension that parsed from "pr_extensions.xml"
|
||||
*
|
||||
* Can be used for old extension which has not xml file
|
||||
* and does not realize \Plumrocket\Base\Api\ModuleInformationInterface
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Container extends DataObject implements ExtensionInformationInterface
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetModuleVersionInterface
|
||||
*/
|
||||
private $getModuleVersion;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Api\GetModuleVersionInterface $getModuleVersion
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
GetModuleVersionInterface $getModuleVersion,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($data);
|
||||
$this->getModuleVersion = $getModuleVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isService(): bool
|
||||
{
|
||||
return (bool) $this->_getData(self::FIELD_IS_SERVICE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getTitle(): string
|
||||
{
|
||||
return (string) $this->_getData(self::FIELD_TITLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDocumentationLink(): string
|
||||
{
|
||||
return (string) ($this->_getData(self::FIELD_DOCUMENTATION) ?: $this->_getData(self::FIELD_WIKI));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getWikiLink(): string
|
||||
{
|
||||
return $this->getDocumentationLink();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getUrl(): string
|
||||
{
|
||||
return (string) $this->_getData(self::FIELD_URL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getMarketplaceUrl(): string
|
||||
{
|
||||
return (string) $this->_getData(self::FIELD_MARKETPLACE_URL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getConfigSection(): string
|
||||
{
|
||||
return (string) $this->_getData(self::FIELD_CONFIG_SECTION);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getIsEnabledFieldConfigPath(): string
|
||||
{
|
||||
return (string) $this->_getData(self::FIELD_IS_ENABLED_PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getModuleName(): string
|
||||
{
|
||||
return (string) $this->_getData(self::FIELD_MODULE_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getVendorAndModuleName(): string
|
||||
{
|
||||
return "Plumrocket_{$this->getModuleName()}";
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInstalledVersion(): string
|
||||
{
|
||||
return $this->getModuleVersion->execute($this->getVendorAndModuleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setIsService(bool $isService): ExtensionInformationInterface
|
||||
{
|
||||
return $this->setData(self::FIELD_IS_SERVICE, $isService);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setTitle(string $title): ExtensionInformationInterface
|
||||
{
|
||||
return $this->setData(self::FIELD_TITLE, $title);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setConfigSection(string $configSection): ExtensionInformationInterface
|
||||
{
|
||||
return $this->setData(self::FIELD_CONFIG_SECTION, $configSection);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setDocumentationLink(string $documentationLink): ExtensionInformationInterface
|
||||
{
|
||||
return $this->setData(self::FIELD_DOCUMENTATION, $documentationLink);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setWikiLink(string $wikiLink): ExtensionInformationInterface
|
||||
{
|
||||
return $this->setDocumentationLink($wikiLink);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setUrl(string $extensionUrl): ExtensionInformationInterface
|
||||
{
|
||||
return $this->setData(self::FIELD_URL, $extensionUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setMarketplaceUrl(string $extensionMarketplaceUrl): ExtensionInformationInterface
|
||||
{
|
||||
return $this->setData(self::FIELD_MARKETPLACE_URL, $extensionMarketplaceUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setModuleName(string $moduleName): ExtensionInformationInterface
|
||||
{
|
||||
return $this->setData(self::FIELD_MODULE_NAME, $moduleName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setIsEnabledFieldConfigPath(string $configPath): ExtensionInformationInterface
|
||||
{
|
||||
return $this->setData(self::FIELD_IS_ENABLED_PATH, $configPath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?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\Information;
|
||||
|
||||
use Magento\Framework\Config\DataInterface;
|
||||
use Magento\Framework\Module\ModuleListInterface;
|
||||
use Plumrocket\Base\Api\Data\ExtensionInformationInterface;
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
use Plumrocket\Base\Model\Extension\GetModuleName;
|
||||
use Plumrocket\Base\Model\Extension\Information\ContainerFactory;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Get implements GetExtensionInformationInterface
|
||||
{
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $services = [
|
||||
'Base',
|
||||
'Token',
|
||||
'AmpEmailApi',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\Data\ExtensionInformationInterface[]
|
||||
*/
|
||||
private $extensions;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Information\ContainerFactory
|
||||
*/
|
||||
private $informationContainerFactory;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetModuleName
|
||||
*/
|
||||
private $getExtensionName;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Config\DataInterface
|
||||
*/
|
||||
private $extensionsConfig;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Module\ModuleListInterface
|
||||
*/
|
||||
private $moduleList;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Information\ContainerFactory $informationContainerFactory
|
||||
* @param \Plumrocket\Base\Model\Extension\GetModuleName $getExtensionName
|
||||
* @param \Magento\Framework\Config\DataInterface $extensionsConfig
|
||||
* @param \Magento\Framework\Module\ModuleListInterface $moduleList
|
||||
* @param array $extensions
|
||||
*/
|
||||
public function __construct(
|
||||
ContainerFactory $informationContainerFactory,
|
||||
GetModuleName $getExtensionName,
|
||||
DataInterface $extensionsConfig,
|
||||
ModuleListInterface $moduleList,
|
||||
array $extensions = []
|
||||
) {
|
||||
$this->extensions = $extensions;
|
||||
$this->informationContainerFactory = $informationContainerFactory;
|
||||
$this->getExtensionName = $getExtensionName;
|
||||
$this->extensionsConfig = $extensionsConfig;
|
||||
$this->moduleList = $moduleList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function execute(string $moduleName): ExtensionInformationInterface
|
||||
{
|
||||
$moduleName = $this->getExtensionName->execute($moduleName);
|
||||
|
||||
if (! isset($this->extensions[$moduleName])) {
|
||||
/** @var \Plumrocket\Base\Model\Extension\Information\Container $infoContainer */
|
||||
$infoContainer = $this->informationContainerFactory->create();
|
||||
$infoContainer->setModuleName($moduleName);
|
||||
|
||||
if ($extensionConfig = $this->extensionsConfig->get($moduleName)) {
|
||||
$infoContainer->setIsService($extensionConfig[ExtensionInformationInterface::FIELD_IS_SERVICE])
|
||||
->setModuleName($extensionConfig['name'])
|
||||
->setTitle($extensionConfig['title'])
|
||||
->setConfigSection($extensionConfig['config_section'])
|
||||
->setIsEnabledFieldConfigPath($extensionConfig['is_enabled_path'])
|
||||
->setDocumentationLink($extensionConfig['documentation'] ?: $extensionConfig['wiki'])
|
||||
->setUrl($extensionConfig[ExtensionInformationInterface::FIELD_URL] ?? '')
|
||||
->setMarketplaceUrl(
|
||||
$extensionConfig[ExtensionInformationInterface::FIELD_MARKETPLACE_URL] ?? ''
|
||||
);
|
||||
} else {
|
||||
if ($this->moduleList->has("Plumrocket_$moduleName")) {
|
||||
$infoContainer->setIsService(in_array($moduleName, $this->services, true));
|
||||
}
|
||||
}
|
||||
|
||||
$this->extensions[$moduleName] = $infoContainer;
|
||||
}
|
||||
|
||||
return $this->extensions[$moduleName];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?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\Information;
|
||||
|
||||
use Magento\Framework\Api\SearchCriteriaBuilder;
|
||||
use Magento\Framework\Api\SearchCriteriaInterface;
|
||||
use Magento\Framework\Api\SearchResultsInterface;
|
||||
use Plumrocket\Base\Api\Data\ExtensionInformationSearchResultsInterfaceFactory;
|
||||
use Plumrocket\Base\Api\ExtensionInformationListInterface;
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
use Plumrocket\Base\Model\Extension\GetListOfInstalled;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class ListProvider implements ExtensionInformationListInterface
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\Api\SearchCriteriaBuilder
|
||||
*/
|
||||
private $searchCriteriaBuilder;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\Data\ExtensionInformationSearchResultsInterfaceFactory
|
||||
*/
|
||||
private $searchResultsFactory;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetListOfInstalled
|
||||
*/
|
||||
private $getListOfInstalledExtensions;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetExtensionInformationInterface
|
||||
*/
|
||||
private $getExtensionInformation;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
|
||||
* @param \Plumrocket\Base\Api\Data\ExtensionInformationSearchResultsInterfaceFactory $searchResultsFactory
|
||||
* @param \Plumrocket\Base\Model\Extension\GetListOfInstalled $getListOfInstalledExtensions
|
||||
* @param \Plumrocket\Base\Api\GetExtensionInformationInterface $getExtensionInformation
|
||||
*/
|
||||
public function __construct(
|
||||
SearchCriteriaBuilder $searchCriteriaBuilder,
|
||||
ExtensionInformationSearchResultsInterfaceFactory $searchResultsFactory,
|
||||
GetListOfInstalled $getListOfInstalledExtensions,
|
||||
GetExtensionInformationInterface $getExtensionInformation
|
||||
) {
|
||||
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
|
||||
$this->searchResultsFactory = $searchResultsFactory;
|
||||
$this->getListOfInstalledExtensions = $getListOfInstalledExtensions;
|
||||
$this->getExtensionInformation = $getExtensionInformation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getList(?SearchCriteriaInterface $searchCriteria = null): SearchResultsInterface
|
||||
{
|
||||
if (null === $searchCriteria) {
|
||||
$searchCriteria = $this->searchCriteriaBuilder->create();
|
||||
}
|
||||
|
||||
$extensionsNames = $this->getListOfInstalledExtensions->execute();
|
||||
|
||||
$informationList = [];
|
||||
foreach ($extensionsNames as $extensionsName) {
|
||||
$informationList[] = $this->getExtensionInformation->execute($extensionsName);
|
||||
}
|
||||
|
||||
/** @var \Plumrocket\Base\Api\Data\ExtensionInformationSearchResultsInterface $searchResults */
|
||||
$searchResults = $this->searchResultsFactory->create();
|
||||
|
||||
$searchResults->setItems($informationList);
|
||||
$searchResults->setTotalCount(count($informationList));
|
||||
$searchResults->setSearchCriteria($searchCriteria);
|
||||
|
||||
return $searchResults;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?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;
|
||||
|
||||
use Magento\Config\Model\Config\Structure\Data as ConfigStructureData;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Plumrocket\Base\Api\Data\ExtensionInformationInterface;
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class IsPaid
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetExtensionInformationInterface
|
||||
*/
|
||||
private $getExtensionInformation;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var ConfigStructureData
|
||||
*/
|
||||
private $systemXmlStructure;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Api\GetExtensionInformationInterface $getExtensionInformation
|
||||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
||||
* @param ConfigStructureData $configStructureData
|
||||
*/
|
||||
public function __construct(
|
||||
GetExtensionInformationInterface $getExtensionInformation,
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
ConfigStructureData $configStructureData
|
||||
) {
|
||||
$this->getExtensionInformation = $getExtensionInformation;
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->systemXmlStructure = $configStructureData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the extension is paid.
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $moduleName): bool
|
||||
{
|
||||
$extensionInformation = $this->getExtensionInformation->execute($moduleName);
|
||||
return $this->hasSerialKey($extensionInformation) || $this->hasDeclaredSerialKeyField($extensionInformation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the extension has empty or not serial key value.
|
||||
*
|
||||
* @param \Plumrocket\Base\Api\Data\ExtensionInformationInterface $extensionInformation
|
||||
* @return bool
|
||||
*/
|
||||
private function hasSerialKey(ExtensionInformationInterface $extensionInformation): bool
|
||||
{
|
||||
$generalConfigurations = $this->scopeConfig->getValue(
|
||||
"{$extensionInformation->getConfigSection()}/general",
|
||||
ScopeInterface::SCOPE_STORE,
|
||||
0
|
||||
);
|
||||
return is_array($generalConfigurations) && array_key_exists('serial', $generalConfigurations);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the serial key field is exists in system.xml
|
||||
*
|
||||
* @param \Plumrocket\Base\Api\Data\ExtensionInformationInterface $extensionInformation
|
||||
* @return bool
|
||||
*/
|
||||
private function hasDeclaredSerialKeyField(ExtensionInformationInterface $extensionInformation): bool
|
||||
{
|
||||
return (bool) $this->systemXmlStructure->get(
|
||||
"sections/{$extensionInformation->getConfigSection()}/children/general/children/serial"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?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\Status;
|
||||
|
||||
use Magento\Config\Model\Config;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\ObjectManagerInterface;
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
use Plumrocket\Base\Model\Extension\Status\Disable\Logger;
|
||||
|
||||
/**
|
||||
* @since 2.3.7
|
||||
*/
|
||||
class Disable
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Config\Model\Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\ResourceConnection
|
||||
*/
|
||||
private $resourceConnection;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetExtensionInformationInterface
|
||||
*/
|
||||
private $getExtensionInformation;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\ObjectManagerInterface
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Status\Disable\Logger
|
||||
*/
|
||||
private $disableExtensionLogger;
|
||||
|
||||
/**
|
||||
* @param \Magento\Config\Model\Config $config
|
||||
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
|
||||
* @param \Plumrocket\Base\Api\GetExtensionInformationInterface $getExtensionInformation
|
||||
* @param \Magento\Framework\ObjectManagerInterface $objectManager
|
||||
* @param \Plumrocket\Base\Model\Extension\Status\Disable\Logger $disableExtensionLogger
|
||||
*/
|
||||
public function __construct(
|
||||
Config $config,
|
||||
ResourceConnection $resourceConnection,
|
||||
GetExtensionInformationInterface $getExtensionInformation,
|
||||
ObjectManagerInterface $objectManager,
|
||||
Logger $disableExtensionLogger
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->resourceConnection = $resourceConnection;
|
||||
$this->getExtensionInformation = $getExtensionInformation;
|
||||
$this->objectManager = $objectManager;
|
||||
$this->disableExtensionLogger = $disableExtensionLogger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $moduleName
|
||||
* @param string $description
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $moduleName, string $description = ''): bool
|
||||
{
|
||||
// TODO: remove usage of 'disableExtension' after finished integration with all extensions
|
||||
$helper = $this->getHelper($moduleName);
|
||||
if ($helper && method_exists($helper, 'disableExtension')) {
|
||||
$helper->disableExtension();
|
||||
$this->disableExtensionLogger->log($moduleName, $description);
|
||||
return true;
|
||||
}
|
||||
|
||||
$configPath = $this->getExtensionInformation->execute($moduleName)->getIsEnabledFieldConfigPath();
|
||||
if (! $configPath) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$connection = $this->resourceConnection->getConnection('core_write');
|
||||
$connection->delete(
|
||||
$this->resourceConnection->getTableName('core_config_data'),
|
||||
[
|
||||
$connection->quoteInto('path = ?', $configPath)
|
||||
]
|
||||
);
|
||||
|
||||
$this->disableExtensionLogger->log($moduleName, $description);
|
||||
|
||||
try {
|
||||
$this->config->setDataByPath($configPath, 0);
|
||||
$this->config->save();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive data helper
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return \Magento\Framework\App\Helper\AbstractHelper|false
|
||||
*/
|
||||
private function getHelper(string $moduleName)
|
||||
{
|
||||
$type = "Plumrocket\\{$moduleName}\Helper\Data";
|
||||
try {
|
||||
return $this->objectManager->get($type);
|
||||
} catch (\ReflectionException $reflectionException) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?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\Status\Disable;
|
||||
|
||||
use Plumrocket\Base\Model\External\LastRequests;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Logger for the extension disabling reason.
|
||||
*
|
||||
* If one of the plumrocket extensions disabled, we need have the opportunity to get know who have done it.
|
||||
* So we add logger that track all disabling due to bad licence, store problem, etc.
|
||||
*
|
||||
* @since 2.5.7
|
||||
*/
|
||||
class Logger
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\External\LastRequests
|
||||
*/
|
||||
private $lastRequests;
|
||||
|
||||
/**
|
||||
* @var \Psr\Log\LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\External\LastRequests $lastRequests
|
||||
* @param \Psr\Log\LoggerInterface $logger
|
||||
*/
|
||||
public function __construct(
|
||||
LastRequests $lastRequests,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->lastRequests = $lastRequests;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function log(string $moduleName, string $description = '')
|
||||
{
|
||||
$logMessage = "$moduleName was disabled, for details see the request data and the backtrace below";
|
||||
$logMessage .= $description ? "\nDescription: $description" : '';
|
||||
$logMessage .= "\nConnector Requests Data: " . json_encode($this->lastRequests->getList());
|
||||
$logMessage .= "\n" . (new \Exception)->getTraceAsString();
|
||||
/** Use exception.log as module disabling is critical */
|
||||
$this->logger->critical(new \Exception($logMessage));
|
||||
|
||||
// TODO: add admin notification
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?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\Status;
|
||||
|
||||
use Magento\Framework\Module\Manager as ModuleManager;
|
||||
use Magento\Framework\Module\ModuleListInterface;
|
||||
use Plumrocket\Base\Api\ExtensionStatusInterface;
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
use Plumrocket\Base\Api\GetExtensionStatusInterface;
|
||||
use Plumrocket\Base\Model\ConfigUtils;
|
||||
use Plumrocket\Base\Model\Extension\GetModuleName;
|
||||
|
||||
/**
|
||||
* Class GetModuleVersion
|
||||
*
|
||||
* @since 2.3.9
|
||||
*/
|
||||
class Get implements GetExtensionStatusInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ModuleManager
|
||||
*/
|
||||
private $moduleManager;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Module\ModuleListInterface
|
||||
*/
|
||||
private $fullModuleList;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetExtensionInformationInterface
|
||||
*/
|
||||
private $getExtensionInformation;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\ConfigUtils
|
||||
*/
|
||||
private $configUtils;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetModuleName
|
||||
*/
|
||||
private $getModuleName;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\Module\Manager $moduleManager
|
||||
* @param \Magento\Framework\Module\ModuleListInterface $fullModuleList
|
||||
* @param \Plumrocket\Base\Api\GetExtensionInformationInterface $getExtensionInformation
|
||||
* @param \Plumrocket\Base\Model\ConfigUtils $configUtils
|
||||
* @param \Plumrocket\Base\Model\Extension\GetModuleName $getModuleName
|
||||
*/
|
||||
public function __construct(
|
||||
ModuleManager $moduleManager,
|
||||
ModuleListInterface $fullModuleList,
|
||||
GetExtensionInformationInterface $getExtensionInformation,
|
||||
ConfigUtils $configUtils,
|
||||
GetModuleName $getModuleName
|
||||
) {
|
||||
$this->moduleManager = $moduleManager;
|
||||
$this->fullModuleList = $fullModuleList;
|
||||
$this->getExtensionInformation = $getExtensionInformation;
|
||||
$this->configUtils = $configUtils;
|
||||
$this->getModuleName = $getModuleName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve status of Plumrocket module
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return int
|
||||
*/
|
||||
public function execute(string $moduleName): int
|
||||
{
|
||||
$moduleName = $this->getModuleName->execute($moduleName);
|
||||
|
||||
$hasModule = $this->moduleManager->isEnabled("Plumrocket_$moduleName");
|
||||
if (! $hasModule) {
|
||||
return $this->fullModuleList->has("Plumrocket_$moduleName")
|
||||
? ExtensionStatusInterface::DISABLED_FROM_CLI
|
||||
: ExtensionStatusInterface::NOT_INSTALLED;
|
||||
}
|
||||
|
||||
$extensionInformation = $this->getExtensionInformation->execute($moduleName);
|
||||
if ($isEnabledConfigPath = $extensionInformation->getIsEnabledFieldConfigPath()) {
|
||||
return $this->configUtils->isSetFlag($isEnabledConfigPath)
|
||||
? ExtensionStatusInterface::ENABLED
|
||||
: ExtensionStatusInterface::DISABLED;
|
||||
}
|
||||
return ExtensionStatusInterface::ENABLED;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?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\Status;
|
||||
|
||||
use Magento\Store\Model\StoreManager;
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
use Plumrocket\Base\Model\ConfigUtils;
|
||||
use Plumrocket\Base\Model\Extension\GetModuleName;
|
||||
|
||||
/**
|
||||
* Check if module is enabled in configurations on any store view
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class IsEnabledOnAnyStoreView
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Magento\Store\Model\StoreManager
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetModuleName
|
||||
*/
|
||||
private $getExtensionName;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetExtensionInformationInterface
|
||||
*/
|
||||
private $getExtensionInformation;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\ConfigUtils
|
||||
*/
|
||||
private $configUtils;
|
||||
|
||||
/**
|
||||
* @param \Magento\Store\Model\StoreManager $storeManager
|
||||
* @param \Plumrocket\Base\Model\Extension\GetModuleName $getExtensionName
|
||||
* @param \Plumrocket\Base\Api\GetExtensionInformationInterface $getExtensionInformation
|
||||
* @param \Plumrocket\Base\Model\ConfigUtils $configUtils
|
||||
*/
|
||||
public function __construct(
|
||||
StoreManager $storeManager,
|
||||
GetModuleName $getExtensionName,
|
||||
GetExtensionInformationInterface $getExtensionInformation,
|
||||
ConfigUtils $configUtils
|
||||
) {
|
||||
$this->storeManager = $storeManager;
|
||||
$this->getExtensionName = $getExtensionName;
|
||||
$this->getExtensionInformation = $getExtensionInformation;
|
||||
$this->configUtils = $configUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if module is enabled on any store view.
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $moduleName): bool
|
||||
{
|
||||
$moduleName = $this->getExtensionName->execute($moduleName);
|
||||
$configPath = $this->getExtensionInformation->execute($moduleName)->getIsEnabledFieldConfigPath();
|
||||
|
||||
foreach ($this->storeManager->getStores() as $store) {
|
||||
if (! $store->getIsActive()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($configPath && $this->configUtils->getStoreConfig($configPath, $store->getId())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?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\Status;
|
||||
|
||||
use Plumrocket\Base\Api\ExtensionStatusInterface;
|
||||
use Plumrocket\Base\Model\Extension\GetModuleName;
|
||||
|
||||
/**
|
||||
* @since 2.4.1
|
||||
*/
|
||||
class Provider implements ExtensionStatusInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Status\Get
|
||||
*/
|
||||
private $getExtensionStatus;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetModuleName
|
||||
*/
|
||||
private $getExtensionName;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Status\Get $getExtensionStatus
|
||||
* @param \Plumrocket\Base\Model\Extension\GetModuleName $getExtensionName
|
||||
*/
|
||||
public function __construct(
|
||||
Get $getExtensionStatus,
|
||||
GetModuleName $getExtensionName
|
||||
) {
|
||||
$this->getExtensionStatus = $getExtensionStatus;
|
||||
$this->getExtensionName = $getExtensionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isEnabled(string $moduleName): bool
|
||||
{
|
||||
return self::ENABLED === $this->getExtensionStatus->execute($this->getExtensionName->execute($moduleName));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isDisabled(string $moduleName): bool
|
||||
{
|
||||
return self::DISABLED === $this->getExtensionStatus->execute($this->getExtensionName->execute($moduleName));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isDisabledFromCli(string $moduleName): bool
|
||||
{
|
||||
$status = $this->getExtensionStatus->execute($this->getExtensionName->execute($moduleName));
|
||||
return self::DISABLED_FROM_CLI === $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isNotInstalled(string $moduleName): bool
|
||||
{
|
||||
$status = $this->getExtensionStatus->execute($this->getExtensionName->execute($moduleName));
|
||||
return self::NOT_INSTALLED === $status;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
<?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\Extensions;
|
||||
|
||||
use Magento\Framework\DataObject;
|
||||
use Plumrocket\Base\Api\GetModuleVersionInterface;
|
||||
|
||||
/**
|
||||
* Container for information about extension
|
||||
* Use in case when module hasn't \Plumrocket\Base\Api\ModuleInformationInterface realise
|
||||
* @since 2.3.0
|
||||
* @deprecated 2.5.0
|
||||
*/
|
||||
class EmptyInformation extends DataObject
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetModuleVersionInterface
|
||||
*/
|
||||
private $getModuleVersion;
|
||||
|
||||
/**
|
||||
* EmptyInformation constructor.
|
||||
*
|
||||
* @param \Plumrocket\Base\Api\GetModuleVersionInterface $getModuleVersion
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct(
|
||||
GetModuleVersionInterface $getModuleVersion,
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($data);
|
||||
$this->getModuleVersion = $getModuleVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isService(): bool
|
||||
{
|
||||
return (bool) $this->_getData('is_service');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getTitle(): string
|
||||
{
|
||||
return (string) $this->_getData('title');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getWikiLink(): string
|
||||
{
|
||||
return (string) $this->_getData('wiki');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getConfigSection(): string
|
||||
{
|
||||
return (string) $this->_getData('config_section');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getIsEnabledFieldConfigPath(): string
|
||||
{
|
||||
return (string) $this->_getData('is_enabled_path');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getModuleName(): string
|
||||
{
|
||||
return (string) $this->_getData('module_name');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getVendorAndModuleName(): string
|
||||
{
|
||||
return (string) $this->_getData('full_module_name') ?: 'Plumrocket_' . $this->getModuleName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInstalledVersion(): string
|
||||
{
|
||||
return $this->getModuleVersion->execute($this->getVendorAndModuleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isService
|
||||
* @return \Plumrocket\Base\Model\Extensions\EmptyInformation
|
||||
*/
|
||||
public function setIsService(bool $isService): EmptyInformation
|
||||
{
|
||||
return $this->setData('is_service', $isService);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
* @return \Plumrocket\Base\Model\Extensions\EmptyInformation
|
||||
*/
|
||||
public function setTitle(string $title): EmptyInformation
|
||||
{
|
||||
return $this->setData('title', $title);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $configSection
|
||||
* @return \Plumrocket\Base\Model\Extensions\EmptyInformation
|
||||
*/
|
||||
public function setConfigSection(string $configSection): EmptyInformation
|
||||
{
|
||||
return $this->setData('config_section', $configSection);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $wikiLink
|
||||
* @return \Plumrocket\Base\Model\Extensions\EmptyInformation
|
||||
*/
|
||||
public function setWikiLink(string $wikiLink): EmptyInformation
|
||||
{
|
||||
return $this->setData('wiki', $wikiLink);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $moduleName
|
||||
* @return \Plumrocket\Base\Model\Extensions\EmptyInformation
|
||||
*/
|
||||
public function setVendorAndModuleName(string $moduleName): EmptyInformation
|
||||
{
|
||||
return $this->setData('full_module_name', $moduleName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $moduleName
|
||||
* @return \Plumrocket\Base\Model\Extensions\EmptyInformation
|
||||
*/
|
||||
public function setModuleName(string $moduleName): EmptyInformation
|
||||
{
|
||||
return $this->setData('module_name', $moduleName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $configPath
|
||||
* @return \Plumrocket\Base\Model\Extensions\EmptyInformation
|
||||
*/
|
||||
public function setIsEnabledFieldConfigPath(string $configPath): EmptyInformation
|
||||
{
|
||||
return $this->setData('is_enabled_path', $configPath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?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\Extensions;
|
||||
|
||||
use Plumrocket\Base\Model\Extension\GetModuleName;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
* @deprecated since 2.5.0
|
||||
* @see \Plumrocket\Base\Model\Extension\Information\Get
|
||||
*/
|
||||
class GetInformation
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\ModuleInformationInterface[]
|
||||
*/
|
||||
private $extensions;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetModuleName
|
||||
*/
|
||||
private $getExtensionName;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\GetModuleName $getModuleName
|
||||
* @param array $extensions
|
||||
*/
|
||||
public function __construct(
|
||||
GetModuleName $getModuleName,
|
||||
array $extensions = []
|
||||
) {
|
||||
$this->getExtensionName = $getModuleName;
|
||||
$this->extensions = $extensions;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $moduleName
|
||||
* @return \Plumrocket\Base\Api\ModuleInformationInterface|null
|
||||
*/
|
||||
public function execute(string $moduleName)
|
||||
{
|
||||
$moduleName = $this->getExtensionName->execute($moduleName);
|
||||
return $this->extensions[$moduleName] ?? null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
<?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\Extensions;
|
||||
|
||||
use Plumrocket\Base\Api\Data\ExtensionInformationInterface;
|
||||
use Plumrocket\Base\Api\GetModuleVersionInterface;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
* @deprecated since 2.5.0 - it is better to use "pr_extensions.xml" than this class with constants
|
||||
*/
|
||||
class Information implements ExtensionInformationInterface
|
||||
{
|
||||
const IS_SERVICE = true;
|
||||
const NAME = 'Base';
|
||||
const WIKI = '';
|
||||
const CONFIG_SECTION = 'plumbase';
|
||||
|
||||
/**
|
||||
* Path to config that define if module enabled,
|
||||
* for modules without configs left this constant empty
|
||||
*/
|
||||
const IS_ENABLED_FIELD_CONFIG_PATH = '';
|
||||
const MODULE_NAME = 'Base';
|
||||
const VENDOR_NAME = 'Plumrocket';
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetModuleVersionInterface
|
||||
*/
|
||||
private $getModuleVersion;
|
||||
|
||||
/**
|
||||
* ModuleInformation constructor.
|
||||
*
|
||||
* @param \Plumrocket\Base\Api\GetModuleVersionInterface $getModuleVersion
|
||||
*/
|
||||
public function __construct(GetModuleVersionInterface $getModuleVersion)
|
||||
{
|
||||
$this->getModuleVersion = $getModuleVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isService(): bool
|
||||
{
|
||||
return static::IS_SERVICE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getTitle(): string
|
||||
{
|
||||
return static::NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getDocumentationLink(): string
|
||||
{
|
||||
return static::WIKI;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getWikiLink(): string
|
||||
{
|
||||
return static::WIKI;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getConfigSection(): string
|
||||
{
|
||||
return static::CONFIG_SECTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getIsEnabledFieldConfigPath(): string
|
||||
{
|
||||
return static::IS_ENABLED_FIELD_CONFIG_PATH;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getModuleName(): string
|
||||
{
|
||||
return static::MODULE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getVendorAndModuleName(): string
|
||||
{
|
||||
return static::VENDOR_NAME . '_' . $this->getModuleName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getUrl(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getMarketplaceUrl(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getInstalledVersion(): string
|
||||
{
|
||||
return $this->getModuleVersion->execute($this->getVendorAndModuleName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setIsService(bool $isService): ExtensionInformationInterface
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setTitle(string $title): ExtensionInformationInterface
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setConfigSection(string $configSection): ExtensionInformationInterface
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setDocumentationLink(string $documentationLink): ExtensionInformationInterface
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setWikiLink(string $wikiLink): ExtensionInformationInterface
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setUrl(string $extensionUrl): ExtensionInformationInterface
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setMarketplaceUrl(string $extensionMarketplaceUrl): ExtensionInformationInterface
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setModuleName(string $moduleName): ExtensionInformationInterface
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setIsEnabledFieldConfigPath(string $configPath): ExtensionInformationInterface
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?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\External;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Serialize\SerializerInterface;
|
||||
use Plumrocket\Base\Helper\Config;
|
||||
|
||||
class Connector
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Helper\Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Serialize\SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\External\LastRequests
|
||||
*/
|
||||
private $lastRequests;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Helper\Config $config
|
||||
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
|
||||
* @param \Plumrocket\Base\Model\External\LastRequests $lastRequests
|
||||
*/
|
||||
public function __construct(
|
||||
Config $config,
|
||||
SerializerInterface $serializer,
|
||||
LastRequests $lastRequests
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->serializer = $serializer;
|
||||
$this->lastRequests = $lastRequests;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send POST request to url.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $data
|
||||
* @return array|bool|float|int|string|null
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function connect(string $url, array $data = [])
|
||||
{
|
||||
$data['v'] = 1;
|
||||
$query = http_build_query($data);
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, count($data));
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
$res = curl_exec($ch);
|
||||
|
||||
if (curl_errno($ch)) {
|
||||
$errorMessage = 'cURL Error: ' . curl_error($ch);
|
||||
$this->lastRequests->add($url, $data, ['error' => $errorMessage]);
|
||||
if ($this->config->isDebugMode()) {
|
||||
throw new \Exception($errorMessage, 1);
|
||||
}
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
if (false === $res) {
|
||||
$this->lastRequests->add($url, $data, ['error' => "Cannot connect to $url"]);
|
||||
throw new LocalizedException(__('Cannot connect to %1', $url));
|
||||
}
|
||||
|
||||
$res = $this->serializer->unserialize($res);
|
||||
$this->lastRequests->add($url, $data, $res);
|
||||
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?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\External;
|
||||
|
||||
/**
|
||||
* Buffer for last external requests.
|
||||
*
|
||||
* Can be used for debugging store endpoint problems.
|
||||
*
|
||||
* @since 2.5.7
|
||||
*/
|
||||
class LastRequests
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array[]
|
||||
*/
|
||||
private $data = [];
|
||||
|
||||
/**
|
||||
* Add request response.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $params
|
||||
* @param array $response
|
||||
*/
|
||||
public function add(string $url, array $params, array $response): void
|
||||
{
|
||||
if (strpos($url, Urls::PINGBACK_URL . '/extension') !== false) {
|
||||
$this->data[] = [
|
||||
'request_url' => $url,
|
||||
'request_params' => $params,
|
||||
'response' => $response
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get saved data.
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
public function getList(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all saved requests data
|
||||
*/
|
||||
public function clear(): void
|
||||
{
|
||||
$this->data = [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?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\External;
|
||||
|
||||
/**
|
||||
* Contains all external URLs that the extension calls.
|
||||
*
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class Urls
|
||||
{
|
||||
/**
|
||||
* Provides xml with extensions changelogs.
|
||||
*/
|
||||
public const CHANGELOGS_URL = 'plumrocket.com/media/info/changelogs_m2.xml';
|
||||
|
||||
/**
|
||||
* Provides xml with the latest version of each extension.
|
||||
*/
|
||||
public const VERSIONS_URL = 'plumrocket.com/media/info/versions.xml';
|
||||
|
||||
/**
|
||||
* Service for collections the extensions usage statistic.
|
||||
*/
|
||||
public const STATISTIC_URL = 'api.plumrocket.net/v1/statistic';
|
||||
|
||||
/**
|
||||
* Base url for retrieving our notifications.
|
||||
*/
|
||||
public const NOTIFICATIONS_URL = 'plumrocket.com/notificationmanager/feed/index';
|
||||
|
||||
/**
|
||||
* Service for collections the extensions pingbacks.
|
||||
*/
|
||||
public const PINGBACK_URL = 'plumrocket.com/ilg/pingback';
|
||||
|
||||
/**
|
||||
* Service for collections the marketplace extensions pingbacks.
|
||||
*/
|
||||
public const MARKETPLACE_PINGBACK_URL = 'plumrocket.com/ilg/pingback/marketplace';
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
namespace Plumrocket\Base\Model;
|
||||
|
||||
use Magento\Framework\App\CacheInterface;
|
||||
use Magento\Framework\App\Config;
|
||||
use Magento\Framework\App\Utility\Files;
|
||||
use Magento\Framework\Component\ComponentRegistrar;
|
||||
use Magento\Framework\Composer\ComposerInformation;
|
||||
use Magento\Framework\Config\DataInterface;
|
||||
use Plumrocket\Base\Api\GetModuleVersionInterface;
|
||||
use Plumrocket\Base\Model\Extension\GetModuleName;
|
||||
|
||||
/**
|
||||
* Reads version from composer.json
|
||||
*
|
||||
* Has special logic for metapackage.
|
||||
*
|
||||
* @since 2.1.6
|
||||
*/
|
||||
class GetModuleVersion implements GetModuleVersionInterface
|
||||
{
|
||||
public const CACHE_IDENTIFIER = 'PR_EXTENSION_VERSION';
|
||||
public const METAPACKAGE_CACHE_IDENTIFIER = 'PR_METAPACKAGE_VERSION';
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Utility\Files
|
||||
*/
|
||||
private $files;
|
||||
|
||||
/**
|
||||
* For example [ ModuleName => '2.1.3' ]
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private $versionsLocalCache = [];
|
||||
|
||||
/**
|
||||
* For example [ ModuleName => '2.1.3' ]
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private $metapackageVersions = [];
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\CacheInterface
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\GetModuleName
|
||||
*/
|
||||
private $getModuleName;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Config\DataInterface
|
||||
*/
|
||||
private $extensionsConfig;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Composer\ComposerInformation
|
||||
*/
|
||||
private $composerInformation;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Utility\Files $files
|
||||
* @param \Magento\Framework\App\CacheInterface $cache
|
||||
* @param \Plumrocket\Base\Model\Extension\GetModuleName $getModuleName
|
||||
* @param \Magento\Framework\Config\DataInterface $extensionsConfig
|
||||
* @param \Magento\Framework\Composer\ComposerInformation $composerInformation
|
||||
*/
|
||||
public function __construct(
|
||||
Files $files,
|
||||
CacheInterface $cache,
|
||||
GetModuleName $getModuleName,
|
||||
DataInterface $extensionsConfig,
|
||||
ComposerInformation $composerInformation
|
||||
) {
|
||||
$this->files = $files;
|
||||
$this->cache = $cache;
|
||||
$this->getModuleName = $getModuleName;
|
||||
$this->extensionsConfig = $extensionsConfig;
|
||||
$this->composerInformation = $composerInformation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get module version from composer.json.
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return string
|
||||
*/
|
||||
public function execute($moduleName)
|
||||
{
|
||||
$moduleName = $this->getModuleName->execute($moduleName);
|
||||
|
||||
if ($this->shouldUseMetapackage($moduleName)) {
|
||||
return $this->getMetapackageVersion($moduleName);
|
||||
}
|
||||
return $this->getModuleVersion($moduleName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if module is part of metapackage.
|
||||
*
|
||||
* For metapackage module we should use version from metapackage, not from specific module.
|
||||
*
|
||||
* @param string $moduleName e.g. SocialLoginFree
|
||||
* @return bool
|
||||
*/
|
||||
private function shouldUseMetapackage(string $moduleName): bool
|
||||
{
|
||||
return (bool) $this->extensionsConfig->get("$moduleName/metapackage/composer_name");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get version of module.
|
||||
*
|
||||
* @param string $moduleName e.g. SocialLoginFree
|
||||
* @return string
|
||||
*/
|
||||
private function getModuleVersion(string $moduleName): string
|
||||
{
|
||||
if (! isset($this->versionsLocalCache[$moduleName]) || ! $this->versionsLocalCache[$moduleName]) {
|
||||
$moduleVersions = $this->getFromCache(self::CACHE_IDENTIFIER);
|
||||
|
||||
if (! isset($moduleVersions[$moduleName])) {
|
||||
$this->versionsLocalCache[$moduleName] = '';
|
||||
$composerFilePaths = $this->files->getComposerFiles(ComponentRegistrar::MODULE);
|
||||
|
||||
$version = $this->getModuleVersionFromAppCode($composerFilePaths, "Plumrocket/$moduleName");
|
||||
if ($version) {
|
||||
$this->versionsLocalCache[$moduleName] = $version;
|
||||
} else {
|
||||
$versions = $this->getModulesVersionFromVendorFolder($composerFilePaths);
|
||||
$this->versionsLocalCache = array_merge($this->versionsLocalCache, $versions);
|
||||
}
|
||||
|
||||
$moduleVersions = array_merge($moduleVersions, $this->versionsLocalCache);
|
||||
$this->saveToCache(self::CACHE_IDENTIFIER, $moduleVersions);
|
||||
}
|
||||
|
||||
$this->versionsLocalCache = $moduleVersions;
|
||||
}
|
||||
|
||||
return $this->versionsLocalCache[$moduleName];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get version of metapackage
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return string
|
||||
*/
|
||||
private function getMetapackageVersion(string $moduleName): string
|
||||
{
|
||||
if (! isset($this->metapackageVersions[$moduleName]) || ! $this->metapackageVersions[$moduleName]) {
|
||||
$moduleVersions = $this->getFromCache(self::METAPACKAGE_CACHE_IDENTIFIER);
|
||||
|
||||
if (! isset($moduleVersions[$moduleName])) {
|
||||
$this->metapackageVersions[$moduleName] = '';
|
||||
$composerFilePaths = $this->files->getComposerFiles(ComponentRegistrar::MODULE);
|
||||
|
||||
$version = $this->getMetapackageVersionFromAppCode($composerFilePaths, $moduleName);
|
||||
if ($version) {
|
||||
$this->metapackageVersions[$moduleName] = $version;
|
||||
} else {
|
||||
$this->metapackageVersions[$moduleName] = $this->getMetapackageVersionFromComposer($moduleName);
|
||||
}
|
||||
|
||||
$moduleVersions = array_merge($moduleVersions, $this->metapackageVersions);
|
||||
$this->saveToCache(self::METAPACKAGE_CACHE_IDENTIFIER, $moduleVersions);
|
||||
}
|
||||
|
||||
$this->metapackageVersions = $moduleVersions;
|
||||
}
|
||||
|
||||
return $this->metapackageVersions[$moduleName];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get versions from app/code/Plumrocket
|
||||
*
|
||||
* @param array $composerFilePaths
|
||||
* @param string $modulePathName
|
||||
* @return mixed|string
|
||||
*/
|
||||
private function getModuleVersionFromAppCode(array $composerFilePaths, string $modulePathName)
|
||||
{
|
||||
foreach ($composerFilePaths as $path => $absolutePath) {
|
||||
if (false !== strpos($path, "code/$modulePathName/composer.json")) {
|
||||
return $this->extractDataFromComposerJson($path)['version'];
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get versions from vendor/plumrocket/
|
||||
*
|
||||
* @param array $composerFilePaths
|
||||
* @return string[]
|
||||
*/
|
||||
private function getModulesVersionFromVendorFolder(array $composerFilePaths): array
|
||||
{
|
||||
$versions = [];
|
||||
foreach ($composerFilePaths as $path => $absolutePath) {
|
||||
if (false !== strpos($path, 'plumrocket')) {
|
||||
$data = $this->extractDataFromComposerJson($path);
|
||||
if ($data['version']) {
|
||||
$versions[$data['name']] = $data['version'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $versions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read and parse composer.json
|
||||
*
|
||||
* @param string $path
|
||||
* @return string[]
|
||||
*/
|
||||
public function extractDataFromComposerJson(string $path): array
|
||||
{
|
||||
if (0 === strpos(trim($path, '/'), 'app')
|
||||
|| 0 === strpos(trim($path, '/'), 'vendor')
|
||||
) {
|
||||
$path = BP . DIRECTORY_SEPARATOR . trim($path, '/');
|
||||
}
|
||||
|
||||
$result = ['version' => '', 'name' => ''];
|
||||
if (! file_exists($path)) {
|
||||
return $result;
|
||||
}
|
||||
$content = file_get_contents($path);
|
||||
|
||||
if ($content) {
|
||||
$jsonContent = json_decode($content, true);
|
||||
if (! empty($jsonContent['version'])) {
|
||||
$result['version'] = $jsonContent['version'];
|
||||
}
|
||||
if (! empty($jsonContent['autoload']['psr-4'])) {
|
||||
$directoryPath = trim(array_keys($jsonContent['autoload']['psr-4'])[0], '\\');
|
||||
$result['name'] = explode('\\', $directoryPath)[1];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get metapackage version from root magento composer.json file.
|
||||
*
|
||||
* Installing metapackage via composer do not create folder with composer.json file.
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return string
|
||||
*/
|
||||
private function getMetapackageVersionFromComposer(string $moduleName): string
|
||||
{
|
||||
$composerName = $this->extensionsConfig->get("$moduleName/metapackage/composer_name");
|
||||
$package = $this->composerInformation->getInstalledMagentoPackages()[$composerName] ?? [];
|
||||
return $package['version'] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Find metapackage in app/code/Plumrocket and get its version.
|
||||
*
|
||||
* @param array $composerFilePaths
|
||||
* @param string $moduleName
|
||||
* @return string
|
||||
*/
|
||||
private function getMetapackageVersionFromAppCode(array $composerFilePaths, string $moduleName): string
|
||||
{
|
||||
$directory = (string) $this->extensionsConfig->get("$moduleName/metapackage/directory");
|
||||
foreach ($composerFilePaths as $path => $absolutePath) {
|
||||
if (false !== strpos($path, "code/Plumrocket/$moduleName/composer.json")) {
|
||||
$path = str_replace("Plumrocket/$moduleName", "Plumrocket/$directory", $path);
|
||||
return $this->extractDataFromComposerJson($path)['version'];
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get versions from cache.
|
||||
*
|
||||
* @param string $key
|
||||
* @return array
|
||||
*/
|
||||
private function getFromCache(string $key): array
|
||||
{
|
||||
$moduleVersionsJson = $this->cache->load($key);
|
||||
if ($moduleVersionsJson) {
|
||||
$moduleVersions = (array) json_decode($moduleVersionsJson, true);
|
||||
} else {
|
||||
$moduleVersions = [];
|
||||
}
|
||||
return $moduleVersions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cache versions as it is heavy operation.
|
||||
*
|
||||
* @param string $key
|
||||
* @param array $moduleVersions
|
||||
*/
|
||||
private function saveToCache(string $key, array $moduleVersions): void
|
||||
{
|
||||
$this->cache->save(json_encode($moduleVersions), $key, [Config::CACHE_TAG]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
namespace Plumrocket\Base\Model;
|
||||
|
||||
use Plumrocket\Base\Model\Extension\Customer\GetKey;
|
||||
use Plumrocket\Base\Model\Extension\Customer\IsMarketplaceKey;
|
||||
|
||||
/**
|
||||
* @since 2.1.6
|
||||
*/
|
||||
class IsModuleInMarketplace
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\GetKey
|
||||
*/
|
||||
private $getCustomerKey;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\IsMarketplaceKey
|
||||
*/
|
||||
private $isMarketplaceKey;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\GetKey $getCustomerKey
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\IsMarketplaceKey $isMarketplaceKey
|
||||
*/
|
||||
public function __construct(
|
||||
GetKey $getCustomerKey,
|
||||
IsMarketplaceKey $isMarketplaceKey
|
||||
) {
|
||||
$this->getCustomerKey = $getCustomerKey;
|
||||
$this->isMarketplaceKey = $isMarketplaceKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if serial key is marketplace one.
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $moduleName): bool
|
||||
{
|
||||
$moduleName = trim($moduleName, '\\');
|
||||
if (false !== strpos($moduleName, '_')) {
|
||||
$moduleName = explode('_', $moduleName)[1];
|
||||
} elseif (false !== strpos($moduleName, '\\')) {
|
||||
$moduleName = explode('\\', $moduleName)[1];
|
||||
}
|
||||
|
||||
return $this->isMarketplaceKey->execute($this->getCustomerKey->execute($moduleName));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?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\OptionSource;
|
||||
|
||||
use Magento\Framework\Data\OptionSourceInterface;
|
||||
|
||||
/**
|
||||
* Allow creating short system config sources
|
||||
*
|
||||
* @since 2.4.1
|
||||
*/
|
||||
abstract class AbstractSource implements OptionSourceInterface
|
||||
{
|
||||
/**
|
||||
* Convert ['<value>' => '<label>'] to ['value' => '<value>', 'label' => '<label>']
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toOptionArray(): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($this->toOptionHash() as $key => $value) {
|
||||
$result[] = ['value' => $key, 'label' => $value];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return array of options
|
||||
*
|
||||
* @return array Format: array(array('<value>' => '<label>'), ...)
|
||||
*/
|
||||
abstract public function toOptionHash(): array;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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\OptionSource;
|
||||
|
||||
/**
|
||||
* Retrieve list of subscription lists (categories)
|
||||
*
|
||||
* @since 2.4.1
|
||||
*/
|
||||
class SubscriptionLists extends AbstractSource
|
||||
{
|
||||
public const PROMOTIONS_LIST = 'promotions';
|
||||
public const ANNOUNCEMENTS_LIST = 'announcements';
|
||||
public const PRODUCT_UPDATES_LIST = 'product_updates';
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function toOptionHash(): array
|
||||
{
|
||||
return [
|
||||
self::PRODUCT_UPDATES_LIST => 'Product Updates',
|
||||
self::ANNOUNCEMENTS_LIST => 'Announcements',
|
||||
self::PROMOTIONS_LIST => 'Promotions',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license/ End-user License Agreement
|
||||
*/
|
||||
|
||||
namespace Plumrocket\Base\Model\ResourceModel;
|
||||
|
||||
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
|
||||
|
||||
/**
|
||||
* @since 1.0.0
|
||||
*/
|
||||
class ExtensionAuthorization extends AbstractDb
|
||||
{
|
||||
public const TABLE_NAME = 'plumbase_product';
|
||||
|
||||
public const ID_FIELD_NAME = 'id';
|
||||
|
||||
/**
|
||||
* Initialize resource model
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _construct()
|
||||
{
|
||||
$this->_init(self::TABLE_NAME, self::ID_FIELD_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove old records
|
||||
*
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function deleteOld(): void
|
||||
{
|
||||
$condition = ['date < ?' => date('Y-m-d H:i:s', time() - 86400 * 30)];
|
||||
$this->getConnection()->delete($this->getMainTable(), $condition);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?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\Statistic\Usage;
|
||||
|
||||
use Plumrocket\Base\Api\ModuleUsageStatisticCollectorInterface;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Collector implements ModuleUsageStatisticCollectorInterface
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Statistic\Usage\StatusInterface
|
||||
*/
|
||||
private $status;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\UsageStatisticCollectorInterface[]
|
||||
*/
|
||||
private $collectors;
|
||||
|
||||
/**
|
||||
* Collector constructor.
|
||||
*
|
||||
* @param \Plumrocket\Base\Model\Statistic\Usage\StatusInterface $status
|
||||
* @param array $collectors
|
||||
*/
|
||||
public function __construct(
|
||||
StatusInterface $status,
|
||||
array $collectors = []
|
||||
) {
|
||||
$this->status = $status;
|
||||
$this->collectors = $collectors;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collect(): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($this->collectors as $type => $collector) {
|
||||
$result[$type] = $collector->collect();
|
||||
}
|
||||
|
||||
$this->status->switchToMiss();
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function hasNewData(): bool
|
||||
{
|
||||
return $this->status->check();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?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\Statistic\Usage;
|
||||
|
||||
use Plumrocket\Base\Api\GetExtensionInformationInterface;
|
||||
use Plumrocket\Base\Api\UsageStatisticCollectorInterface;
|
||||
use Plumrocket\Base\Model\ConfigUtils;
|
||||
use Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey;
|
||||
|
||||
/**
|
||||
* Class for integrate and merge other collectors data
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class CompositeCollector implements UsageStatisticCollectorInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\ConfigUtils
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\GetExtensionInformationInterface
|
||||
*/
|
||||
private $getExtensionInformation;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\ModuleUsageStatisticCollectorInterface[]
|
||||
*/
|
||||
private $moduleCollectors;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey
|
||||
*/
|
||||
private $getTrueCustomerKey;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\ConfigUtils $config
|
||||
* @param \Plumrocket\Base\Api\GetExtensionInformationInterface $getExtensionInformation
|
||||
* @param \Plumrocket\Base\Model\Extension\Customer\GetTrueCustomerKey $getTrueCustomerKey
|
||||
* @param array $moduleCollectors
|
||||
*/
|
||||
public function __construct(
|
||||
ConfigUtils $config,
|
||||
GetExtensionInformationInterface $getExtensionInformation,
|
||||
GetTrueCustomerKey $getTrueCustomerKey,
|
||||
array $moduleCollectors = []
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->getExtensionInformation = $getExtensionInformation;
|
||||
$this->moduleCollectors = $moduleCollectors;
|
||||
$this->getTrueCustomerKey = $getTrueCustomerKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function collect(): array
|
||||
{
|
||||
$modulesData = [];
|
||||
foreach ($this->moduleCollectors as $moduleName => $moduleCollector) {
|
||||
if (! $moduleCollector->hasNewData()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$extensionInformation = $this->getExtensionInformation->execute($moduleName);
|
||||
$serialKey = (string) $this->config->getStoreConfig(
|
||||
"{$extensionInformation->getConfigSection()}/general/serial"
|
||||
);
|
||||
|
||||
$modulesData[$moduleName] = [
|
||||
'license_key' => $serialKey,
|
||||
'customer_hash'=> $this->getTrueCustomerKey->execute($moduleName),
|
||||
'statistic' => $moduleCollector->collect()
|
||||
];
|
||||
}
|
||||
|
||||
return $modulesData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
<?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\Statistic\Usage\Config;
|
||||
|
||||
use Magento\Config\Model\Config\Structure\Element\Field;
|
||||
use Magento\Config\Model\Config\Structure\Element\Group;
|
||||
use Magento\Config\Model\Config\Structure\Element\Section;
|
||||
use Magento\Framework\App\Area;
|
||||
use Magento\Framework\App\Config\Initial\Reader;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\State;
|
||||
use Magento\Framework\Config\ScopeInterface;
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Framework\DataObjectFactory;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\State\InvalidTransitionException;
|
||||
use Plumrocket\Base\Model\Extensions\Information;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Collector
|
||||
{
|
||||
const ATTRIBUTE_USE_IN_STATISTIC = 'pr_use_in_statistic';
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Statistic\Usage\Config\GetConfigStructure
|
||||
*/
|
||||
private $getConfigStructure;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Config\ScopeInterface
|
||||
*/
|
||||
private $scope;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\State
|
||||
*/
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\Initial\Reader
|
||||
*/
|
||||
private $configReader;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\DataObjectFactory
|
||||
*/
|
||||
private $dataObjectFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\DataObject|null
|
||||
*/
|
||||
protected $defaultConfig;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $configSection;
|
||||
|
||||
/**
|
||||
* Collector constructor.
|
||||
*
|
||||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
||||
* @param \Plumrocket\Base\Model\Statistic\Usage\Config\GetConfigStructure $getConfigStructure
|
||||
* @param \Magento\Framework\Config\ScopeInterface $scope
|
||||
* @param \Magento\Framework\App\State $state
|
||||
* @param \Magento\Framework\App\Config\Initial\Reader $configReader
|
||||
* @param \Magento\Framework\DataObjectFactory $dataObjectFactory
|
||||
* @param string $configSection
|
||||
*/
|
||||
public function __construct(
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
GetConfigStructure $getConfigStructure,
|
||||
ScopeInterface $scope,
|
||||
State $state,
|
||||
Reader $configReader,
|
||||
DataObjectFactory $dataObjectFactory,
|
||||
string $configSection = Information::CONFIG_SECTION
|
||||
) {
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->getConfigStructure = $getConfigStructure;
|
||||
$this->scope = $scope;
|
||||
$this->state = $state;
|
||||
$this->configReader = $configReader;
|
||||
$this->dataObjectFactory = $dataObjectFactory;
|
||||
$this->configSection = $configSection;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws \Magento\Framework\Exception\State\InvalidTransitionException
|
||||
*/
|
||||
public function collect(): array
|
||||
{
|
||||
$currentScope = $this->scope->getCurrentScope();
|
||||
|
||||
$data = [];
|
||||
|
||||
try {
|
||||
$data = $this->state->emulateAreaCode(Area::AREA_ADMINHTML, function () {
|
||||
$this->scope->setCurrentScope(Area::AREA_ADMINHTML);
|
||||
|
||||
return $this->collectConfigs();
|
||||
});
|
||||
|
||||
$this->scope->setCurrentScope($currentScope);
|
||||
} catch (\Exception $e) {
|
||||
throw new InvalidTransitionException(__('%1', $e->getMessage()), $e);
|
||||
} finally {
|
||||
$this->scope->setCurrentScope($currentScope);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function collectConfigs(): array
|
||||
{
|
||||
$configData = [];
|
||||
/**
|
||||
* Use factory (getConfigStructure) in order to initialize object after code area emulated
|
||||
* @var \Magento\Config\Model\Config\Structure\Element\Section $section
|
||||
*/
|
||||
$section = $this->getConfigStructure->execute()->getElement($this->configSection);
|
||||
/** @var \Magento\Config\Model\Config\Structure\Element\Group $group */
|
||||
foreach ($section->getChildren() as $group) {
|
||||
/** @var \Magento\Config\Model\Config\Structure\Element\Field $field */
|
||||
foreach ($group->getChildren() as $field) {
|
||||
if ($field->getAttribute(self::ATTRIBUTE_USE_IN_STATISTIC)) {
|
||||
$configData[$field->getPath()] = $this->proccesField($section, $group, $field);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $configData;
|
||||
}
|
||||
|
||||
/**
|
||||
* [
|
||||
* 'label' => '',
|
||||
* 'value' => '',
|
||||
* 'options' => [
|
||||
* ['value' => value_1, 'label' => label_1],
|
||||
* ['value' => value_1, 'label' => label_1],
|
||||
* ],
|
||||
* 'default' => ''
|
||||
* ]
|
||||
*
|
||||
* @param \Magento\Config\Model\Config\Structure\Element\Section $section
|
||||
* @param \Magento\Config\Model\Config\Structure\Element\Group $group
|
||||
* @param \Magento\Config\Model\Config\Structure\Element\Field $field
|
||||
* @return array
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
protected function proccesField(
|
||||
Section $section,
|
||||
Group $group,
|
||||
Field $field
|
||||
) {
|
||||
$fieldData = [
|
||||
'label' => $field->getLabel(),
|
||||
'value' => $this->anonymizeData($field, 'value', $this->scopeConfig->getValue($field->getPath())),
|
||||
'default' => $this->getDefaultConfig()->getDataByPath($field->getPath()),
|
||||
];
|
||||
|
||||
if (in_array($field->getType(), ['select', 'multiselect'], true)) {
|
||||
$fieldData['options'] = $this->prepareOptions(
|
||||
$this->anonymizeData($field, 'options', $field->getOptions())
|
||||
);
|
||||
}
|
||||
|
||||
return $fieldData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Framework\DataObject
|
||||
*/
|
||||
protected function getDefaultConfig(): DataObject
|
||||
{
|
||||
if (null === $this->defaultConfig) {
|
||||
try {
|
||||
$config = $this->configReader->read();
|
||||
$moduleConfig = $config['data']['default'][$this->configSection] ?? [];
|
||||
} catch (LocalizedException $e) {
|
||||
$moduleConfig = [];
|
||||
}
|
||||
|
||||
$this->defaultConfig = $this->dataObjectFactory->create(
|
||||
['data' => [$this->configSection => $moduleConfig]]
|
||||
);
|
||||
}
|
||||
|
||||
return $this->defaultConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $options
|
||||
* @return array
|
||||
*/
|
||||
protected function prepareOptions(array $options) : array
|
||||
{
|
||||
return array_map(
|
||||
static function ($option) {
|
||||
$option['label'] = (string) $option['label'];
|
||||
return $option;
|
||||
},
|
||||
$options
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Magento\Config\Model\Config\Structure\Element\Field $field
|
||||
* @param string $type
|
||||
* @param $currentValue
|
||||
* @return array|mixed|string
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*/
|
||||
protected function anonymizeData(Field $field, string $type, $currentValue)
|
||||
{
|
||||
return $currentValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?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\Statistic\Usage\Config;
|
||||
|
||||
use Magento\Config\Model\Config\Structure;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class GetConfigStructure
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Config\Model\Config\StructureFactory
|
||||
*/
|
||||
private $configStructureFactory;
|
||||
|
||||
/**
|
||||
* @var \Magento\Config\Model\Config\Structure
|
||||
*/
|
||||
private $configStructure;
|
||||
|
||||
/**
|
||||
* GetConfigStructure constructor.
|
||||
*
|
||||
* @param \Magento\Config\Model\Config\StructureFactory $configStructureFactory
|
||||
*/
|
||||
public function __construct(\Magento\Config\Model\Config\StructureFactory $configStructureFactory)
|
||||
{
|
||||
$this->configStructureFactory = $configStructureFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Magento\Config\Model\Config\Structure
|
||||
*/
|
||||
public function execute(): Structure
|
||||
{
|
||||
if (null === $this->configStructure) {
|
||||
$this->configStructure = $this->configStructureFactory->create();
|
||||
}
|
||||
|
||||
return $this->configStructure;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?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\Statistic\Usage;
|
||||
|
||||
use Magento\Framework\HTTP\Client\Curl;
|
||||
use Magento\Framework\Serialize\SerializerInterface;
|
||||
use Plumrocket\Base\Model\External\Urls;
|
||||
|
||||
/**
|
||||
* Send usage statistic
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Sender
|
||||
{
|
||||
/**
|
||||
* @var Curl
|
||||
*/
|
||||
private $curl;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\Serialize\SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\HTTP\Client\Curl $curl
|
||||
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
|
||||
*/
|
||||
public function __construct(
|
||||
Curl $curl,
|
||||
SerializerInterface $serializer
|
||||
) {
|
||||
$this->curl = $curl;
|
||||
$this->serializer = $serializer;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $usageStatisticData
|
||||
* @return bool
|
||||
*/
|
||||
public function send(array $usageStatisticData): bool
|
||||
{
|
||||
try {
|
||||
$this->curl->post(
|
||||
'https://' . Urls::STATISTIC_URL,
|
||||
$this->serializer->serialize($usageStatisticData)
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->curl->getStatus() === 200;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
<?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\Statistic\Usage;
|
||||
|
||||
use Magento\Config\Model\ConfigFactory;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
class Status implements StatusInterface
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Framework\App\Config\ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\ResourceConnection
|
||||
*/
|
||||
private $resourceConnection;
|
||||
|
||||
/**
|
||||
* @var \Magento\Config\Model\ConfigFactory
|
||||
*/
|
||||
private $configFactory;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $xmlPath;
|
||||
|
||||
/**
|
||||
* SystemConfigSaveAfter constructor.
|
||||
*
|
||||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
||||
* @param \Magento\Framework\App\ResourceConnection $resourceConnection
|
||||
* @param \Magento\Config\Model\ConfigFactory $configFactory
|
||||
* @param string $xmlPath
|
||||
*/
|
||||
public function __construct(
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
ResourceConnection $resourceConnection,
|
||||
ConfigFactory $configFactory,
|
||||
string $xmlPath
|
||||
) {
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->resourceConnection = $resourceConnection;
|
||||
$this->configFactory = $configFactory;
|
||||
$this->xmlPath = $xmlPath . (strpos($xmlPath, '/') === false ? '/general/new_changes' : '');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function check(): bool
|
||||
{
|
||||
return (bool) $this->scopeConfig->getValue($this->xmlPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function switchToCollect(): StatusInterface
|
||||
{
|
||||
return $this->changeStatus(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function switchToMiss(): StatusInterface
|
||||
{
|
||||
return $this->changeStatus(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $status
|
||||
* @return $this
|
||||
*/
|
||||
private function changeStatus(int $status): self
|
||||
{
|
||||
/** @var \Magento\Config\Model\Config $config */
|
||||
$config = $this->configFactory->create();
|
||||
$connection = $this->resourceConnection->getConnection('core_write');
|
||||
|
||||
$connection->delete(
|
||||
$this->resourceConnection->getTableName('core_config_data'),
|
||||
[
|
||||
$connection->quoteInto('path = ?', $this->xmlPath)
|
||||
]
|
||||
);
|
||||
|
||||
$config->setDataByPath($this->xmlPath, $status);
|
||||
|
||||
try {
|
||||
$config->save();
|
||||
} catch (\Exception $e) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?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\Statistic\Usage;
|
||||
|
||||
/**
|
||||
* @since 2.3.0
|
||||
*/
|
||||
interface StatusInterface
|
||||
{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function check(): bool;
|
||||
|
||||
/**
|
||||
* @return \Plumrocket\Base\Model\Statistic\Usage\StatusInterface
|
||||
*/
|
||||
public function switchToCollect(): StatusInterface;
|
||||
|
||||
/**
|
||||
* @return \Plumrocket\Base\Model\Statistic\Usage\StatusInterface
|
||||
*/
|
||||
public function switchToMiss(): StatusInterface;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?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\System\Config;
|
||||
|
||||
/**
|
||||
* Allow to create short system config sources
|
||||
*
|
||||
* @since 2.3.0
|
||||
* @deprecated since 2.4.1 - it is better to keep option source in Model/OptionSource directory
|
||||
*/
|
||||
abstract class AbstractSource extends \Plumrocket\Base\Model\OptionSource\AbstractSource
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
<?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\System\Config;
|
||||
|
||||
use Magento\Config\Model\Config\Structure;
|
||||
use Magento\Config\Model\Config\Structure\Element\Section;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Plumrocket\Base\Api\ExtensionInformationListInterface;
|
||||
|
||||
/**
|
||||
* Retrieve current section in system config and allow check if it's one of plumrocket sections
|
||||
*
|
||||
* @since 2.3.1
|
||||
*/
|
||||
class CurrentSection
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Config\Model\Config\Structure
|
||||
*/
|
||||
private $configStructure;
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Api\ExtensionInformationListInterface
|
||||
*/
|
||||
private $informationList;
|
||||
|
||||
/**
|
||||
* @param \Magento\Config\Model\Config\Structure $configStructure
|
||||
* @param \Magento\Framework\App\RequestInterface $request
|
||||
* @param \Plumrocket\Base\Api\ExtensionInformationListInterface $informationList
|
||||
*/
|
||||
public function __construct(
|
||||
Structure $configStructure,
|
||||
RequestInterface $request,
|
||||
ExtensionInformationListInterface $informationList
|
||||
) {
|
||||
$this->configStructure = $configStructure;
|
||||
$this->request = $request;
|
||||
$this->informationList = $informationList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get section id.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getId(): ?string
|
||||
{
|
||||
if ($section = $this->get()) {
|
||||
return $section->getId();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current section.
|
||||
*
|
||||
* @return \Magento\Config\Model\Config\Structure\Element\Section|null
|
||||
*/
|
||||
public function get(): ?Section
|
||||
{
|
||||
$current = $this->request->getParam('section', '');
|
||||
|
||||
if (! $current) {
|
||||
try {
|
||||
$section = $this->configStructure->getFirstSection();
|
||||
} catch (LocalizedException $e) {
|
||||
$section = null;
|
||||
}
|
||||
} else {
|
||||
$section = $this->configStructure->getElement($current);
|
||||
if (! $section instanceof Section) {
|
||||
$section = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $section;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current section is related one of plumrocket extensions.
|
||||
*
|
||||
* Ad some extensions that customize admin menu can change tab we try check section id first.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isPlumrocketExtension(): bool
|
||||
{
|
||||
if ($this->isPlumrocketSectionId($this->request->getParam('section', ''))) {
|
||||
return true;
|
||||
}
|
||||
$section = $this->get();
|
||||
return $section && 'plumrocket' === $section->getAttribute('tab');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if section is declared in extensions.xml
|
||||
*
|
||||
* If the module consists of two sections, such as the Advanced Reviews and Reminders extension,
|
||||
* it may not function properly.
|
||||
*
|
||||
* @param string $sectionId
|
||||
* @return bool
|
||||
*/
|
||||
private function isPlumrocketSectionId(string $sectionId): bool
|
||||
{
|
||||
if (! $sectionId) {
|
||||
return false;
|
||||
}
|
||||
foreach ($this->informationList->getList()->getItems() as $extensionInformation) {
|
||||
if ($sectionId === $extensionInformation->getConfigSection()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2020 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
namespace Plumrocket\Base\Model\System\Config;
|
||||
|
||||
use Magento\Framework\Exception\NotFoundException;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class GetCurrentExtensionName
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\System\Config\CurrentSection
|
||||
*/
|
||||
private $currentSection;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\System\Config\CurrentSection $currentSection
|
||||
*/
|
||||
public function __construct(CurrentSection $currentSection)
|
||||
{
|
||||
$this->currentSection = $currentSection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current extension name.
|
||||
*
|
||||
* @return string
|
||||
* @throws \Magento\Framework\Exception\NotFoundException
|
||||
*/
|
||||
public function execute(): string
|
||||
{
|
||||
if ($section = $this->currentSection->get()) {
|
||||
/** @var \Magento\Config\Model\Config\Structure\Element\Group $group */
|
||||
foreach ($section->getChildren() as $group) {
|
||||
if ($group->getId() !== 'general' && $group->getId() !== 'toolbox') {
|
||||
continue;
|
||||
}
|
||||
/** @var \Magento\Config\Model\Config\Structure\Element\Field $field */
|
||||
foreach ($group->getChildren() as $field) {
|
||||
if ($field->getId() !== 'version') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (! $field->getAttribute('pr_extension_name')) {
|
||||
$versionFieldData = $field->getData();
|
||||
// TODO: remove "if" after changing system config in all modules
|
||||
if (isset($versionFieldData['frontend_model'])) {
|
||||
return explode('\\', $versionFieldData['frontend_model'])[1];
|
||||
}
|
||||
throw new NotFoundException(__('Version field does not contain extension name.'));
|
||||
}
|
||||
|
||||
return $field->getAttribute('pr_extension_name');
|
||||
}
|
||||
}
|
||||
|
||||
throw new NotFoundException(__('Not found version field'));
|
||||
}
|
||||
|
||||
throw new NotFoundException(__('Not found section'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?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\Theme;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
|
||||
use Magento\Framework\View\DesignInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
/**
|
||||
* @since 2.3.5
|
||||
*/
|
||||
class GetInformation
|
||||
{
|
||||
|
||||
public const THEME_CODE_DEFAULT = 'Magento/blank';
|
||||
|
||||
/** @var ScopeConfigInterface */
|
||||
private $scopeConfig;
|
||||
|
||||
/** @var ThemeProviderInterface */
|
||||
private $themeProvider;
|
||||
|
||||
/** @var StoreManagerInterface */
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
|
||||
* @param \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
*/
|
||||
public function __construct(
|
||||
ScopeConfigInterface $scopeConfig,
|
||||
ThemeProviderInterface $themeProvider,
|
||||
StoreManagerInterface $storeManager
|
||||
) {
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
$this->themeProvider = $themeProvider;
|
||||
$this->storeManager = $storeManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getVendor(): string
|
||||
{
|
||||
return mb_strstr(mb_strtolower($this->getCode()), '/', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return str_replace('/', '-', mb_strtolower($this->getCode()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCode(): string
|
||||
{
|
||||
$themeId = $this->scopeConfig->getValue(
|
||||
DesignInterface::XML_PATH_THEME_ID,
|
||||
ScopeInterface::SCOPE_STORE,
|
||||
$this->storeManager->getStore()->getId()
|
||||
);
|
||||
|
||||
$themeData = $this->themeProvider->getThemeById($themeId)->getData();
|
||||
return $themeData['code'] ?? self::THEME_CODE_DEFAULT;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2023 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Plumrocket\Base\Model\ConfigUtils;
|
||||
|
||||
/**
|
||||
* @since 2.9.4
|
||||
*/
|
||||
class AdminConfig
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Magento\Framework\App\RequestInterface
|
||||
*/
|
||||
private $request;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\ConfigUtils
|
||||
*/
|
||||
private $configUtils;
|
||||
|
||||
/**
|
||||
* @param \Magento\Framework\App\RequestInterface $request
|
||||
* @param \Plumrocket\Base\Model\ConfigUtils $configUtils
|
||||
*/
|
||||
public function __construct(
|
||||
RequestInterface $request,
|
||||
ConfigUtils $configUtils
|
||||
) {
|
||||
$this->request = $request;
|
||||
$this->configUtils = $configUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get config for current scope selected in admin.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public function getScopedConfig(string $path): string
|
||||
{
|
||||
if ($scopeCode = $this->request->getParam('website')) {
|
||||
$scopeType = ScopeInterface::SCOPE_WEBSITE;
|
||||
} elseif ($scopeCode = $this->request->getParam('store')) {
|
||||
$scopeType = ScopeInterface::SCOPE_STORE;
|
||||
} else {
|
||||
$scopeCode = null;
|
||||
$scopeType = ScopeConfigInterface::SCOPE_TYPE_DEFAULT;
|
||||
}
|
||||
return (string) $this->configUtils->getConfig($path, $scopeCode, $scopeType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?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\Utils;
|
||||
|
||||
use Plumrocket\Base\Api\ConfigUtilsInterface;
|
||||
use Plumrocket\Base\Model\ConfigUtils;
|
||||
|
||||
/**
|
||||
* @since 2.5.2
|
||||
* @deprecated since 2.10.0
|
||||
* @see \Plumrocket\Base\Model\ConfigUtils
|
||||
*/
|
||||
class Config extends ConfigUtils implements ConfigUtilsInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2022 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
/**
|
||||
* @since 2.6.0
|
||||
*/
|
||||
class Debug
|
||||
{
|
||||
|
||||
/**
|
||||
* Get backtrace as a table
|
||||
*
|
||||
* @param string $title
|
||||
* @return string
|
||||
* phpcs:disable Magento2.Functions.StaticFunction
|
||||
*/
|
||||
public static function backtrace(string $title = 'Debug Backtrace:'): string
|
||||
{
|
||||
$rows = '';
|
||||
foreach (debug_backtrace() as $stack) {
|
||||
if (! isset($stack['file'])) {
|
||||
$stack['file'] = '[PHP Kernel]';
|
||||
}
|
||||
if (! isset($stack['line'])) {
|
||||
$stack['line'] = '';
|
||||
}
|
||||
$rows .= '<tr>' .
|
||||
"<td>{$stack['file']}</td>" .
|
||||
"<td>{$stack['line']}</td>".
|
||||
"<td>{$stack['function']}</td>" .
|
||||
'</tr>';
|
||||
}
|
||||
|
||||
return <<<HTML
|
||||
<hr>
|
||||
<style>
|
||||
#pr_debug_bactrace,
|
||||
#pr_debug_bactrace th,
|
||||
#pr_debug_bactrace td {
|
||||
border: 1px solid black;
|
||||
border-collapse: collapse;
|
||||
padding: 4px;
|
||||
}
|
||||
#pr_debug_bactrace tr:nth-child(2n) {
|
||||
background-color: #e5e5e5;
|
||||
}
|
||||
</style>
|
||||
<p>$title</p>
|
||||
<table id="pr_debug_bactrace">
|
||||
<tr>
|
||||
<th><strong>File</strong></th>
|
||||
<th><strong>Line</strong></th>
|
||||
<th><strong>Function</strong></th>
|
||||
</tr>
|
||||
$rows
|
||||
</table>
|
||||
<hr>
|
||||
HTML;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?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\Utils;
|
||||
|
||||
use Magento\Framework\App\Request\Http;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Plumrocket\Base\Library\Mobile\Detect;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class DeviceDetect implements DeviceDetectInterface
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Library\Mobile\Detect
|
||||
*/
|
||||
private $mobileDetect;
|
||||
|
||||
/**
|
||||
* @var bool[]
|
||||
*/
|
||||
private $cache = [];
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Library\Mobile\Detect $mobileDetect
|
||||
*/
|
||||
public function __construct(Detect $mobileDetect)
|
||||
{
|
||||
$this->mobileDetect = $mobileDetect;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isMobile(RequestInterface $request): bool
|
||||
{
|
||||
if (! $request instanceof Http) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$key = 'mobile';
|
||||
if (! isset($this->cache[$key])) {
|
||||
$this->cache[$key] = $this->mobileDetect->isMobile();
|
||||
}
|
||||
|
||||
return $this->cache[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function isTablet(RequestInterface $request): bool
|
||||
{
|
||||
if (! $request instanceof Http) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$key = 'tablet';
|
||||
if (! isset($this->cache[$key])) {
|
||||
$this->cache[$key] = $this->mobileDetect->isTablet();
|
||||
}
|
||||
|
||||
return $this->cache[$key];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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\Utils;
|
||||
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
interface DeviceDetectInterface
|
||||
{
|
||||
/**
|
||||
* Check whether current device is mobile
|
||||
*
|
||||
* @param \Magento\Framework\App\RequestInterface $request
|
||||
* @return bool
|
||||
*/
|
||||
public function isMobile(RequestInterface $request): bool;
|
||||
|
||||
/**
|
||||
* Check whether current device is tablet
|
||||
*
|
||||
* @param \Magento\Framework\App\RequestInterface $request
|
||||
* @return bool
|
||||
*/
|
||||
public function isTablet(RequestInterface $request): bool;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?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\Utils;
|
||||
|
||||
use Magento\Store\Model\Store;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Plumrocket\Base\Model\ConfigUtils;
|
||||
|
||||
/**
|
||||
* @since 2.5.0
|
||||
*/
|
||||
class GetEnabledStoresUrls
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Magento\Store\Model\StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\ConfigUtils
|
||||
*/
|
||||
private $configUtils;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\ConfigUtils $configUtils
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
*/
|
||||
public function __construct(
|
||||
ConfigUtils $configUtils,
|
||||
StoreManagerInterface $storeManager
|
||||
) {
|
||||
$this->storeManager = $storeManager;
|
||||
$this->configUtils = $configUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve list of unique enabled stores urls
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function execute(): array
|
||||
{
|
||||
$storesUrls = [];
|
||||
foreach ($this->storeManager->getStores() as $store) {
|
||||
if ($store->getIsActive()) {
|
||||
$storesUrls[] = $this->configUtils->getStoreConfig(
|
||||
Store::XML_PATH_SECURE_BASE_URL,
|
||||
$store->getId()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return array_unique($storesUrls);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?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\Utils;
|
||||
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
/**
|
||||
* @since 2.3.1
|
||||
*/
|
||||
class GetRelativePathFromUrl
|
||||
{
|
||||
/**
|
||||
* @var \Magento\Store\Model\StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
/**
|
||||
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
*/
|
||||
public function __construct(StoreManagerInterface $storeManager)
|
||||
{
|
||||
$this->storeManager = $storeManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove base url from giving url.
|
||||
*
|
||||
* @param string $url
|
||||
* @param bool $removeGetParams
|
||||
* @param bool $removeFragment
|
||||
* @return string
|
||||
*/
|
||||
public function execute(string $url, bool $removeGetParams = true, bool $removeFragment = true): string
|
||||
{
|
||||
$baseUrl = $this->storeManager->getStore()->getBaseUrl();
|
||||
|
||||
$url = str_replace(["\n", "\r"], '', $url);
|
||||
|
||||
if ($removeGetParams && $this->hasParams($url)) {
|
||||
$paramsWithFragment = strstr($url, '?');
|
||||
if ($removeFragment) {
|
||||
$url = str_replace($paramsWithFragment, '', $url);
|
||||
} else {
|
||||
$url = str_replace(strstr($paramsWithFragment, '#', true), '', $url);
|
||||
}
|
||||
}
|
||||
|
||||
if ($removeFragment && $this->hasFragment($url)) {
|
||||
$url = str_replace(strstr($url, '#'), '', $url);
|
||||
}
|
||||
|
||||
$url = str_replace($baseUrl, '', $url);
|
||||
if ('' === $url || $url[0] !== '/') {
|
||||
$url = '/' . $url;
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if url has get params.
|
||||
*
|
||||
* @param string $url
|
||||
* @return bool
|
||||
*/
|
||||
private function hasParams(string $url): bool
|
||||
{
|
||||
return strpos($url, '?') !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if url has fragment (hash).
|
||||
*
|
||||
* @param string $url
|
||||
* @return bool
|
||||
*/
|
||||
private function hasFragment(string $url): bool
|
||||
{
|
||||
return strpos($url, '#') !== false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,492 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2023 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
|
||||
/**
|
||||
* @api
|
||||
* @since 2.11.0
|
||||
*/
|
||||
class HtmlTag
|
||||
{
|
||||
|
||||
/**
|
||||
* Get first tag from html.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return string
|
||||
*/
|
||||
public function get(string $tagName, string $html): string
|
||||
{
|
||||
if ($this->isSelfClosingTag($tagName)) {
|
||||
if (! preg_match("/<$tagName>|<$tagName\s[^>]*?>/", $html, $results)) {
|
||||
return '';
|
||||
}
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
if (! preg_match("/(<$tagName>|<$tagName\s[^>]*?>)[\s\S]*?<\/$tagName>/", $html, $results)) {
|
||||
return '';
|
||||
}
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all image tags from html.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return array
|
||||
*/
|
||||
public function getAll(string $tagName, string $html): array
|
||||
{
|
||||
if ($this->isSelfClosingTag($tagName)) {
|
||||
if (! preg_match_all("/<$tagName>|<$tagName\s[^>]*?>/", $html, $results)) {
|
||||
return [];
|
||||
}
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
if (! preg_match_all("/(<$tagName>|<$tagName\s[^>]*?>)[\s\S]*?<\/$tagName>/", $html, $results)) {
|
||||
return [];
|
||||
}
|
||||
return $results[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tags with specific content.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @param string $tagContent
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function getAllWithContent(string $tagName, string $html, string $tagContent): array
|
||||
{
|
||||
$tags = $this->getAll($tagName, $html);
|
||||
$result = [];
|
||||
foreach ($tags as $tag) {
|
||||
if (false !== strpos($tag, $tagContent)) {
|
||||
$result[] = $tag;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if tag is exist in html.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return bool
|
||||
*/
|
||||
public function isExistElement(string $tagName, string $html): bool
|
||||
{
|
||||
return (bool) $this->get($tagName, $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse html
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return string
|
||||
*/
|
||||
public function getTagContent(string $tagName, string $html): string
|
||||
{
|
||||
preg_match('#<' .$tagName . '[\s\S]*?>([^`]*?)<\/' . $tagName . '>#', $html, $matches);
|
||||
return (string) $matches[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse html
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return array
|
||||
*/
|
||||
public function getTagContents(string $tagName, string $html): array
|
||||
{
|
||||
preg_match_all('#<' .$tagName . '[\s\S]*?>([^`]*?)<\/' . $tagName . '>#', $html, $matches);
|
||||
return $matches[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if tag is self-closing.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @return bool
|
||||
*/
|
||||
public function isSelfClosingTag(string $tagName): bool
|
||||
{
|
||||
return in_array(
|
||||
$tagName,
|
||||
[
|
||||
'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
|
||||
'link', 'meta', 'param', 'source', 'track', 'wbr',
|
||||
],
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cut tag from HTML.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return array
|
||||
*/
|
||||
public function cut(string $tagName, string $html): array
|
||||
{
|
||||
$tag = $this->get($tagName, $html);
|
||||
if (! $tag) {
|
||||
return ['', $html];
|
||||
}
|
||||
$html = str_replace($tag, '', $html);
|
||||
return [$tag, $html];
|
||||
}
|
||||
|
||||
/**
|
||||
* Cut tags from HTML.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @return array
|
||||
*/
|
||||
public function cutAll(string $tagName, string $html): array
|
||||
{
|
||||
$tags = $this->getAll($tagName, $html);
|
||||
if (! $tags) {
|
||||
return [[], $html];
|
||||
}
|
||||
$html = str_replace($tags, '', $html);
|
||||
return [$tags, $html];
|
||||
}
|
||||
|
||||
/**
|
||||
* Cut first tag with specific content.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @param string $tagContent
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function cutWithContent(string $tagName, string $html, string $tagContent): array
|
||||
{
|
||||
$tags = $this->getAll($tagName, $html);
|
||||
foreach ($tags as $tag) {
|
||||
if (false !== strpos($tag, $tagContent)) {
|
||||
$html = str_replace($tag, '', $html);
|
||||
return [$tag, $html];
|
||||
}
|
||||
}
|
||||
return ['', $html];
|
||||
}
|
||||
|
||||
/**
|
||||
* Cut tags with specific content.
|
||||
*
|
||||
* @param string $tagName
|
||||
* @param string $html
|
||||
* @param string $tagContent
|
||||
* @return array|string[]
|
||||
*/
|
||||
public function cutAllWithContent(string $tagName, string $html, string $tagContent): array
|
||||
{
|
||||
$tags = $this->getAll($tagName, $html);
|
||||
$result = [];
|
||||
foreach ($tags as $tag) {
|
||||
if (false !== strpos($tag, $tagContent)) {
|
||||
$html = str_replace($tag, '', $html);
|
||||
$result[] = $tag;
|
||||
}
|
||||
}
|
||||
return [$result, $html];
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a html string before the first child of the tag.
|
||||
*
|
||||
* @param string $html
|
||||
* @param string $tagName
|
||||
* @param string $htmlString
|
||||
* @return string
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function prepend(string $html, string $tagName, string $htmlString): string
|
||||
{
|
||||
if ($this->isSelfClosingTag($tagName)) {
|
||||
throw new LocalizedException(__('Cannot prepend html for self-closing tag %1.', $tagName));
|
||||
}
|
||||
|
||||
if (! $this->isExistElement($tagName, $html)) {
|
||||
throw new LocalizedException(__('Cannot find element %1 in which we have to prepend html.', $tagName));
|
||||
}
|
||||
|
||||
if (! $htmlString) {
|
||||
return $html;
|
||||
}
|
||||
return preg_replace("/(<$tagName>|<$tagName\s[^>]*?>)/", "$1\n$htmlString", $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a html string after the last child of the tag.
|
||||
*
|
||||
* @param string $html
|
||||
* @param string $tagName
|
||||
* @param string $htmlString
|
||||
* @return string
|
||||
* @throws \Magento\Framework\Exception\LocalizedException
|
||||
*/
|
||||
public function append(string $html, string $tagName, string $htmlString): string
|
||||
{
|
||||
if ($this->isSelfClosingTag($tagName)) {
|
||||
throw new LocalizedException(__('Cannot append html for self-closing tag %1.', $tagName));
|
||||
}
|
||||
|
||||
if (! $this->isExistElement($tagName, $html)) {
|
||||
throw new LocalizedException(__('Cannot find element %1 in which we have to append html.', $tagName));
|
||||
}
|
||||
|
||||
if (! $htmlString) {
|
||||
return $html;
|
||||
}
|
||||
return str_replace("</$tagName>", "\n$htmlString\n</$tagName>", $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove empty lines from html.
|
||||
*
|
||||
* @param string $html
|
||||
* @return string
|
||||
*/
|
||||
public function removeEmptyLines(string $html): string
|
||||
{
|
||||
return preg_replace('/\n{2,}/', '', $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute value.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @param bool $useDomDocument
|
||||
* @return string
|
||||
*/
|
||||
public function getAttribute(string $tagHtml, string $attributeName, bool $useDomDocument = true): string
|
||||
{
|
||||
if (! $tagHtml || false === strpos($tagHtml, $attributeName)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$tagName = $this->getTagName($tagHtml);
|
||||
if (! $tagName) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (! $useDomDocument || in_array($tagName, ['source', 'section', 'figure'], true)) {
|
||||
return $this->getAttributeByRegex($tagHtml, $attributeName, $tagName);
|
||||
}
|
||||
|
||||
try {
|
||||
$doc = new \DOMDocument();
|
||||
$doc->loadHTML($tagHtml);
|
||||
/** @var \DOMElement $element */
|
||||
$element = $doc->getElementsByTagName($tagName)[0];
|
||||
return $element->getAttribute($attributeName);
|
||||
} catch (\Exception $e) {
|
||||
return $this->getAttributeByRegex($tagHtml, $attributeName, $tagName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute value by regex.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @param string $tagName
|
||||
* @return string
|
||||
*/
|
||||
private function getAttributeByRegex(string $tagHtml, string $attributeName, string $tagName = ''): string
|
||||
{
|
||||
$tagName = $tagName ?: $this->getTagName($tagHtml);
|
||||
$found = preg_match(
|
||||
"/<{$tagName}[\s\S]*?\s$attributeName\s*?=\s*?['\"]([\s\S]*?)['\"][\s\S]*?>/",
|
||||
$tagHtml,
|
||||
$result
|
||||
);
|
||||
return $found ? trim((string) $result[1]) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attributes from tag.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param array $attributes
|
||||
* @return array
|
||||
*/
|
||||
public function getAttributes(string $tagHtml, array $attributes): array
|
||||
{
|
||||
if (! $tagHtml) {
|
||||
return array_fill_keys($attributes, '');
|
||||
}
|
||||
|
||||
$tagName = $this->getTagName($tagHtml);
|
||||
|
||||
try {
|
||||
$doc = new \DOMDocument();
|
||||
$doc->loadHTML($tagHtml);
|
||||
/** @var \DOMElement $element */
|
||||
$element = $doc->getElementsByTagName($tagName)[0];
|
||||
$result = [];
|
||||
foreach ($attributes as $attributeName) {
|
||||
$result[$attributeName] = $element->getAttribute($attributeName);
|
||||
}
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
$result = [];
|
||||
foreach ($attributes as $attributeName) {
|
||||
$result[$attributeName] = $this->getAttributeByRegex($tagHtml, $attributeName, $tagName);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set attribute value to tag.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function setAttribute(string $tagHtml, string $attributeName, string $value): string
|
||||
{
|
||||
if ($this->getAttribute($tagHtml, $attributeName)) {
|
||||
$tagHtml = preg_replace(
|
||||
"/(<\S[\s\S]*?)\s$attributeName\s*?=\s*?['\"][\s\S]*?['\"]([\s\S]*?>)/",
|
||||
"$1 $attributeName=\"$value\"$2",
|
||||
$tagHtml
|
||||
);
|
||||
} else {
|
||||
$tagName = $this->getTagName($tagHtml);
|
||||
$tagHtml = preg_replace("/^<$tagName/", "<$tagName $attributeName=\"$value\"", $tagHtml);
|
||||
}
|
||||
return $tagHtml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name of first tag in html.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @return string
|
||||
*/
|
||||
public function getTagName(string $tagHtml): string
|
||||
{
|
||||
if (! preg_match('/(?:<(\w+?)\s|<(\S+?)>)/', $tagHtml, $result)) {
|
||||
return '';
|
||||
}
|
||||
return $result[1] ?: $result[2];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove attribute from tag.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @return string
|
||||
*/
|
||||
public function removeAttribute(string $tagHtml, string $attributeName): string
|
||||
{
|
||||
if (! $this->getAttribute($tagHtml, $attributeName, false)) {
|
||||
if (false === strpos($tagHtml, $attributeName)) {
|
||||
return $tagHtml;
|
||||
}
|
||||
$tagHtml = $this->removeEmptyAttribute($tagHtml, $attributeName);
|
||||
return $this->removeAttributeWithoutValue($tagHtml, $attributeName);
|
||||
}
|
||||
return $this->removeExistingAttribute($tagHtml, $attributeName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Join two parts of the tag nicely.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $beginning
|
||||
* @param string $end
|
||||
* @return string
|
||||
*/
|
||||
private function joinTag(string $tagHtml, string $beginning, string $end): string
|
||||
{
|
||||
$beginning = trim($beginning);
|
||||
$end = trim($end, "\t\n\r");
|
||||
$lineCount = substr_count($tagHtml, "\n");
|
||||
if (substr_count($beginning, "\n") + substr_count($end, "\n") !== $lineCount) {
|
||||
return "$beginning\n$end";
|
||||
}
|
||||
if (0 === strpos($end, '>')) {
|
||||
return "$beginning$end";
|
||||
}
|
||||
return "$beginning $end";
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove attributes like src in <img src="">.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @return string
|
||||
*/
|
||||
private function removeEmptyAttribute(string $tagHtml, string $attributeName): string
|
||||
{
|
||||
return $this->removeExistingAttribute($tagHtml, $attributeName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove attributes like required in <input required>.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @return string
|
||||
*/
|
||||
private function removeAttributeWithoutValue(string $tagHtml, string $attributeName): string
|
||||
{
|
||||
preg_match("/(<\S[\s\S]*?)\s$attributeName\s*?([\s\S]*?>)/", $tagHtml, $result);
|
||||
if (empty($result[1]) || empty($result[2])) {
|
||||
return $tagHtml;
|
||||
}
|
||||
return $this->joinTag($tagHtml, $result[1], $result[2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Do not check if attribute is exists.
|
||||
*
|
||||
* @param string $tagHtml
|
||||
* @param string $attributeName
|
||||
* @return string
|
||||
*/
|
||||
private function removeExistingAttribute(string $tagHtml, string $attributeName): string
|
||||
{
|
||||
if (false === strpos($tagHtml, "\n")) {
|
||||
return preg_replace(
|
||||
"/(<\S[\s\S]*?)\s$attributeName\s*?=\s*?['\"][\s\S]*?['\"]([\s\S]*?>)/",
|
||||
"$1$2",
|
||||
$tagHtml
|
||||
);
|
||||
}
|
||||
preg_match("/(<\S[\s\S]*?)\s$attributeName\s*?=\s*?['\"][\s\S]*?['\"]([\s\S]*?>)/", $tagHtml, $result);
|
||||
if (empty($result[1]) || empty($result[2])) {
|
||||
return $tagHtml;
|
||||
}
|
||||
return $this->joinTag($tagHtml, $result[1], $result[2]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Plumrocket_Base
|
||||
* @copyright Copyright (c) 2022 Plumrocket Inc. (https://plumrocket.com)
|
||||
* @license https://plumrocket.com/license End-user License Agreement
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Plumrocket\Base\Model\Utils;
|
||||
|
||||
/**
|
||||
* @since 2.9.3
|
||||
*/
|
||||
class IsMatchFilePath
|
||||
{
|
||||
|
||||
/**
|
||||
* Check if file path match given pattern.
|
||||
*
|
||||
* @param string $filePath
|
||||
* @param string $pattern
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $filePath, string $pattern): bool
|
||||
{
|
||||
if ('' === $pattern) {
|
||||
return false;
|
||||
}
|
||||
return 1 === preg_match($this->patternToRegex($pattern), $filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if file path match at least one of patterns.
|
||||
*
|
||||
* @param string $filePath
|
||||
* @param array $patterns
|
||||
* @return bool
|
||||
*/
|
||||
public function executeList(string $filePath, array $patterns): bool
|
||||
{
|
||||
foreach ($patterns as $pattern) {
|
||||
if ($this->execute($filePath, $pattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert pattern to regex.
|
||||
*
|
||||
* @param string $pattern
|
||||
* @return string
|
||||
*/
|
||||
public function patternToRegex(string $pattern): string
|
||||
{
|
||||
if (0 === strpos($pattern, 'regex::')) {
|
||||
return str_replace('regex::', '', $pattern);
|
||||
}
|
||||
|
||||
$mapping = [
|
||||
'\\\\\\+' => '[+]',
|
||||
'\\\\\\*' => '[*]',
|
||||
'/' => '\/',
|
||||
'\*' => '(?:.*)',
|
||||
'\+' => '(?:.+)',
|
||||
];
|
||||
|
||||
$regex = str_replace(
|
||||
array_keys($mapping),
|
||||
array_values($mapping),
|
||||
preg_quote($pattern, '~')
|
||||
);
|
||||
|
||||
return '~^' . $regex . '$~';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?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\Utils;
|
||||
|
||||
/**
|
||||
* Check if url is matched by pattern, compare url without "get" params and fragment
|
||||
*
|
||||
* @since 2.3.1
|
||||
*/
|
||||
class IsMatchUrl
|
||||
{
|
||||
/**
|
||||
* @var \Plumrocket\Base\Model\Utils\GetRelativePathFromUrl
|
||||
*/
|
||||
private $getRelativePathFromUrl;
|
||||
|
||||
/**
|
||||
* @param \Plumrocket\Base\Model\Utils\GetRelativePathFromUrl $getRelativePathFromUrl
|
||||
*/
|
||||
public function __construct(GetRelativePathFromUrl $getRelativePathFromUrl)
|
||||
{
|
||||
$this->getRelativePathFromUrl = $getRelativePathFromUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if url match the pattern.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $pattern
|
||||
* @return bool
|
||||
*/
|
||||
public function execute(string $url, string $pattern): bool
|
||||
{
|
||||
if ('' === $pattern) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$relativeUrl = $this->getRelativePathFromUrl->execute($url);
|
||||
$regex = $this->patternToRegex($pattern);
|
||||
return 1 === preg_match($regex, $relativeUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if url match at least one of patterns.
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $patterns
|
||||
* @return bool
|
||||
*/
|
||||
public function executeList(string $url, array $patterns): bool
|
||||
{
|
||||
foreach ($patterns as $pattern) {
|
||||
if ($this->execute($url, $pattern)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert pattern to regex.
|
||||
*
|
||||
* @param string $pattern
|
||||
* @return string|string[]
|
||||
*/
|
||||
private function patternToRegex(string $pattern): string
|
||||
{
|
||||
if (0 === strpos($pattern, 'regex::')) {
|
||||
return str_replace('regex::', '', $pattern);
|
||||
}
|
||||
|
||||
if (false !== strpos($pattern, 'http://') || false !== strpos($pattern, 'https://')) {
|
||||
$pattern = $this->getRelativePathFromUrl->execute($pattern);
|
||||
} elseif ('' === $pattern || ! in_array($pattern[0], ['/', '*', '+'], true)) {
|
||||
$pattern = '/' . $pattern;
|
||||
}
|
||||
|
||||
$mapping = [
|
||||
'\\\\\\+' => '[+]',
|
||||
'\\\\\\*' => '[*]',
|
||||
'/' => '\/',
|
||||
'\*' => '(?:.*)',
|
||||
'\+' => '(?:.+)',
|
||||
];
|
||||
|
||||
$regex = str_replace(
|
||||
array_keys($mapping),
|
||||
array_values($mapping),
|
||||
preg_quote($pattern, '~')
|
||||
);
|
||||
|
||||
return '~^' . $regex . '$~';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user