Update
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitrix\Rest\Internal\Integration\Market;
|
||||
|
||||
use Bitrix\Main\Error;
|
||||
use Bitrix\Main\Loader;
|
||||
use Bitrix\Main\ModuleManager;
|
||||
use Bitrix\Main\Result;
|
||||
use Bitrix\Market\Subscription\Trial;
|
||||
|
||||
class Subscription
|
||||
{
|
||||
private bool $isModuleIncluded;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->isModuleIncluded = Loader::includeModule('market');
|
||||
}
|
||||
|
||||
public function activateDemo(): Result
|
||||
{
|
||||
$result = new Result();
|
||||
|
||||
if (
|
||||
!$this->isModuleIncluded
|
||||
|| !$this->isDemoAvailable()
|
||||
)
|
||||
{
|
||||
return $result->addError(
|
||||
new Error('Subscription is not available')
|
||||
);
|
||||
}
|
||||
|
||||
$res = Trial::activate();
|
||||
|
||||
if (isset($res['error']))
|
||||
{
|
||||
$error = new Error(
|
||||
is_string($res['error']) ? $res['error'] : 'Unknown error',
|
||||
isset($res['error_code']) && (is_string($res['error_code']) || is_int($res['error_code']))
|
||||
? $res['error_code']
|
||||
: null,
|
||||
);
|
||||
|
||||
return $result->addError($error);
|
||||
}
|
||||
|
||||
return $result->setData($res);
|
||||
}
|
||||
|
||||
private function isDemoAvailable(): bool
|
||||
{
|
||||
return Trial::isAvailable()
|
||||
&& (
|
||||
!ModuleManager::isModuleInstalled('extranet')
|
||||
|| (Loader::includeModule('extranet') && \CExtranet::IsIntranetUser())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Bitrix\Rest\Internal\Request\Listener;
|
||||
|
||||
use Bitrix\Main\HttpRequest;
|
||||
|
||||
class ActivateDemoMarketListener implements Listener
|
||||
{
|
||||
protected const PARAM_NAME = 'activate_demo_market';
|
||||
|
||||
public function handle(HttpRequest $request): void
|
||||
{
|
||||
if ($request->getQuery(static::PARAM_NAME) === 'Y')
|
||||
{
|
||||
$subscription = new \Bitrix\Rest\Internal\Integration\Market\Subscription();
|
||||
$subscription->activateDemo();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Bitrix\Rest\Internal\Request\Listener;
|
||||
|
||||
use Bitrix\Main\HttpRequest;
|
||||
|
||||
interface Listener
|
||||
{
|
||||
public function handle(HttpRequest $request): void;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Bitrix\Rest\Internal\Request\Listener;
|
||||
|
||||
use Bitrix\Main\Application;
|
||||
use Bitrix\Main\HttpRequest;
|
||||
|
||||
class ListenerCollection
|
||||
{
|
||||
/** @var Listener[] */
|
||||
private array $listeners = [];
|
||||
|
||||
public function addListener(Listener $listener): void
|
||||
{
|
||||
$this->listeners[] = $listener;
|
||||
}
|
||||
|
||||
public function handleRequest(HttpRequest $request = null): void
|
||||
{
|
||||
$request ??= Application::getInstance()->getContext()->getRequest();
|
||||
|
||||
foreach ($this->listeners as $listener)
|
||||
{
|
||||
$listener->handle($request);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Bitrix\Rest\Internal\Service;
|
||||
|
||||
use Bitrix\Main\Config\Option;
|
||||
|
||||
class MemoryLimitChecker
|
||||
{
|
||||
public function __construct(
|
||||
private readonly int $memoryLimit,
|
||||
private readonly float $memoryRate,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function createByDefault(): self
|
||||
{
|
||||
return new self(
|
||||
self::memoryLimitFromPhpIniInMb(),
|
||||
(float)Option::get('rest', 'memory_limit_rate', 0.9),
|
||||
);
|
||||
}
|
||||
|
||||
public function executeOnMemoryExcess(callable $callback): void
|
||||
{
|
||||
if ($this->memoryLimit <= 0 || $this->memoryRate <= 0 || $this->memoryRate > 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$memoryUsageMB = memory_get_usage() / (1024 * 1024);
|
||||
$maxAllowedSizeMB = $this->memoryLimit * $this->memoryRate;
|
||||
|
||||
if ($memoryUsageMB > $maxAllowedSizeMB)
|
||||
{
|
||||
$callback($memoryUsageMB, $maxAllowedSizeMB);
|
||||
}
|
||||
}
|
||||
|
||||
private static function memoryLimitFromPhpIniInMb(): int
|
||||
{
|
||||
$memoryLimit = ini_get('memory_limit');
|
||||
if ($memoryLimit === false)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
$unit = strtoupper(substr($memoryLimit, -1));
|
||||
$value = (int)$memoryLimit;
|
||||
|
||||
return match ($unit)
|
||||
{
|
||||
'G' => $value * 1024,
|
||||
'M' => $value,
|
||||
'K' => (int)ceil($value / 1024),
|
||||
default => (int)ceil((int)$memoryLimit / (1024 * 1024)),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user