refactor: standardize design
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "ktrix/file-provider-local",
|
||||
"name": "ktrix/provider-local-documents",
|
||||
"type": "project",
|
||||
"authors": [
|
||||
{
|
||||
@@ -12,7 +12,7 @@
|
||||
"platform": {
|
||||
"php": "8.2"
|
||||
},
|
||||
"autoloader-suffix": "FileProviderLocal",
|
||||
"autoloader-suffix": "ProviderLocalDocuments",
|
||||
"vendor-dir": "lib/vendor"
|
||||
},
|
||||
"require": {
|
||||
@@ -20,7 +20,7 @@
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"KTXM\\FileProviderLocal\\": "lib/"
|
||||
"KTXM\\ProviderLocalDocuments\\": "lib/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,53 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace KTXM\FileProviderLocal;
|
||||
namespace KTXM\ProviderLocalDocuments;
|
||||
|
||||
use KTXC\Resource\ProviderManager;
|
||||
use KTXF\Module\ModuleBrowserInterface;
|
||||
use KTXF\Module\ModuleInstanceAbstract;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
use KTXM\FileProviderLocal\Providers\Provider;
|
||||
use KTXM\ProviderLocalDocuments\Providers\Provider;
|
||||
|
||||
/**
|
||||
* File Provider Local Module
|
||||
*/
|
||||
class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
|
||||
{
|
||||
public const MODULE_HANDLE = 'provider_local_documents';
|
||||
public const MODULE_NAMESPACE = 'ProviderLocalDocuments';
|
||||
public const MODULE_LABEL = 'Local Documents Storage Provider';
|
||||
public const MODULE_DESCRIPTION = 'Provides a local disk documents storage';
|
||||
public const MODULE_AUTHOR = 'Ktrix';
|
||||
public const MODULE_VERSION = '0.0.1';
|
||||
|
||||
public function __construct(
|
||||
private readonly ProviderManager $providerManager,
|
||||
) {}
|
||||
|
||||
public function handle(): string
|
||||
{
|
||||
return 'file_provider_local';
|
||||
return self::MODULE_HANDLE;
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'File Provider Local';
|
||||
return self::MODULE_LABEL;
|
||||
}
|
||||
|
||||
public function author(): string
|
||||
{
|
||||
return 'Ktrix';
|
||||
return self::MODULE_AUTHOR;
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'File provider module for Ktrix - provides local file storage';
|
||||
return self::MODULE_DESCRIPTION;
|
||||
}
|
||||
|
||||
public function version(): string
|
||||
{
|
||||
return '0.0.1';
|
||||
return self::MODULE_VERSION;
|
||||
}
|
||||
|
||||
public function permissions(): array
|
||||
{
|
||||
return [
|
||||
'file_provider_local' => [
|
||||
'label' => 'Access File Provider Local',
|
||||
'description' => 'View and access the local file provider module',
|
||||
'provider_local_documents' => [
|
||||
'label' => 'Access Local Documents Storage Provider',
|
||||
'description' => 'View and access the local documents storage provider module',
|
||||
'group' => 'File Providers'
|
||||
],
|
||||
];
|
||||
@@ -55,17 +59,17 @@ class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->providerManager->register(ProviderInterface::TYPE_FILES, 'default', Provider::class);
|
||||
$this->providerManager->register(ProviderInterface::TYPE_DOCUMENT, 'default', Provider::class);
|
||||
}
|
||||
|
||||
public function registerBI(): array {
|
||||
return [
|
||||
'handle' => $this->handle(),
|
||||
'namespace' => 'FileProviderLocal',
|
||||
'version' => $this->version(),
|
||||
'label' => $this->label(),
|
||||
'author' => $this->author(),
|
||||
'description' => $this->description()
|
||||
'handle' => self::MODULE_HANDLE,
|
||||
'namespace' => self::MODULE_NAMESPACE,
|
||||
'version' => self::MODULE_VERSION,
|
||||
'label' => self::MODULE_LABEL,
|
||||
'author' => self::MODULE_AUTHOR,
|
||||
'description' => self::MODULE_DESCRIPTION
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
40
lib/Providers/Personal/CollectionProperties.php
Normal file
40
lib/Providers/Personal/CollectionProperties.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderLocalDocuments\Providers\Personal;
|
||||
|
||||
use KTXF\Resource\Documents\Collection\CollectionPropertiesMutableAbstract;
|
||||
|
||||
class CollectionProperties extends CollectionPropertiesMutableAbstract {
|
||||
|
||||
/**
|
||||
* Converts store document values into collection properties.
|
||||
*/
|
||||
public function fromStore(array|object $data): static {
|
||||
|
||||
if (is_object($data)) {
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
if (isset($data['label'])) {
|
||||
$this->data[self::JSON_PROPERTY_LABEL] = (string) $data['label'];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts collection properties into store document values.
|
||||
*/
|
||||
public function toStore(): array {
|
||||
return array_filter([
|
||||
'label' => $this->getLabel(),
|
||||
], static fn($value) => $value !== null);
|
||||
}
|
||||
}
|
||||
63
lib/Providers/Personal/CollectionResource.php
Normal file
63
lib/Providers/Personal/CollectionResource.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderLocalDocuments\Providers\Personal;
|
||||
|
||||
use KTXF\Resource\Documents\Collection\CollectionMutableAbstract;
|
||||
|
||||
class CollectionResource extends CollectionMutableAbstract {
|
||||
|
||||
public function __construct(
|
||||
string $provider = 'default',
|
||||
string|int $service = 'personal',
|
||||
) {
|
||||
parent::__construct($provider, $service);
|
||||
}
|
||||
|
||||
public function fromStore(array|object $data): self
|
||||
{
|
||||
if (is_object($data)) {
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
$this->data[static::JSON_PROPERTY_COLLECTION] = $data['cid'] ?? null;
|
||||
$this->data[static::JSON_PROPERTY_IDENTIFIER] = $data['nid'];
|
||||
$this->data[static::JSON_PROPERTY_CREATED] = $data['created'] ?? null;
|
||||
$this->data[static::JSON_PROPERTY_MODIFIED] = $data['modified'] ?? null;
|
||||
$this->data[static::JSON_PROPERTY_SIGNATURE] = $data['signature'] ?? null;
|
||||
$this->getProperties()->fromStore($data['properties'] ?? []);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toStore(): array
|
||||
{
|
||||
$properties = $this->getProperties();
|
||||
|
||||
$data = array_filter([
|
||||
'cid' => $this->data[static::JSON_PROPERTY_COLLECTION] ?? null,
|
||||
'nid' => $this->data[static::JSON_PROPERTY_IDENTIFIER] ?? null,
|
||||
'created' => $this->data[static::JSON_PROPERTY_CREATED] ?? null,
|
||||
'modified' => $this->data[static::JSON_PROPERTY_MODIFIED] ?? null,
|
||||
'signature' => $this->data[static::JSON_PROPERTY_SIGNATURE] ?? null,
|
||||
'properties' => $properties ? $properties->toStore() : null,
|
||||
], static fn($value) => $value !== null);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getProperties(): CollectionProperties {
|
||||
if (!isset($this->properties)) {
|
||||
$this->properties = new CollectionProperties([]);
|
||||
}
|
||||
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
}
|
||||
59
lib/Providers/Personal/EntityProperties.php
Normal file
59
lib/Providers/Personal/EntityProperties.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderLocalDocuments\Providers\Personal;
|
||||
|
||||
use KTXF\Resource\Documents\Entity\EntityPropertiesMutableAbstract;
|
||||
|
||||
class EntityProperties extends EntityPropertiesMutableAbstract {
|
||||
|
||||
/**
|
||||
* Converts store document values into entity properties.
|
||||
*/
|
||||
public function fromStore(array|object $data): static {
|
||||
|
||||
if (is_object($data)) {
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
if (isset($data['size'])) {
|
||||
$this->data[self::JSON_PROPERTY_SIZE] = (int) $data['size'];
|
||||
}
|
||||
|
||||
if (isset($data['label'])) {
|
||||
$this->data[self::JSON_PROPERTY_LABEL] = (string) $data['label'];
|
||||
}
|
||||
|
||||
if (isset($data['mime'])) {
|
||||
$this->data[self::JSON_PROPERTY_MIME] = (string) $data['mime'];
|
||||
}
|
||||
|
||||
if (isset($data['format'])) {
|
||||
$this->data[self::JSON_PROPERTY_FORMAT] = (string) $data['format'];
|
||||
}
|
||||
|
||||
if (isset($data['encoding'])) {
|
||||
$this->data[self::JSON_PROPERTY_ENCODING] = (string) $data['encoding'];
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts entity properties into store document values.
|
||||
*/
|
||||
public function toStore(): array {
|
||||
return array_filter([
|
||||
'label' => $this->data[self::JSON_PROPERTY_LABEL],
|
||||
'mime' => $this->data[self::JSON_PROPERTY_MIME] ?? 'application/octet-stream',
|
||||
'format' => $this->data[self::JSON_PROPERTY_FORMAT] ?? null,
|
||||
'encoding' => $this->data[self::JSON_PROPERTY_ENCODING] ?? null,
|
||||
], static fn($value) => $value !== null);
|
||||
}
|
||||
}
|
||||
63
lib/Providers/Personal/EntityResource.php
Normal file
63
lib/Providers/Personal/EntityResource.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderLocalDocuments\Providers\Personal;
|
||||
|
||||
use KTXF\Resource\Documents\Entity\EntityMutableAbstract;
|
||||
|
||||
class EntityResource extends EntityMutableAbstract {
|
||||
|
||||
public function __construct(
|
||||
string $provider = 'default',
|
||||
string|int $service = 'personal',
|
||||
) {
|
||||
parent::__construct($provider, $service);
|
||||
}
|
||||
|
||||
public function fromStore(array|object $data): self
|
||||
{
|
||||
if (is_object($data)) {
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
$this->data[static::JSON_PROPERTY_COLLECTION] = $data['cid'] ?? null;
|
||||
$this->data[static::JSON_PROPERTY_IDENTIFIER] = $data['nid'];
|
||||
$this->data[static::JSON_PROPERTY_CREATED] = $data['created'] ?? null;
|
||||
$this->data[static::JSON_PROPERTY_MODIFIED] = $data['modified'] ?? null;
|
||||
$this->data[static::JSON_PROPERTY_SIGNATURE] = $data['signature'] ?? null;
|
||||
$this->getProperties()->fromStore($data['properties'] ?? []);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toStore(): array
|
||||
{
|
||||
$properties = $this->getProperties();
|
||||
|
||||
$data = array_filter([
|
||||
'cid' => $this->data[static::JSON_PROPERTY_COLLECTION] ?? null,
|
||||
'nid' => $this->data[static::JSON_PROPERTY_IDENTIFIER] ?? null,
|
||||
'created' => $this->data[static::JSON_PROPERTY_CREATED] ?? null,
|
||||
'modified' => $this->data[static::JSON_PROPERTY_MODIFIED] ?? null,
|
||||
'signature' => $this->data[static::JSON_PROPERTY_SIGNATURE] ?? null,
|
||||
'properties' => $properties ? $properties->toStore() : null,
|
||||
], static fn($value) => $value !== null);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getProperties(): EntityProperties {
|
||||
if (!isset($this->properties)) {
|
||||
$this->properties = new EntityProperties([]);
|
||||
}
|
||||
|
||||
return $this->properties;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\FileProviderLocal\Providers\Personal;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use KTXF\Files\Node\INodeCollectionBase;
|
||||
use KTXF\Files\Node\INodeCollectionMutable;
|
||||
use KTXF\Files\Node\NodeType;
|
||||
|
||||
/**
|
||||
* NodeCollection implementation - represents a folder/collection in the file system
|
||||
*/
|
||||
class NodeCollection implements INodeCollectionBase, INodeCollectionMutable {
|
||||
|
||||
// node system properties
|
||||
private string|null $tenantId = null;
|
||||
private string|null $userId = null;
|
||||
private string|int|null $nodeIn = null;
|
||||
private string|int|null $nodeId = null;
|
||||
private string|null $nodeCreatedBy = null;
|
||||
private DateTimeImmutable|null $nodeCreatedOn = null;
|
||||
private string|null $nodeModifiedBy = null;
|
||||
private DateTimeImmutable|null $nodeModifiedOn = null;
|
||||
private string|null $nodeOwner = null;
|
||||
private string|null $nodeSignature = null;
|
||||
// node specific properties
|
||||
private string|null $nodeLabel = null;
|
||||
|
||||
public function jsonSerialize(): mixed {
|
||||
return [
|
||||
// node meta properties
|
||||
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
||||
self::JSON_PROPERTY_IN => $this->nodeIn,
|
||||
self::JSON_PROPERTY_ID => $this->nodeId,
|
||||
self::JSON_PROPERTY_CREATED_BY => $this->nodeCreatedBy,
|
||||
self::JSON_PROPERTY_CREATED_ON => $this->nodeCreatedOn?->format('c'),
|
||||
self::JSON_PROPERTY_MODIFIED_BY => $this->nodeModifiedBy,
|
||||
self::JSON_PROPERTY_MODIFIED_ON => $this->nodeModifiedOn?->format('c'),
|
||||
self::JSON_PROPERTY_OWNER => $this->nodeOwner,
|
||||
self::JSON_PROPERTY_SIGNATURE => $this->nodeSignature,
|
||||
// node specific properties
|
||||
self::JSON_PROPERTY_LABEL => $this->nodeLabel,
|
||||
];
|
||||
}
|
||||
|
||||
public function jsonDeserialize(array|string $data): static {
|
||||
if (is_string($data)) {
|
||||
$data = json_decode($data, true);
|
||||
}
|
||||
// node meta properties
|
||||
$this->nodeIn = $data[self::JSON_PROPERTY_IN] ?? null;
|
||||
$this->nodeId = $data[self::JSON_PROPERTY_ID] ?? null;
|
||||
$this->nodeCreatedBy = $data[self::JSON_PROPERTY_CREATED_BY] ?? null;
|
||||
$this->nodeCreatedOn = isset($data[self::JSON_PROPERTY_CREATED_ON])
|
||||
? new DateTimeImmutable($data[self::JSON_PROPERTY_CREATED_ON])
|
||||
: null;
|
||||
$this->nodeModifiedBy = $data[self::JSON_PROPERTY_MODIFIED_BY] ?? null;
|
||||
$this->nodeModifiedOn = isset($data[self::JSON_PROPERTY_MODIFIED_ON])
|
||||
? new DateTimeImmutable($data[self::JSON_PROPERTY_MODIFIED_ON])
|
||||
: null;
|
||||
$this->nodeOwner = $data[self::JSON_PROPERTY_OWNER] ?? null;
|
||||
$this->nodeSignature = $data[self::JSON_PROPERTY_SIGNATURE] ?? null;
|
||||
// node specific properties
|
||||
$this->nodeLabel = $data[self::JSON_PROPERTY_LABEL] ?? null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fromStore(array|object $document): self {
|
||||
if (is_object($document)) {
|
||||
$document = (array) $document;
|
||||
}
|
||||
// node system properties
|
||||
$this->tenantId = $document['tid'] ?? null;
|
||||
$this->userId = $document['uid'] ?? null;
|
||||
$this->nodeId = $document['nid'] ?? null;
|
||||
$this->nodeIn = $document['pid'] ?? null;
|
||||
$this->nodeCreatedBy = $document['createdBy'] ?? null;
|
||||
$this->nodeCreatedOn = isset($document['createdOn'])
|
||||
? new DateTimeImmutable($document['createdOn'])
|
||||
: null;
|
||||
$this->nodeModifiedBy = $document['modifiedBy'] ?? null;
|
||||
$this->nodeModifiedOn = isset($document['modifiedOn'])
|
||||
? new DateTimeImmutable($document['modifiedOn'])
|
||||
: null;
|
||||
$this->nodeOwner = $document['owner'] ?? null;
|
||||
$this->nodeSignature = $document['signature'] ?? md5(json_encode([
|
||||
$this->nodeId, NodeType::Collection->value, $document['modifiedOn'] ?? ''
|
||||
]));
|
||||
// node specific properties
|
||||
$this->nodeLabel = $document['label'] ?? null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toStore(): array {
|
||||
$now = date('c');
|
||||
return [
|
||||
// node system properties
|
||||
'tid' => $this->tenantId,
|
||||
'uid' => $this->userId,
|
||||
'nid' => $this->nodeId,
|
||||
'pid' => $this->nodeIn,
|
||||
'type' => NodeType::Collection->value,
|
||||
'createdBy' => $this->nodeCreatedBy,
|
||||
'createdOn' => $this->nodeCreatedOn?->format('c') ?? $now,
|
||||
'modifiedBy' => $this->nodeModifiedBy,
|
||||
'modifiedOn' => $this->nodeModifiedOn?->format('c') ?? $now,
|
||||
'owner' => $this->nodeOwner,
|
||||
'signature' => $this->nodeSignature,
|
||||
// node specific properties
|
||||
'label' => $this->nodeLabel,
|
||||
];
|
||||
}
|
||||
|
||||
// Immutable properties
|
||||
|
||||
public function in(): string|int|null {
|
||||
return $this->nodeIn;
|
||||
}
|
||||
|
||||
public function id(): string|int {
|
||||
return $this->nodeId ?? '';
|
||||
}
|
||||
|
||||
public function type(): NodeType {
|
||||
return NodeType::Collection;
|
||||
}
|
||||
|
||||
public function createdBy(): string|null {
|
||||
return $this->nodeCreatedBy;
|
||||
}
|
||||
|
||||
public function createdOn(): DateTimeImmutable|null {
|
||||
return $this->nodeCreatedOn;
|
||||
}
|
||||
|
||||
public function modifiedBy(): string|null {
|
||||
return $this->nodeModifiedBy;
|
||||
}
|
||||
|
||||
public function modifiedOn(): DateTimeImmutable|null {
|
||||
return $this->nodeModifiedOn;
|
||||
}
|
||||
|
||||
public function signature(): string|null {
|
||||
return $this->nodeSignature;
|
||||
}
|
||||
|
||||
public function isCollection(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isEntity(): bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Mutable properties
|
||||
|
||||
public function getLabel(): string|null {
|
||||
return $this->nodeLabel;
|
||||
}
|
||||
|
||||
public function setLabel(string $value): static {
|
||||
$this->nodeLabel = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,226 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\FileProviderLocal\Providers\Personal;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use KTXF\Files\Node\INodeEntityBase;
|
||||
use KTXF\Files\Node\INodeEntityMutable;
|
||||
use KTXF\Files\Node\NodeType;
|
||||
|
||||
/**
|
||||
* NodeEntity implementation - represents a file/entity in the file system
|
||||
*/
|
||||
class NodeEntity implements INodeEntityBase, INodeEntityMutable {
|
||||
|
||||
// node system properties
|
||||
private string|null $tenantId = null;
|
||||
private string|null $userId = null;
|
||||
private string|int|null $nodeIn = null;
|
||||
private string|int|null $nodeId = null;
|
||||
private string|null $nodeCreatedBy = null;
|
||||
private DateTimeImmutable|null $nodeCreatedOn = null;
|
||||
private string|null $nodeModifiedBy = null;
|
||||
private DateTimeImmutable|null $nodeModifiedOn = null;
|
||||
private string|null $nodeOwner = null;
|
||||
private string|null $nodeSignature = null;
|
||||
// node specific properties
|
||||
private string|null $nodeLabel = null;
|
||||
private string|null $nodeMime = null;
|
||||
private string|null $nodeFormat = null;
|
||||
private string|null $nodeEncoding = null;
|
||||
private int $nodeSize = 0;
|
||||
|
||||
public function jsonSerialize(): mixed {
|
||||
return [
|
||||
// node meta properties
|
||||
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
||||
self::JSON_PROPERTY_IN => $this->nodeIn,
|
||||
self::JSON_PROPERTY_ID => $this->nodeId,
|
||||
self::JSON_PROPERTY_CREATED_BY => $this->nodeCreatedBy,
|
||||
self::JSON_PROPERTY_CREATED_ON => $this->nodeCreatedOn?->format('c'),
|
||||
self::JSON_PROPERTY_MODIFIED_BY => $this->nodeModifiedBy,
|
||||
self::JSON_PROPERTY_MODIFIED_ON => $this->nodeModifiedOn?->format('c'),
|
||||
self::JSON_PROPERTY_OWNER => $this->nodeOwner,
|
||||
self::JSON_PROPERTY_SIGNATURE => $this->nodeSignature,
|
||||
self::JSON_PROPERTY_SIZE => $this->nodeSize,
|
||||
// node specific properties
|
||||
self::JSON_PROPERTY_LABEL => $this->nodeLabel,
|
||||
self::JSON_PROPERTY_MIME => $this->nodeMime,
|
||||
self::JSON_PROPERTY_FORMAT => $this->nodeFormat,
|
||||
self::JSON_PROPERTY_ENCODING => $this->nodeEncoding,
|
||||
];
|
||||
}
|
||||
|
||||
public function jsonDeserialize(array|string $data): static {
|
||||
if (is_string($data)) {
|
||||
$data = json_decode($data, true);
|
||||
}
|
||||
// node meta properties
|
||||
$this->nodeIn = $data[self::JSON_PROPERTY_IN] ?? null;
|
||||
$this->nodeId = $data[self::JSON_PROPERTY_ID] ?? null;
|
||||
$this->nodeCreatedBy = $data[self::JSON_PROPERTY_CREATED_BY] ?? null;
|
||||
$this->nodeCreatedOn = isset($data[self::JSON_PROPERTY_CREATED_ON])
|
||||
? new DateTimeImmutable($data[self::JSON_PROPERTY_CREATED_ON])
|
||||
: null;
|
||||
$this->nodeModifiedBy = $data[self::JSON_PROPERTY_MODIFIED_BY] ?? null;
|
||||
$this->nodeModifiedOn = isset($data[self::JSON_PROPERTY_MODIFIED_ON])
|
||||
? new DateTimeImmutable($data[self::JSON_PROPERTY_MODIFIED_ON])
|
||||
: null;
|
||||
$this->nodeOwner = $data[self::JSON_PROPERTY_OWNER] ?? null;
|
||||
$this->nodeSignature = $data[self::JSON_PROPERTY_SIGNATURE] ?? null;
|
||||
$this->nodeSize = $data[self::JSON_PROPERTY_SIZE] ?? 0;
|
||||
// node specific properties
|
||||
$this->nodeLabel = $data[self::JSON_PROPERTY_LABEL] ?? null;
|
||||
$this->nodeMime = $data[self::JSON_PROPERTY_MIME] ?? null;
|
||||
$this->nodeFormat = $data[self::JSON_PROPERTY_FORMAT] ?? null;
|
||||
$this->nodeEncoding = $data[self::JSON_PROPERTY_ENCODING] ?? null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fromStore(array|object $document): self {
|
||||
if (is_object($document)) {
|
||||
$document = (array) $document;
|
||||
}
|
||||
// node system properties
|
||||
$this->tenantId = $document['tid'] ?? null;
|
||||
$this->userId = $document['uid'] ?? null;
|
||||
$this->nodeId = $document['nid'] ?? null;
|
||||
$this->nodeIn = $document['pid'] ?? null;
|
||||
$this->nodeCreatedBy = $document['createdBy'] ?? null;
|
||||
$this->nodeCreatedOn = isset($document['createdOn'])
|
||||
? new DateTimeImmutable($document['createdOn'])
|
||||
: null;
|
||||
$this->nodeModifiedBy = $document['modifiedBy'] ?? null;
|
||||
$this->nodeModifiedOn = isset($document['modifiedOn'])
|
||||
? new DateTimeImmutable($document['modifiedOn'])
|
||||
: null;
|
||||
$this->nodeOwner = $document['owner'] ?? null;
|
||||
$this->nodeSignature = $document['signature'] ?? md5(json_encode([
|
||||
$this->nodeId, NodeType::Entity->value, $document['modifiedOn'] ?? ''
|
||||
]));
|
||||
$this->nodeSize = $document['size'] ?? 0;
|
||||
// node specific properties
|
||||
$this->nodeLabel = $document['label'] ?? null;
|
||||
$this->nodeMime = $document['mime'] ?? null;
|
||||
$this->nodeFormat = $document['format'] ?? null;
|
||||
$this->nodeEncoding = $document['encoding'] ?? null;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toStore(): array {
|
||||
$now = date('c');
|
||||
return [
|
||||
// node system properties
|
||||
'tid' => $this->tenantId,
|
||||
'uid' => $this->userId,
|
||||
'nid' => $this->nodeId,
|
||||
'pid' => $this->nodeIn,
|
||||
'type' => NodeType::Entity->value,
|
||||
'createdBy' => $this->nodeCreatedBy,
|
||||
'createdOn' => $this->nodeCreatedOn?->format('c') ?? $now,
|
||||
'modifiedBy' => $this->nodeModifiedBy,
|
||||
'modifiedOn' => $this->nodeModifiedOn?->format('c') ?? $now,
|
||||
'owner' => $this->nodeOwner,
|
||||
'signature' => $this->nodeSignature,
|
||||
'size' => $this->nodeSize,
|
||||
// node specific properties
|
||||
'label' => $this->nodeLabel,
|
||||
'mime' => $this->nodeMime,
|
||||
'format' => $this->nodeFormat,
|
||||
'encoding' => $this->nodeEncoding,
|
||||
];
|
||||
}
|
||||
|
||||
// Immutable properties
|
||||
|
||||
public function in(): string|int|null {
|
||||
return $this->nodeIn;
|
||||
}
|
||||
|
||||
public function id(): string|int {
|
||||
return $this->nodeId ?? '';
|
||||
}
|
||||
|
||||
public function type(): NodeType {
|
||||
return NodeType::Entity;
|
||||
}
|
||||
|
||||
public function createdBy(): string|null {
|
||||
return $this->nodeCreatedBy;
|
||||
}
|
||||
|
||||
public function createdOn(): DateTimeImmutable|null {
|
||||
return $this->nodeCreatedOn;
|
||||
}
|
||||
|
||||
public function modifiedBy(): string|null {
|
||||
return $this->nodeModifiedBy;
|
||||
}
|
||||
|
||||
public function modifiedOn(): DateTimeImmutable|null {
|
||||
return $this->nodeModifiedOn;
|
||||
}
|
||||
|
||||
public function signature(): string|null {
|
||||
return $this->nodeSignature;
|
||||
}
|
||||
|
||||
public function size(): int {
|
||||
return $this->nodeSize;
|
||||
}
|
||||
|
||||
public function isCollection(): bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isEntity(): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Mutable properties
|
||||
|
||||
public function getLabel(): string|null {
|
||||
return $this->nodeLabel;
|
||||
}
|
||||
|
||||
public function setLabel(string $value): static {
|
||||
$this->nodeLabel = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getMime(): string|null {
|
||||
return $this->nodeMime;
|
||||
}
|
||||
|
||||
public function setMime(string $value): static {
|
||||
$this->nodeMime = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFormat(): string|null {
|
||||
return $this->nodeFormat;
|
||||
}
|
||||
|
||||
public function setFormat(string $value): static {
|
||||
$this->nodeFormat = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEncoding(): string|null {
|
||||
return $this->nodeEncoding;
|
||||
}
|
||||
|
||||
public function setEncoding(string $value): static {
|
||||
$this->nodeEncoding = $value;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -7,20 +7,21 @@ declare(strict_types=1);
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\FileProviderLocal\Providers;
|
||||
namespace KTXM\ProviderLocalDocuments\Providers;
|
||||
|
||||
use DI\Attribute\Inject;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use KTXF\Files\Provider\IProviderBase;
|
||||
use KTXF\Files\Service\IServiceBase;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
use KTXM\FileProviderLocal\Providers\Personal\PersonalService;
|
||||
use KTXF\Resource\Documents\Provider\ProviderBaseInterface;
|
||||
use KTXF\Resource\Documents\Service\ServiceBaseInterface;
|
||||
use KTXM\ProviderLocalDocuments\Module;
|
||||
use KTXM\ProviderLocalDocuments\Providers\Personal\PersonalService;
|
||||
|
||||
class Provider implements IProviderBase, ProviderInterface {
|
||||
class Provider implements ProviderBaseInterface {
|
||||
|
||||
protected const PROVIDER_ID = 'default';
|
||||
protected const PROVIDER_LABEL = 'Default File Provider';
|
||||
protected const PROVIDER_DESCRIPTION = 'Provides local file storage';
|
||||
public const JSON_TYPE = ProviderBaseInterface::JSON_TYPE;
|
||||
public const PROVIDER_IDENTIFIER = 'default';
|
||||
protected const PROVIDER_LABEL = Module::MODULE_LABEL;
|
||||
protected const PROVIDER_DESCRIPTION = Module::MODULE_DESCRIPTION;
|
||||
protected const PROVIDER_ICON = 'folder';
|
||||
|
||||
protected string $storeLocation = '/tmp/ktrix';
|
||||
@@ -30,8 +31,6 @@ class Provider implements IProviderBase, ProviderInterface {
|
||||
self::CAPABILITY_SERVICE_FETCH => true,
|
||||
self::CAPABILITY_SERVICE_EXTANT => true,
|
||||
];
|
||||
private ?array $servicesCache = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly ContainerInterface $container,
|
||||
#[Inject('rootDir')] private readonly string $rootDir,
|
||||
@@ -39,176 +38,94 @@ class Provider implements IProviderBase, ProviderInterface {
|
||||
$this->storeLocation = $this->rootDir . '/storage/';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function type(): string {
|
||||
return ProviderInterface::TYPE_FILES;
|
||||
}
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
||||
self::JSON_PROPERTY_IDENTIFIER => self::PROVIDER_IDENTIFIER,
|
||||
self::JSON_PROPERTY_LABEL => self::PROVIDER_LABEL,
|
||||
self::JSON_PROPERTY_CAPABILITIES => $this->providerAbilities,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function identifier(): string {
|
||||
return self::PROVIDER_ID;
|
||||
}
|
||||
public function jsonDeserialize(array|string $data): static
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function description(): string {
|
||||
return self::PROVIDER_DESCRIPTION;
|
||||
}
|
||||
public function type(): string
|
||||
{
|
||||
return self::TYPE_CHRONO;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function icon(): string {
|
||||
return self::PROVIDER_ICON;
|
||||
}
|
||||
public function identifier(): string
|
||||
{
|
||||
return self::PROVIDER_IDENTIFIER;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed {
|
||||
return $this->toJson();
|
||||
}
|
||||
public function label(): string
|
||||
{
|
||||
return self::PROVIDER_LABEL;
|
||||
}
|
||||
|
||||
public function toJson(): array {
|
||||
return [
|
||||
self::JSON_PROPERTY_TYPE => self::JSON_TYPE,
|
||||
self::JSON_PROPERTY_ID => self::PROVIDER_ID,
|
||||
self::JSON_PROPERTY_LABEL => self::PROVIDER_LABEL,
|
||||
self::JSON_PROPERTY_CAPABILITIES => $this->providerAbilities,
|
||||
];
|
||||
}
|
||||
public function description(): string
|
||||
{
|
||||
return self::PROVIDER_DESCRIPTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirms if specific capability is supported
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function capable(string $value): bool {
|
||||
if (isset($this->providerAbilities[$value])) {
|
||||
return (bool)$this->providerAbilities[$value];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public function icon(): string
|
||||
{
|
||||
return self::PROVIDER_ICON;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all supported capabilities
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function capabilities(): array {
|
||||
return $this->providerAbilities;
|
||||
}
|
||||
public function capable(string $value): bool
|
||||
{
|
||||
return !empty($this->providerAbilities[$value]);
|
||||
}
|
||||
|
||||
/**
|
||||
* An arbitrary unique text string identifying this provider
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function id(): string {
|
||||
return self::PROVIDER_ID;
|
||||
}
|
||||
public function capabilities(): array
|
||||
{
|
||||
return $this->providerAbilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* The localized human friendly name of this provider
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function label(): string {
|
||||
return self::PROVIDER_LABEL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve collection of services for a specific user
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function serviceList(string $tenantId, string $userId, array $filter = []): array {
|
||||
// if no filter is provided, return all services
|
||||
if ($filter === []) {
|
||||
$filter = ['personal', 'shared'];
|
||||
}
|
||||
// check if services are cached
|
||||
if (in_array('personal', $filter, true) && !isset($this->servicesCache[$userId]['personal'])) {
|
||||
$this->servicesCache[$userId]['personal'] = $this->serviceInstancePersonal($tenantId, $userId);
|
||||
}
|
||||
/*
|
||||
if (in_array('shared', $filter, true) && !isset($this->servicesCache[$userId]['shared'])) {
|
||||
$this->servicesCache[$userId]['shared'] = $this->serviceInstanceShared($tenantId, $userId);
|
||||
}
|
||||
*/
|
||||
// return requested services
|
||||
return array_intersect_key($this->servicesCache[$userId],array_flip($filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* construct service object instance
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @return PersonalService blank service instance
|
||||
*/
|
||||
protected function serviceInstancePersonal(string $tenantId, string $userId): PersonalService {
|
||||
$service = $this->container->get(PersonalService::class);
|
||||
$service->init($tenantId, $userId, $this->storeLocation . "$tenantId/$userId");
|
||||
$service->initialize($tenantId, $userId, $this->storeLocation . "$tenantId/$userId");
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if any services are configured for a specific user
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function serviceList(string $tenantId, string $userId, array $filter = []): array {
|
||||
// if no filter is provided, return all services
|
||||
if ($filter === []) {
|
||||
$filter = ['personal'];
|
||||
}
|
||||
// build services list
|
||||
$services = [];
|
||||
if (in_array('personal', $filter, true)) {
|
||||
$services['personal'] = $this->serviceInstancePersonal($tenantId, $userId);
|
||||
}
|
||||
return $services;
|
||||
}
|
||||
|
||||
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?ServiceBaseInterface {
|
||||
|
||||
if ($identifier === 'personal') {
|
||||
return $this->serviceInstancePersonal($tenantId, $userId);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public function serviceExtant(string $tenantId, string $userId, int|string ...$identifiers): array {
|
||||
$data = [];
|
||||
foreach ($identifiers as $id) {
|
||||
$data[$id] = match ($id) {
|
||||
'personal' => true,
|
||||
//'shared' => true,
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a service with a specific identifier
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function serviceFetch(string $tenantId, string $userId, string|int $identifier): ?IServiceBase {
|
||||
|
||||
// check if services are cached
|
||||
if (isset($this->servicesCache[$userId][$identifier])) {
|
||||
return $this->servicesCache[$userId][$identifier];
|
||||
}
|
||||
// convert to service object
|
||||
if ($identifier === 'personal') {
|
||||
$this->servicesCache[$userId][$identifier] = $this->serviceInstancePersonal($tenantId, $userId);
|
||||
}
|
||||
/*
|
||||
if ($identifier === 'shared') {
|
||||
$this->servicesCache[$userId][$identifier] = $this->serviceInstanceShared($tenantId, $userId);
|
||||
}
|
||||
*/
|
||||
|
||||
return $this->servicesCache[$userId][$identifier];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ declare(strict_types=1);
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\FileProviderLocal\Store;
|
||||
namespace KTXM\ProviderLocalDocuments\Store;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
|
||||
@@ -7,11 +7,9 @@ declare(strict_types=1);
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\FileProviderLocal\Store;
|
||||
namespace KTXM\ProviderLocalDocuments\Store;
|
||||
|
||||
use KTXC\Db\DataStore;
|
||||
use KTXF\Files\Node\INodeCollectionMutable;
|
||||
use KTXF\Files\Node\INodeEntityMutable;
|
||||
use KTXF\Files\Node\NodeType;
|
||||
use KTXF\Resource\Filter\Filter;
|
||||
use KTXF\Resource\Filter\FilterComparisonOperator;
|
||||
@@ -21,13 +19,15 @@ use KTXF\Resource\Range\IRangeTally;
|
||||
use KTXF\Resource\Range\RangeType;
|
||||
use KTXF\Resource\Sort\Sort;
|
||||
use KTXF\Utile\UUID;
|
||||
use KTXM\FileProviderLocal\Providers\Personal\NodeCollection;
|
||||
use KTXM\FileProviderLocal\Providers\Personal\NodeEntity;
|
||||
use KTXM\ProviderLocalDocuments\Providers\Personal\CollectionResource;
|
||||
use KTXM\ProviderLocalDocuments\Providers\Personal\EntityResource;
|
||||
use KTXM\ProviderLocalDocuments\Providers\Personal\NodeCollection;
|
||||
|
||||
class MetaStore {
|
||||
|
||||
protected string $_NodeTable = 'file_provider_local_node';
|
||||
protected string $_ChronicleTable = 'file_provider_local_chronicle';
|
||||
private const ROOT_COLLECTION_ID = '00000000-0000-0000-0000-000000000000';
|
||||
protected string $_NodeTable = 'provider_local_documents_node';
|
||||
protected string $_ChronicleTable = 'provider_local_documents_chronicle';
|
||||
|
||||
protected array $_CollectionFilterAttributeMap = [
|
||||
'id' => 'nid',
|
||||
@@ -137,7 +137,7 @@ class MetaStore {
|
||||
$query = [
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'pid' => $location,
|
||||
'cid' => empty($location) ? self::ROOT_COLLECTION_ID : $location,
|
||||
'type' => NodeType::Collection->value,
|
||||
];
|
||||
|
||||
@@ -157,8 +157,8 @@ class MetaStore {
|
||||
$cursor = $this->_store->selectCollection($this->_NodeTable)->find($query, $options);
|
||||
$list = [];
|
||||
foreach ($cursor as $entry) {
|
||||
$node = (new NodeCollection())->fromStore($entry);
|
||||
$list[$node->id()] = $node;
|
||||
$node = $this->collectionFresh()->fromStore($entry);
|
||||
$list[$node->identifier()] = $node;
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
@@ -166,14 +166,22 @@ class MetaStore {
|
||||
/**
|
||||
* Check if a collection exists
|
||||
*/
|
||||
public function collectionExtant(string $tenantId, string $userId, string|int $identifier): bool {
|
||||
$cursor = $this->_store->selectCollection($this->_NodeTable)->findOne([
|
||||
public function collectionExtant(string $tenantId, string $userId, string|int ...$identifiers): array {
|
||||
$cursor = $this->_store->selectCollection($this->_NodeTable)->find([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'nid' => $identifier,
|
||||
'nid' => ['$in' => $identifiers],
|
||||
'type' => NodeType::Collection->value,
|
||||
]);
|
||||
return $cursor !== null;
|
||||
], ['projection' => ['nid' => 1]]);
|
||||
$found = [];
|
||||
foreach ($cursor as $entry) {
|
||||
$found[(string)$entry['nid']] = true;
|
||||
}
|
||||
$result = [];
|
||||
foreach ($identifiers as $id) {
|
||||
$result[$id] = isset($found[(string)$id]);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -189,47 +197,50 @@ class MetaStore {
|
||||
|
||||
$list = [];
|
||||
foreach ($cursor as $entry) {
|
||||
$node = (new NodeCollection())->fromStore($entry);
|
||||
$list[$node->id()] = $node;
|
||||
$node = $this->collectionFresh()->fromStore($entry);
|
||||
$list[$node->identifier()] = $node;
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fresh collection instance
|
||||
*/
|
||||
protected function collectionFresh(): CollectionResource {
|
||||
return new CollectionResource();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a collection
|
||||
*/
|
||||
public function collectionCreate(string $tenantId, string $userId, string|int|null $location, INodeCollectionMutable $collection, array $options = []): NodeCollection {
|
||||
public function collectionCreate(string $tenantId, string $userId, string|int|null $location, CollectionResource $collection, array $options = []): CollectionResource {
|
||||
$data = [
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => empty($location) ? self::ROOT_COLLECTION_ID : $location,
|
||||
'nid' => UUID::v4(),
|
||||
'pid' => $location,
|
||||
'type' => NodeType::Collection->value,
|
||||
'createdBy' => $userId,
|
||||
'createdOn' => date('c'),
|
||||
'modifiedBy' => $userId,
|
||||
'modifiedOn' => date('c'),
|
||||
'owner' => $userId,
|
||||
'label' => $collection->getLabel(),
|
||||
'created' => date('c'),
|
||||
'modified' => date('c'),
|
||||
'properties' => $collection->getProperties()->toStore(),
|
||||
];
|
||||
$data['signature'] = md5(json_encode([$data['label'], $data['modifiedOn']]));
|
||||
$data['signature'] = time();
|
||||
|
||||
$this->_store->selectCollection($this->_NodeTable)->insertOne($data);
|
||||
$this->chronicleNode($tenantId, $userId, $data['nid'], 1);
|
||||
|
||||
return (new NodeCollection())->fromStore($data);
|
||||
return $this->collectionFresh()->fromStore($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify a collection
|
||||
*/
|
||||
public function collectionModify(string $tenantId, string $userId, string|int $identifier, INodeCollectionMutable $collection): NodeCollection {
|
||||
public function collectionModify(string $tenantId, string $userId, string|int $identifier, CollectionResource $collection): CollectionResource {
|
||||
$data = [
|
||||
'modifiedOn' => date('c'),
|
||||
'modifiedBy' => $userId,
|
||||
'label' => $collection->getLabel(),
|
||||
'modified' => date('c'),
|
||||
'properties' => $collection->getProperties()->toStore(),
|
||||
];
|
||||
$data['signature'] = md5(json_encode([$data['label'], $data['modifiedOn']]));
|
||||
$data['signature'] = time();
|
||||
|
||||
$this->_store->selectCollection($this->_NodeTable)->updateOne(
|
||||
['tid' => $tenantId, 'uid' => $userId, 'nid' => $identifier],
|
||||
@@ -273,11 +284,10 @@ class MetaStore {
|
||||
/**
|
||||
* Move a collection
|
||||
*/
|
||||
public function collectionMove(string $tenantId, string $userId, string|int $identifier, string|int|null $location): NodeCollection {
|
||||
public function collectionMove(string $tenantId, string $userId, string|int $identifier, string|int|null $location): CollectionResource {
|
||||
$data = [
|
||||
'pid' => $location,
|
||||
'modifiedBy' => $userId,
|
||||
'modifiedOn' => date('c'),
|
||||
'cid' => empty($location) ? self::ROOT_COLLECTION_ID : $location,
|
||||
'modified' => date('c'),
|
||||
];
|
||||
|
||||
$this->_store->selectCollection($this->_NodeTable)->updateOne(
|
||||
@@ -293,14 +303,14 @@ class MetaStore {
|
||||
/**
|
||||
* Copy a collection
|
||||
*/
|
||||
public function collectionCopy(string $tenantId, string $userId, string|int $identifier, string|int|null $location): NodeCollection {
|
||||
public function collectionCopy(string $tenantId, string $userId, string|int $identifier, string|int|null $location): CollectionResource {
|
||||
$collections = $this->collectionFetch($tenantId, $userId, $identifier);
|
||||
if (!isset($collections[$identifier])) {
|
||||
throw new \RuntimeException("Collection not found: $identifier");
|
||||
}
|
||||
|
||||
$source = $collections[$identifier];
|
||||
$newCollection = new NodeCollection();
|
||||
$newCollection = $this->collectionFresh();
|
||||
$newCollection->setLabel($source->getLabel());
|
||||
|
||||
$newNode = $this->collectionCreate($tenantId, $userId, $location, $newCollection);
|
||||
@@ -309,9 +319,9 @@ class MetaStore {
|
||||
$children = $this->nodeList($tenantId, $userId, $identifier, false);
|
||||
foreach ($children as $childId => $child) {
|
||||
if ($child->isCollection()) {
|
||||
$this->collectionCopy($tenantId, $userId, $childId, $newNode->id());
|
||||
$this->collectionCopy($tenantId, $userId, $childId, $newNode->identifier());
|
||||
} else {
|
||||
$this->entityCopy($tenantId, $userId, $identifier, $childId, $newNode->id());
|
||||
$this->entityCopy($tenantId, $userId, $identifier, $childId, $newNode->identifier());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -320,6 +330,13 @@ class MetaStore {
|
||||
|
||||
// ========== Entity Operations ==========
|
||||
|
||||
/**
|
||||
* Fresh entity instance
|
||||
*/
|
||||
protected function entityFresh(): EntityResource {
|
||||
return new EntityResource();
|
||||
}
|
||||
|
||||
/**
|
||||
* List entities in a collection
|
||||
*/
|
||||
@@ -327,7 +344,7 @@ class MetaStore {
|
||||
$query = [
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'pid' => $collection,
|
||||
'cid' => $collection,
|
||||
'type' => NodeType::Entity->value,
|
||||
];
|
||||
|
||||
@@ -352,8 +369,8 @@ class MetaStore {
|
||||
$cursor = $this->_store->selectCollection($this->_NodeTable)->find($query, $options);
|
||||
$list = [];
|
||||
foreach ($cursor as $entry) {
|
||||
$node = (new NodeEntity())->fromStore($entry);
|
||||
$list[$node->id()] = $node;
|
||||
$node = $this->entityFresh()->fromStore($entry);
|
||||
$list[$node->identifier()] = $node;
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
@@ -365,7 +382,7 @@ class MetaStore {
|
||||
$cursor = $this->_store->selectCollection($this->_NodeTable)->findOne([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'pid' => $collection,
|
||||
'cid' => $collection,
|
||||
'nid' => $identifier,
|
||||
'type' => NodeType::Entity->value,
|
||||
]);
|
||||
@@ -379,15 +396,15 @@ class MetaStore {
|
||||
$cursor = $this->_store->selectCollection($this->_NodeTable)->find([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'pid' => $collection,
|
||||
'cid' => $collection,
|
||||
'nid' => ['$in' => $identifiers],
|
||||
'type' => NodeType::Entity->value,
|
||||
]);
|
||||
|
||||
$list = [];
|
||||
foreach ($cursor as $entry) {
|
||||
$node = (new NodeEntity())->fromStore($entry);
|
||||
$list[$node->id()] = $node;
|
||||
$node = $this->entityFresh()->fromStore($entry);
|
||||
$list[$node->identifier()] = $node;
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
@@ -395,48 +412,52 @@ class MetaStore {
|
||||
/**
|
||||
* Create an entity
|
||||
*/
|
||||
public function entityCreate(string $tenantId, string $userId, string|int|null $collection, INodeEntityMutable $entity, array $options = []): NodeEntity {
|
||||
$data = [
|
||||
public function entityCreate(string $tenantId, string $userId, string|int|null $collection, EntityResource $entity, array $options = []): EntityResource {
|
||||
|
||||
$entity = [
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => empty($collection) ? self::ROOT_COLLECTION_ID : $collection,
|
||||
'nid' => UUID::v4(),
|
||||
'pid' => $collection,
|
||||
'type' => NodeType::Entity->value,
|
||||
'createdOn' => date('c'),
|
||||
'createdBy' => $userId,
|
||||
'modifiedOn' => date('c'),
|
||||
'modifiedBy' => $userId,
|
||||
'size' => 0,
|
||||
'mime' => $entity->getMime(),
|
||||
'format' => $entity->getFormat(),
|
||||
'encoding' => $entity->getEncoding(),
|
||||
'label' => $entity->getLabel(),
|
||||
'created' => date('c'),
|
||||
'modified' => date('c'),
|
||||
'properties' => $entity->getProperties()->toStore(),
|
||||
];
|
||||
$data['signature'] = md5(json_encode([$data['label'], $data['size'], $data['mime'], $data['modifiedOn']]));
|
||||
$entity['signature'] = time();
|
||||
|
||||
$this->_store->selectCollection($this->_NodeTable)->insertOne($data);
|
||||
$this->chronicleNode($tenantId, $userId, $data['nid'], 1);
|
||||
$this->_store->selectCollection($this->_NodeTable)->insertOne($entity);
|
||||
$this->chronicleNode($tenantId, $userId, $entity['nid'], 1);
|
||||
|
||||
return (new NodeEntity())->fromStore($data);
|
||||
return $this->entityFresh()->fromStore($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify an entity
|
||||
*/
|
||||
public function entityModify(string $tenantId, string $userId, string|int|null $collection, string|int $identifier, INodeEntityMutable $entity): NodeEntity {
|
||||
$data = [
|
||||
'label' => $entity->getLabel(),
|
||||
'mime' => $entity->getMime(),
|
||||
'format' => $entity->getFormat(),
|
||||
'encoding' => $entity->getEncoding(),
|
||||
'modifiedOn' => date('c'),
|
||||
'modifiedBy' => $userId,
|
||||
];
|
||||
$data['signature'] = md5(json_encode([$data['label'], $data['mime'], $data['modifiedOn']]));
|
||||
public function entityModify(string $tenantId, string $userId, string|int|null $collection, string|int $identifier, EntityResource|array $data, bool $partial = false): EntityResource {
|
||||
$attributes = [];
|
||||
|
||||
if ($data instanceof EntityResource) {
|
||||
$data = $data->getProperties()->toStore();
|
||||
}
|
||||
|
||||
if ($partial) {
|
||||
foreach ($data['properties'] ?? [] as $key => $value) {
|
||||
if ($value !== null) {
|
||||
$attributes["properties.$key"] = $value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$attributes['properties'] = $data['properties'] ?? [];
|
||||
}
|
||||
|
||||
$attributes['modified'] = date('c');
|
||||
$attributes['signature'] = time();
|
||||
|
||||
$this->_store->selectCollection($this->_NodeTable)->updateOne(
|
||||
['tid' => $tenantId, 'uid' => $userId, 'nid' => $identifier],
|
||||
['$set' => $data]
|
||||
['tid' => $tenantId, 'uid' => $userId, 'cid' => $collection, 'nid' => $identifier],
|
||||
['$set' => $attributes]
|
||||
);
|
||||
$this->chronicleNode($tenantId, $userId, $identifier, 2);
|
||||
|
||||
@@ -444,58 +465,31 @@ class MetaStore {
|
||||
return $entities[$identifier];
|
||||
}
|
||||
|
||||
/**
|
||||
* Update entity attributes
|
||||
*
|
||||
* Supported attributes: size, format, mime, encoding, label
|
||||
*
|
||||
* @param string $tenantId tenant identifier
|
||||
* @param string $userId user identifier
|
||||
* @param string|int $collection collection identifier
|
||||
* @param string|int $identifier entity identifier
|
||||
* @param array $attributes key-value pairs of attributes to update
|
||||
*/
|
||||
public function entityUpdate(string $tenantId, string $userId, string|int $collection, string|int $identifier, array $attributes): void {
|
||||
// Filter to allowed attributes only
|
||||
$allowed = ['size', 'format', 'mime', 'encoding', 'label'];
|
||||
$data = array_intersect_key($attributes, array_flip($allowed));
|
||||
|
||||
if (empty($data)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Always update modification timestamp
|
||||
$data['modifiedOn'] = date('c');
|
||||
|
||||
$this->_store->selectCollection($this->_NodeTable)->updateOne(
|
||||
['tid' => $tenantId, 'uid' => $userId, 'nid' => $identifier],
|
||||
['$set' => $data]
|
||||
);
|
||||
$this->chronicleNode($tenantId, $userId, $identifier, 2);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Destroy an entity
|
||||
*/
|
||||
public function entityDestroy(string $tenantId, string $userId, string|int|null $collection, string|int $identifier): bool {
|
||||
|
||||
$result = $this->_store->selectCollection($this->_NodeTable)->deleteOne([
|
||||
'tid' => $tenantId,
|
||||
'uid' => $userId,
|
||||
'cid' => $collection,
|
||||
'nid' => $identifier
|
||||
]);
|
||||
|
||||
|
||||
if ($result->getDeletedCount() === 1) {
|
||||
$this->chronicleNode($tenantId, $userId, $identifier, 3);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Move an entity to another collection
|
||||
*/
|
||||
public function entityMove(string $tenantId, string $userId, string|int|null $collection, string|int $identifier, string|int|null $destination): NodeEntity {
|
||||
public function entityMove(string $tenantId, string $userId, string|int|null $collection, string|int $identifier, string|int|null $destination): EntityResource {
|
||||
$data = [
|
||||
'pid' => $destination,
|
||||
'modifiedOn' => date('c'),
|
||||
@@ -503,7 +497,7 @@ class MetaStore {
|
||||
];
|
||||
|
||||
$this->_store->selectCollection($this->_NodeTable)->updateOne(
|
||||
['tid' => $tenantId, 'uid' => $userId, 'nid' => $identifier],
|
||||
['tid' => $tenantId, 'uid' => $userId, 'nid' => $identifier, 'cid' => $collection],
|
||||
['$set' => $data]
|
||||
);
|
||||
$this->chronicleNode($tenantId, $userId, $identifier, 2);
|
||||
@@ -515,14 +509,14 @@ class MetaStore {
|
||||
/**
|
||||
* Copy an entity
|
||||
*/
|
||||
public function entityCopy(string $tenantId, string $userId, string|int|null $collection, string|int $identifier, string|int|null $destination): NodeEntity {
|
||||
public function entityCopy(string $tenantId, string $userId, string|int|null $collection, string|int $identifier, string|int|null $destination): EntityResource {
|
||||
$entities = $this->entityFetch($tenantId, $userId, $collection, $identifier);
|
||||
if (!isset($entities[$identifier])) {
|
||||
throw new \RuntimeException("Entity not found: $identifier");
|
||||
}
|
||||
|
||||
$source = $entities[$identifier];
|
||||
$newEntity = new NodeEntity();
|
||||
$newEntity = new EntityResource();
|
||||
$newEntity->setLabel($source->getLabel());
|
||||
$newEntity->setMime($source->getMime());
|
||||
$newEntity->setFormat($source->getFormat());
|
||||
@@ -588,7 +582,7 @@ class MetaStore {
|
||||
if ($nodeType === NodeType::Collection->value) {
|
||||
$node = (new NodeCollection())->fromStore($entry);
|
||||
} else {
|
||||
$node = (new NodeEntity())->fromStore($entry);
|
||||
$node = (new EntityResource())->fromStore($entry);
|
||||
}
|
||||
$list[$node->id()] = $node;
|
||||
}
|
||||
@@ -737,7 +731,7 @@ class MetaStore {
|
||||
if ($nodeType === NodeType::Collection->value) {
|
||||
$node = (new NodeCollection())->fromStore($entry);
|
||||
} else {
|
||||
$node = (new NodeEntity())->fromStore($entry);
|
||||
$node = (new EntityResource())->fromStore($entry);
|
||||
}
|
||||
$list[$node->id()] = $node;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user