Initial Version
This commit is contained in:
119
core/lib/Module/Store/ModuleEntry.php
Normal file
119
core/lib/Module/Store/ModuleEntry.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
namespace KTXC\Module\Store;
|
||||
|
||||
use KTXF\Json\JsonDeserializable;
|
||||
|
||||
/**
|
||||
* Module entity representing an installed module
|
||||
*/
|
||||
class ModuleEntry implements \JsonSerializable, JsonDeserializable
|
||||
{
|
||||
private ?string $id = null;
|
||||
private ?string $namespace = null;
|
||||
private ?string $handle = null;
|
||||
private bool $installed = false;
|
||||
private bool $enabled = false;
|
||||
private string $version = '0.0.1';
|
||||
|
||||
/**
|
||||
* Deserialize from associative array.
|
||||
*/
|
||||
public function jsonDeserialize(array|string $data): static
|
||||
{
|
||||
if (is_string($data)) {
|
||||
$data = json_decode($data, true);
|
||||
}
|
||||
// Map only if key exists to avoid notices and allow partial input
|
||||
if (array_key_exists('_id', $data)) $this->id = $data['_id'] !== null ? (string)$data['_id'] : null;
|
||||
elseif (array_key_exists('id', $data)) $this->id = $data['id'] !== null ? (string)$data['id'] : null;
|
||||
if (array_key_exists('namespace', $data)) $this->namespace = $data['namespace'] !== null ? (string)$data['namespace'] : null;
|
||||
if (array_key_exists('handle', $data)) $this->handle = $data['handle'] !== null ? (string)$data['handle'] : null;
|
||||
if (array_key_exists('installed', $data)) $this->installed = (bool)$data['installed'];
|
||||
if (array_key_exists('enabled', $data)) $this->enabled = (bool)$data['enabled'];
|
||||
if (array_key_exists('version', $data)) $this->version = (string)$data['version'];
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize to JSON-friendly structure.
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'namespace' => $this->namespace,
|
||||
'handle' => $this->handle,
|
||||
'installed' => $this->installed,
|
||||
'enabled' => $this->enabled,
|
||||
'version' => $this->version,
|
||||
];
|
||||
}
|
||||
|
||||
public function getId(): ?string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setId(string $value): self
|
||||
{
|
||||
$this->id = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getNamespace(): ?string
|
||||
{
|
||||
return $this->namespace;
|
||||
}
|
||||
|
||||
public function setNamespace(string $value): self
|
||||
{
|
||||
$this->namespace = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getHandle(): ?string
|
||||
{
|
||||
return $this->handle;
|
||||
}
|
||||
|
||||
public function setHandle(string $value): self
|
||||
{
|
||||
$this->handle = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getInstalled(): bool
|
||||
{
|
||||
return $this->installed;
|
||||
}
|
||||
|
||||
public function setInstalled(bool $value): self
|
||||
{
|
||||
$this->installed = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEnabled(): bool
|
||||
{
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
public function setEnabled(bool $value): self
|
||||
{
|
||||
$this->enabled = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getVersion(): string
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
public function setVersion(string $value): self
|
||||
{
|
||||
$this->version = $value;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
66
core/lib/Module/Store/ModuleStore.php
Normal file
66
core/lib/Module/Store/ModuleStore.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace KTXC\Module\Store;
|
||||
|
||||
use KTXC\Db\DataStore;
|
||||
|
||||
class ModuleStore
|
||||
{
|
||||
|
||||
protected const COLLECTION_NAME = 'modules';
|
||||
|
||||
public function __construct(
|
||||
protected readonly DataStore $dataStore
|
||||
) { }
|
||||
|
||||
public function list(): array
|
||||
{
|
||||
$cursor = $this->dataStore->selectCollection(self::COLLECTION_NAME)->find(['enabled' => true, 'installed' => true]);
|
||||
$modules = [];
|
||||
foreach ($cursor as $entry) {
|
||||
$entity = new ModuleEntry();
|
||||
$entity->jsonDeserialize((array)$entry);
|
||||
$modules[$entity->getId()] = $entity;
|
||||
}
|
||||
return $modules;
|
||||
}
|
||||
|
||||
public function fetch(string $handle): ?ModuleEntry
|
||||
{
|
||||
$entry = $this->dataStore->selectCollection(self::COLLECTION_NAME)->findOne(['handle' => $handle]);
|
||||
if (!$entry) { return null; }
|
||||
return (new ModuleEntry())->jsonDeserialize((array)$entry);
|
||||
}
|
||||
|
||||
public function deposit(ModuleEntry $entry): ?ModuleEntry
|
||||
{
|
||||
if ($entry->getId()) {
|
||||
return $this->update($entry);
|
||||
} else {
|
||||
return $this->create($entry);
|
||||
}
|
||||
}
|
||||
|
||||
private function create(ModuleEntry $entry): ?ModuleEntry
|
||||
{
|
||||
$result = $this->dataStore->selectCollection(self::COLLECTION_NAME)->insertOne($entry->jsonSerialize());
|
||||
$entry->setId((string)$result->getInsertedId());
|
||||
return $entry;
|
||||
}
|
||||
|
||||
private function update(ModuleEntry $entry): ?ModuleEntry
|
||||
{
|
||||
$id = $entry->getId();
|
||||
if (!$id) { return null; }
|
||||
$this->dataStore->selectCollection(self::COLLECTION_NAME)->updateOne(['_id' => $id], ['$set' => $entry->jsonSerialize()]);
|
||||
return $entry;
|
||||
}
|
||||
|
||||
public function destroy(ModuleEntry $entry): void
|
||||
{
|
||||
$id = $entry->getId();
|
||||
if (!$id) { return; }
|
||||
$this->dataStore->selectCollection(self::COLLECTION_NAME)->deleteOne([ '_id' => $id]);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user