Initial commit
This commit is contained in:
214
lib/Providers/Provider.php
Normal file
214
lib/Providers/Provider.php
Normal file
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\FileProviderLocal\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;
|
||||
|
||||
class Provider implements IProviderBase, ProviderInterface {
|
||||
|
||||
protected const PROVIDER_ID = 'default';
|
||||
protected const PROVIDER_LABEL = 'Default File Provider';
|
||||
protected const PROVIDER_DESCRIPTION = 'Provides local file storage';
|
||||
protected const PROVIDER_ICON = 'folder';
|
||||
|
||||
protected string $storeLocation = '/tmp/ktrix';
|
||||
|
||||
protected array $providerAbilities = [
|
||||
self::CAPABILITY_SERVICE_LIST => true,
|
||||
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,
|
||||
) {
|
||||
$this->storeLocation = $this->rootDir . '/storage/';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function type(): string {
|
||||
return ProviderInterface::TYPE_FILES;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function identifier(): string {
|
||||
return self::PROVIDER_ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function description(): string {
|
||||
return self::PROVIDER_DESCRIPTION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function icon(): string {
|
||||
return self::PROVIDER_ICON;
|
||||
}
|
||||
|
||||
public function jsonSerialize(): mixed {
|
||||
return $this->toJson();
|
||||
}
|
||||
|
||||
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,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all supported capabilities
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function capabilities(): array {
|
||||
return $this->providerAbilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* An arbitrary unique text string identifying this provider
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function id(): string {
|
||||
return self::PROVIDER_ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if any services are configured for a specific user
|
||||
*
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @inheritdoc
|
||||
*/
|
||||
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];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user