2024-07-16 05:26:53 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Appwrite\Functions\Validator;
|
|
|
|
|
|
|
|
|
|
use Utopia\Config\Config;
|
|
|
|
|
use Utopia\System\System;
|
|
|
|
|
use Utopia\Validator;
|
|
|
|
|
|
2024-07-31 11:27:32 +00:00
|
|
|
class RuntimeSpecification extends Validator
|
2024-07-16 05:26:53 +00:00
|
|
|
{
|
|
|
|
|
private array $plan;
|
|
|
|
|
|
|
|
|
|
public function __construct(array $plan)
|
|
|
|
|
{
|
|
|
|
|
$this->plan = $plan;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-07-31 11:27:32 +00:00
|
|
|
* Get Allowed Specifications.
|
2024-07-16 05:26:53 +00:00
|
|
|
*
|
2024-07-31 11:27:32 +00:00
|
|
|
* Get allowed specifications taking into account the limits set by the environment variables and the plan.
|
2024-07-16 05:26:53 +00:00
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2024-07-31 11:27:32 +00:00
|
|
|
public function getAllowedSpecifications(): array
|
2024-07-16 05:26:53 +00:00
|
|
|
{
|
2024-07-31 11:27:32 +00:00
|
|
|
$specifications = Config::getParam('runtime-specifications', []);
|
2024-07-16 05:26:53 +00:00
|
|
|
|
2024-08-02 07:45:22 +00:00
|
|
|
$allowedSpecifications = [];
|
2024-07-16 05:26:53 +00:00
|
|
|
|
2024-07-31 11:27:32 +00:00
|
|
|
foreach ($specifications as $size => $values) {
|
2024-07-16 05:26:53 +00:00
|
|
|
if ($values['cpus'] <= System::getEnv('_APP_FUNCTIONS_CPUS', 1) && $values['memory'] <= System::getEnv('_APP_FUNCTIONS_MEMORY', 512)) {
|
2024-07-31 11:27:32 +00:00
|
|
|
if (!empty($this->plan) && key_exists('runtimeSpecifications', $this->plan)) {
|
|
|
|
|
if (!\in_array($size, $this->plan['runtimeSpecifications'])) {
|
2024-07-16 05:26:53 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 11:27:32 +00:00
|
|
|
$allowedSpecifications[] = $size;
|
2024-07-16 05:26:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 11:27:32 +00:00
|
|
|
return $allowedSpecifications;
|
2024-07-16 05:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Description.
|
|
|
|
|
*
|
|
|
|
|
* Returns validator description.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getDescription(): string
|
|
|
|
|
{
|
2024-08-02 03:05:07 +00:00
|
|
|
return 'String must be a valid specification value of ' . implode(', ', $this->getAllowedSpecifications());
|
2024-07-16 05:26:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is valid.
|
|
|
|
|
*
|
|
|
|
|
* Returns true if valid or false if not.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed $value
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isValid($value): bool
|
|
|
|
|
{
|
|
|
|
|
if (empty($value)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!\is_string($value)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 11:27:32 +00:00
|
|
|
if (!\in_array($value, $this->getAllowedSpecifications())) {
|
2024-07-16 05:26:53 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is array.
|
|
|
|
|
*
|
|
|
|
|
* Function will return true if object is array.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isArray(): bool
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get Type.
|
|
|
|
|
*
|
|
|
|
|
* Returns validator type.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getType(): string
|
|
|
|
|
{
|
|
|
|
|
return self::TYPE_STRING;
|
|
|
|
|
}
|
|
|
|
|
}
|