2024-05-06 16:00:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Modules\Plugins\Manifest;
|
|
|
|
|
|
|
|
use CodeIgniter\Validation\Validation;
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
abstract class ManifestObject
|
|
|
|
{
|
|
|
|
protected const VALIDATION_RULES = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string,string|array{string}>
|
|
|
|
*/
|
|
|
|
protected const CASTS = [];
|
|
|
|
|
|
|
|
/**
|
2024-05-16 15:53:42 +00:00
|
|
|
* @var array<string,array<string,string>>
|
2024-05-06 16:00:47 +00:00
|
|
|
*/
|
2024-05-16 15:53:42 +00:00
|
|
|
protected static array $errors = [];
|
2024-05-06 16:00:47 +00:00
|
|
|
|
|
|
|
public function __construct(
|
2024-05-16 15:53:42 +00:00
|
|
|
protected readonly string $pluginKey,
|
2024-05-06 16:00:47 +00:00
|
|
|
) {
|
2024-05-16 15:53:42 +00:00
|
|
|
self::$errors[$pluginKey] = [];
|
2024-05-06 16:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __get(string $name): mixed
|
|
|
|
{
|
2024-05-09 17:55:41 +00:00
|
|
|
if (property_exists($this, $name)) {
|
2024-05-06 16:00:47 +00:00
|
|
|
return $this->{$name};
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Exception('Undefined object property ' . static::class . '::' . $name);
|
|
|
|
}
|
|
|
|
|
2024-05-12 18:38:33 +00:00
|
|
|
public function __isset(string $property): bool
|
|
|
|
{
|
|
|
|
return property_exists($this, $property);
|
|
|
|
}
|
|
|
|
|
2024-05-28 15:57:04 +00:00
|
|
|
public function loadFromFile(string $manifestPath): void
|
|
|
|
{
|
|
|
|
$manifestContents = @file_get_contents($manifestPath);
|
|
|
|
|
|
|
|
if (! $manifestContents) {
|
|
|
|
$manifestContents = '{}';
|
|
|
|
$this->addError('manifest', lang('Plugins.errors.manifestMissing', [
|
|
|
|
'manifestPath' => $manifestPath,
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var array<mixed>|null $manifestData */
|
|
|
|
$manifestData = json_decode($manifestContents, true);
|
|
|
|
|
|
|
|
if ($manifestData === null) {
|
|
|
|
$manifestData = [];
|
|
|
|
$this->addError('manifest', lang('Plugins.errors.manifestJsonInvalid', [
|
|
|
|
'manifestPath' => $manifestPath,
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->loadData($manifestData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<mixed> $data
|
|
|
|
*/
|
|
|
|
public function loadData(array $data): void
|
2024-05-06 16:00:47 +00:00
|
|
|
{
|
|
|
|
/** @var Validation $validation */
|
|
|
|
$validation = service('validation');
|
|
|
|
|
|
|
|
$validation->setRules($this::VALIDATION_RULES);
|
|
|
|
|
2024-05-28 15:57:04 +00:00
|
|
|
if (! $validation->run($data)) {
|
2024-05-16 15:53:42 +00:00
|
|
|
foreach ($validation->getErrors() as $key => $message) {
|
|
|
|
$this->addError($key, $message);
|
|
|
|
}
|
|
|
|
|
|
|
|
$validation->reset();
|
2024-05-06 16:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($validation->getValidated() as $key => $value) {
|
|
|
|
if (array_key_exists($key, $this::CASTS)) {
|
|
|
|
$cast = $this::CASTS[$key];
|
|
|
|
|
|
|
|
if (is_array($cast)) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
foreach ($value as $valueKey => $valueElement) {
|
2024-06-03 14:32:42 +00:00
|
|
|
if (is_subclass_of($cast[0], self::class)) {
|
|
|
|
$value[$valueKey] = new $cast[0]($this->pluginKey);
|
|
|
|
$value[$valueKey]->loadData($valueElement);
|
|
|
|
} else {
|
|
|
|
$value[$valueKey] = new $cast[0]($valueElement);
|
|
|
|
}
|
2024-05-06 16:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-03 14:32:42 +00:00
|
|
|
} elseif (is_subclass_of($cast, self::class)) {
|
|
|
|
$valueElement = $value;
|
|
|
|
$value = new $cast($this->pluginKey);
|
|
|
|
$value->loadData($valueElement ?? []);
|
2024-05-06 16:00:47 +00:00
|
|
|
} else {
|
2024-06-03 14:32:42 +00:00
|
|
|
$value = new $cast($value ?? []);
|
2024-05-06 16:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->{$key} = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string,string>
|
|
|
|
*/
|
2024-05-16 15:53:42 +00:00
|
|
|
public static function getPluginErrors(string $pluginKey): array
|
|
|
|
{
|
|
|
|
return self::$errors[$pluginKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addError(string $errorKey, string $errorMessage): void
|
2024-05-06 16:00:47 +00:00
|
|
|
{
|
2024-05-16 15:53:42 +00:00
|
|
|
self::$errors[$this->pluginKey][$errorKey] = $errorMessage;
|
2024-05-06 16:00:47 +00:00
|
|
|
}
|
|
|
|
}
|