ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client;
use Amasty\ElasticSearch\Exception\NotAmastySearchEngine;
use Magento\Framework\ObjectManagerInterface;
class ClientFactory
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var array
*/
private $engines;
/**
* @var ConfigBuilder
*/
private $configBuilder;
public function __construct(
ConfigBuilder $configBuilder,
ObjectManagerInterface $objectManager,
array $engines = []
) {
$this->configBuilder = $configBuilder;
$this->objectManager = $objectManager;
$this->engines = $engines;
}
/**
* @throws NotAmastySearchEngine
*/
public function create(string $searchEngine, array $options): ClientInterface
{
if (!isset($this->engines[$searchEngine])) {
throw new NotAmastySearchEngine(__('Selected engine not supported by Amasty_Elasticsearch module.'));
}
/** @var ClientInterface $client */
$client = $this->objectManager->create($this->engines[$searchEngine], [
'options' => $this->configBuilder->build($searchEngine, $options)
]);
return $client;
}
}
@@ -0,0 +1,112 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client;
interface ClientInterface
{
/**
* @param array $query
* @return array
*/
public function query(array $query): array;
/**
* @param array $query
* @return array
* @throws \Amasty\ElasticSearch\Exception\Missing404Exception
*/
public function search(array $query): array;
/**
* @param array $query
* @return array
*/
public function bulkQuery(array $query): array;
/**
* @param string $index
* @return bool
*/
public function isEmptyIndex(string $index): bool;
/**
* @param string $index
* @param array $settings
* @return void
*/
public function createIndex(string $index, array $settings): void;
/**
* @param string $index
* @return void
*/
public function deleteIndex(string $index): void;
/**
* @param string $index
* @return bool
*/
public function indexExists(string $index): bool;
/**
* @param string $alias
* @param string $index
* @return bool
* @throws \Amasty\ElasticSearch\Exception\NoNodesAvailableException
*/
public function existsAlias(string $alias, string $index = ''): bool;
/**
* @param string $alias
* @return array
*/
public function getAlias(string $alias): array;
/**
* @param string $alias
* @param string $newIndex
* @return void
*/
public function updateAlias(string $alias, string $newIndex): void;
/**
* @param array $params
* @return void
*/
public function updateAliases(array $params): void;
/**
* @param array $params
* @return void
*/
public function putMapping(array $params): void;
/**
* @return bool
*/
public function ping(): bool;
/**
* Check if correct system package installed.
* @return bool
*/
public function isSystemPackageAvailable(): bool;
/**
* Get version of system package needed to install.
* @return string
*/
public function getNeededSystemPackageVersion(): string;
/**
* @return string
*/
public function getSystemPackageName(): string;
public function getCurrentEngineVersion(): string;
}
@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client;
use Amasty\ElasticSearch\Model\Config;
use Magento\Framework\Serialize\Serializer\Json as Serializer;
class ClientRepository implements ClientRepositoryInterface
{
/**
* @var Config
*/
private $config;
/**
* @var Serializer
*/
private $serializer;
/**
* @var ClientFactory
*/
private $clientFactory;
/**
* @var ClientInterface[] ['key' => client, ...]
*/
private $clients = [];
public function __construct(
Config $config,
Serializer $serializer,
ClientFactory $clientFactory
) {
$this->config = $config;
$this->serializer = $serializer;
$this->clientFactory = $clientFactory;
}
public function get(?string $searchEngine = null, ?array $options = null): ClientInterface
{
$searchEngine = $searchEngine ?? $this->config->getEngine();
$options = $options ?? $this->config->prepareConnectionData($options ?? []);
$cacheKey = $this->getCacheKey($searchEngine, $options);
if (!isset($this->clients[$cacheKey])) {
$this->clients[$cacheKey] = $this->clientFactory->create($searchEngine, $options);
}
return $this->clients[$cacheKey];
}
private function getCacheKey(string $searchEngine, array $options): string
{
$options['engine'] = $searchEngine;
$serializeData = $this->serializer->serialize(ksort($options));
return sha1($serializeData);
}
}
@@ -0,0 +1,21 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client;
/**
* Interface ClientRepositoryInterface
*/
interface ClientRepositoryInterface
{
/**
* @param string|null $searchEngine
* @param array|null $options
* @return \Amasty\ElasticSearch\Model\Client\ClientInterface
*/
public function get(?string $searchEngine = null, ?array $options = null): ClientInterface;
}
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\RuntimeException;
class ConfigBuilder
{
/**
* @var ConfigBuilderInterface[] ['engine' => builder, ...]
*/
private $configBuilders;
public function __construct(array $configBuilders = [])
{
$this->configBuilders = $configBuilders;
}
/**
* @throws LocalizedException
*/
public function build(string $engine, array $clientOptions): array
{
$configBuilder = $this->configBuilders[$engine] ?? $this->configBuilders['default'] ?? null;
if ($configBuilder === null) {
throw new RuntimeException(__('Config builder not set.'));
}
return $configBuilder->build($clientOptions);
}
}
@@ -0,0 +1,18 @@
<?php
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client;
interface ConfigBuilderInterface
{
/**
* @param array $clientOptions
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function build(array $clientOptions): array;
}
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client\Defaults;
use Amasty\ElasticSearch\Model\Client\ConfigBuilderInterface;
use Magento\Framework\Exception\LocalizedException;
class ConfigBuilder implements ConfigBuilderInterface
{
public function build(array $clientOptions): array
{
if (empty($clientOptions['hostname'])
|| (array_key_exists('enableAuth', $clientOptions) && 1 == $clientOptions['enableAuth']
&& (empty($clientOptions['username']) || empty($clientOptions['password'])))
) {
throw new LocalizedException(__(
'It is impossible to perform the search. Please make sure Elasticsearch Engine'
. ' is installed on the server and configured properly.'
));
}
$host = preg_replace('/http[s]?:\/\//i', '', $clientOptions['hostname']);
//@codingStandardsIgnoreLine
$protocol = parse_url($clientOptions['hostname'], PHP_URL_SCHEME) ?: 'http';
if (!empty($clientOptions['port'])) {
$host .= ':' . $clientOptions['port'];
}
if (!empty($clientOptions['enableAuth']) && ($clientOptions['enableAuth'] == 1)) {
$host = sprintf('%s:%s@%s', $clientOptions['username'], $clientOptions['password'], $host);
}
$host = sprintf('%s://%s', $protocol, $host);
$clientOptions['hosts'] = [$host];
return $clientOptions;
}
}
@@ -0,0 +1,192 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client\Elasticsearch7;
use Amasty\ElasticSearch\Exception\Missing404Exception as AmastyMissing404Exception;
use Amasty\ElasticSearch\Exception\NoNodesAvailableException as AmastyNoNodesAvailableException;
use Amasty\ElasticSearch\Exception\SystemPackageNotInstalled;
use Amasty\ElasticSearch\Model\Client\ClientInterface;
use Elasticsearch\Client as ElasticsearchClient;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use Elasticsearch\Common\Exceptions\NoNodesAvailableException;
use Magento\Framework\Exception\LocalizedException;
class Client implements ClientInterface
{
/**
* @var array
*/
private $options;
/**
* @var ElasticsearchClient[] ['pid' => client, ...]
*/
private $client = [];
/**
* @var string|null
*/
private $elasticEngineVersion;
public function __construct(array $options)
{
$this->options = $options;
}
public function query(array $query): array
{
return $this->getElasticsearchClient()->search($query);
}
public function search(array $query): array
{
try {
return $this->getElasticsearchClient()->search($query);
} catch (Missing404Exception $e) {
$message = __('Elasticsearch index not found. Run "bin/magento indexer:reindex catalogsearch_fulltext".');
throw new AmastyMissing404Exception($message, 0, $e);
}
}
public function bulkQuery(array $query): array
{
return $this->getElasticsearchClient()->bulk($query);
}
public function isEmptyIndex(string $index): bool
{
$stats = $this->getElasticsearchClient()->indices()->stats(['index' => $index, 'metric' => 'docs']);
return !isset($stats['indices'][$index]) || $stats['indices'][$index]['primaries']['docs']['count'] === 0;
}
public function createIndex(string $index, array $settings): void
{
$params = [
'index' => $index,
'body' => []
];
if (!empty($settings)) {
$params['body']['settings'] = $settings;
}
$this->getElasticsearchClient()->indices()->create($params);
}
public function deleteIndex(string $index): void
{
$this->getElasticsearchClient()->indices()->delete(['index' => $index]);
}
public function indexExists(string $index): bool
{
return $this->getElasticsearchClient()->indices()->exists(['index' => $index]);
}
public function existsAlias(string $alias, string $index = ''): bool
{
$params = ['name' => $alias];
if ($index) {
$params['index'] = $index;
}
try {
return $this->getElasticsearchClient()->indices()->existsAlias($params);
} catch (NoNodesAvailableException $e) {
$message = __('Elasticsearch server not found. Check "Stores > Configuration'
. ' > Amasty Elastic Search > Connection" and make sure that "Test Connection"'
.' is successfull with appropriate host name and port.');
throw new AmastyNoNodesAvailableException($message, 0, $e);
}
}
public function getAlias(string $alias): array
{
return $this->getElasticsearchClient()->indices()->getAlias(['name' => $alias]);
}
public function updateAlias(string $alias, string $newIndex): void
{
$params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
$this->updateAliases($params);
}
public function updateAliases(array $params): void
{
$this->getElasticsearchClient()->indices()->updateAliases($params);
}
public function putMapping(array $params): void
{
$this->getElasticsearchClient()->indices()->putMapping($params);
}
public function ping(): bool
{
return $this->getElasticsearchClient()->ping(
['client' => ['timeout' => $this->options['timeout']]]
);
}
public function isSystemPackageAvailable(): bool
{
return class_exists(ClientBuilder::class);
}
public function getNeededSystemPackageVersion(): string
{
return '7.17';
}
public function getSystemPackageName(): string
{
return 'elasticsearch/elasticsearch';
}
public function getCurrentEngineVersion(): string
{
if ($this->elasticEngineVersion === null) {
$elasticInfo = $this->getElasticsearchClient()->info();
$this->elasticEngineVersion = $elasticInfo['version']['number'] ?? null;
}
return $this->elasticEngineVersion;
}
/**
* @throws SystemPackageNotInstalled
*/
private function getElasticsearchClient(): ElasticsearchClient
{
$pid = getmypid();
if (!isset($this->client[$pid])) {
$this->validateClient();
$this->client[$pid] = ClientBuilder::fromConfig($this->options, true);
}
return $this->client[$pid];
}
/**
* @throws SystemPackageNotInstalled
*/
private function validateClient(): void
{
if (!$this->isSystemPackageAvailable()) {
throw new SystemPackageNotInstalled(__(
'Elasticsearch package is not installed. Please, run the following command
in the SSH: composer require %1 ^%2',
$this->getSystemPackageName(),
$this->getNeededSystemPackageVersion()
));
}
}
}
@@ -0,0 +1,195 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client\Elasticsearch8;
use Amasty\ElasticSearch\Exception\Missing404Exception as AmastyMissing404Exception;
use Amasty\ElasticSearch\Exception\NoNodesAvailableException as AmastyNoNodesAvailableException;
use Amasty\ElasticSearch\Exception\SystemPackageNotInstalled;
use Amasty\ElasticSearch\Model\Client\ClientInterface;
use Elastic\Elasticsearch\Client as ElasticsearchClient;
use Elastic\Elasticsearch\ClientBuilder;
use Elastic\Elasticsearch\Exception\ClientResponseException;
use Elastic\Transport\Exception\NoNodeAvailableException;
use Magento\Framework\Exception\LocalizedException;
class Client implements ClientInterface
{
/**
* @var array
*/
private $options;
/**
* @var ElasticsearchClient[] ['pid' => client, ...]
*/
private $client = [];
/**
* @var string|null
*/
private $elasticEngineVersion;
public function __construct(array $options)
{
$this->options = $options;
}
public function query(array $query): array
{
return $this->getElasticsearchClient()->search($query)->asArray();
}
public function search(array $query): array
{
try {
return $this->getElasticsearchClient()->search($query)->asArray();
} catch (ClientResponseException $e) {
$message = __('Elasticsearch index not found. Run "bin/magento indexer:reindex catalogsearch_fulltext".');
throw new AmastyMissing404Exception($message->render(), 0, $e);
}
}
/**
* TODO: check
*/
public function bulkQuery(array $query): array
{
return $this->getElasticsearchClient()->bulk($query)->asArray();
}
public function isEmptyIndex(string $index): bool
{
$stats = $this->getElasticsearchClient()->indices()->stats(['index' => $index, 'metric' => 'docs'])->asArray();
return !isset($stats['indices'][$index]) || $stats['indices'][$index]['primaries']['docs']['count'] === 0;
}
public function createIndex(string $index, array $settings): void
{
$params = [
'index' => $index,
'body' => []
];
if (!empty($settings)) {
$params['body']['settings'] = $settings;
}
$this->getElasticsearchClient()->indices()->create($params);
}
public function deleteIndex(string $index): void
{
$this->getElasticsearchClient()->indices()->delete(['index' => $index]);
}
public function indexExists(string $index): bool
{
return $this->getElasticsearchClient()->indices()->exists(['index' => $index])->asBool();
}
/**
* TODO: check
*/
public function existsAlias(string $alias, string $index = ''): bool
{
$params = ['name' => $alias];
if ($index) {
$params['index'] = $index;
}
try {
return $this->getElasticsearchClient()->indices()->existsAlias($params)->asBool();
} catch (NoNodeAvailableException $e) {
$message = __('Elasticsearch server not found. Check "Stores > Configuration'
. ' > Amasty Elastic Search > Connection" and make sure that "Test Connection"'
.' is successfull with appropriate host name and port.');
throw new AmastyNoNodesAvailableException($message->render(), 0, $e);
}
}
public function getAlias(string $alias): array
{
return $this->getElasticsearchClient()->indices()->getAlias(['name' => $alias])->asArray();
}
public function updateAlias(string $alias, string $newIndex): void
{
$params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
$this->updateAliases($params);
}
public function updateAliases(array $params): void
{
$this->getElasticsearchClient()->indices()->updateAliases($params);
}
public function putMapping(array $params): void
{
$this->getElasticsearchClient()->indices()->putMapping($params);
}
public function ping(): bool
{
return $this->getElasticsearchClient()->ping(
['client' => ['timeout' => $this->options['timeout']]]
)->asBool();
}
public function isSystemPackageAvailable(): bool
{
return class_exists(ClientBuilder::class);
}
public function getNeededSystemPackageVersion(): string
{
return '8.6';
}
public function getSystemPackageName(): string
{
return 'elasticsearch/elasticsearch';
}
public function getCurrentEngineVersion(): string
{
if ($this->elasticEngineVersion === null) {
$elasticInfo = $this->getElasticsearchClient()->info()->asArray();
$this->elasticEngineVersion = $elasticInfo['version']['number'] ?? null;
}
return $this->elasticEngineVersion;
}
private function getElasticsearchClient(): ElasticsearchClient
{
$pid = getmypid();
if (!isset($this->client[$pid])) {
$this->validateClient();
$this->client[$pid] = ClientBuilder::fromConfig($this->options, true);
}
return $this->client[$pid];
}
/**
* @throws SystemPackageNotInstalled
*/
private function validateClient(): void
{
if (!$this->isSystemPackageAvailable()) {
throw new SystemPackageNotInstalled(__(
'Elasticsearch package is not installed. Please, run the following command
in the SSH: composer require %1 ^%2',
$this->getSystemPackageName(),
$this->getNeededSystemPackageVersion()
));
}
}
}
@@ -0,0 +1,193 @@
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) Amasty (https://www.amasty.com)
* @package Elastic Search Base for Magento 2
*/
namespace Amasty\ElasticSearch\Model\Client\Opensearch;
use Amasty\ElasticSearch\Exception\Missing404Exception as AmastyMissing404Exception;
use Amasty\ElasticSearch\Exception\NoNodesAvailableException as AmastyNoNodesAvailableException;
use Amasty\ElasticSearch\Exception\SystemPackageNotInstalled;
use Amasty\ElasticSearch\Model\Client\ClientInterface;
use OpenSearch\Client as OpenSearchClient;
use OpenSearch\ClientBuilder;
use OpenSearch\Common\Exceptions\Missing404Exception;
use OpenSearch\Common\Exceptions\NoNodesAvailableException;
class Client implements ClientInterface
{
/**
* @var array
*/
private $options;
/**
* @var OpenSearchClient[] ['pid' => client, ...]
*/
private $client = [];
/**
* @var string|null
*/
private $elasticEngineVersion;
public function __construct(array $options)
{
$this->options = $options;
}
public function query(array $query): array
{
unset($query['type']);
return $this->getOpenSearchClient()->search($query);
}
public function search(array $query): array
{
try {
unset($query['type']);
return $this->getOpenSearchClient()->search($query);
} catch (Missing404Exception $e) {
$message = __('Opensearch index not found. Run "bin/magento indexer:reindex catalogsearch_fulltext".');
throw new AmastyMissing404Exception($message, 0, $e);
}
}
public function bulkQuery(array $query): array
{
return $this->getOpenSearchClient()->bulk($query);
}
public function isEmptyIndex(string $index): bool
{
$stats = $this->getOpenSearchClient()->indices()->stats(['index' => $index, 'metric' => 'docs']);
return !isset($stats['indices'][$index]) || $stats['indices'][$index]['primaries']['docs']['count'] === 0;
}
public function createIndex(string $index, array $settings): void
{
$params = [
'index' => $index,
'body' => []
];
if (!empty($settings)) {
$params['body']['settings'] = $settings;
}
$this->getOpenSearchClient()->indices()->create($params);
}
public function deleteIndex(string $index): void
{
$this->getOpenSearchClient()->indices()->delete(['index' => $index]);
}
public function indexExists(string $index): bool
{
return $this->getOpenSearchClient()->indices()->exists(['index' => $index]);
}
public function existsAlias(string $alias, string $index = ''): bool
{
$params = ['name' => $alias];
if ($index) {
$params['index'] = $index;
}
try {
return $this->getOpenSearchClient()->indices()->existsAlias($params);
} catch (NoNodesAvailableException $e) {
$message = __('Opensearch server not found. Check "Stores > Configuration'
. ' > Amasty Elastic Search > Connection" and make sure that "Test Connection"'
.' is successfull with appropriate host name and port.');
throw new AmastyNoNodesAvailableException($message, 0, $e);
}
}
public function getAlias(string $alias): array
{
return $this->getOpenSearchClient()->indices()->getAlias(['name' => $alias]);
}
public function updateAlias(string $alias, string $newIndex): void
{
$params['body']['actions'][] = ['add' => ['alias' => $alias, 'index' => $newIndex]];
$this->updateAliases($params);
}
public function updateAliases(array $params): void
{
$this->getOpenSearchClient()->indices()->updateAliases($params);
}
public function putMapping(array $params): void
{
$this->getOpenSearchClient()->indices()->putMapping($params);
}
public function ping(): bool
{
return $this->getOpenSearchClient()->ping(
['client' => ['timeout' => $this->options['timeout']]]
);
}
public function isSystemPackageAvailable(): bool
{
return class_exists(ClientBuilder::class);
}
public function getNeededSystemPackageVersion(): string
{
return '2.0.0';
}
public function getSystemPackageName(): string
{
return 'opensearch-project/opensearch-php';
}
public function getCurrentEngineVersion(): string
{
if ($this->elasticEngineVersion === null) {
$elasticInfo = $this->getOpenSearchClient()->info();
$this->elasticEngineVersion = $elasticInfo['version']['number'] ?? null;
}
return $this->elasticEngineVersion;
}
/**
* @throws SystemPackageNotInstalled
*/
private function getOpenSearchClient(): OpenSearchClient
{
$pid = getmypid();
if (!isset($this->client[$pid])) {
$this->validateClient();
$this->client[$pid] = ClientBuilder::fromConfig($this->options, true);
}
return $this->client[$pid];
}
/**
* @throws SystemPackageNotInstalled
*/
private function validateClient(): void
{
if (!$this->isSystemPackageAvailable()) {
throw new SystemPackageNotInstalled(__(
'Elasticsearch package is not installed. Please, run the following command
in the SSH: composer require %1 ^%2',
$this->getSystemPackageName(),
$this->getNeededSystemPackageVersion()
));
}
}
}