Skip to content

Internals Vfs Storage StorageRepository

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

vfs\storage\StorageRepository

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/vfs/storage/StorageRepository.php · Snapshot 8411f4287706.

VFS includes package resource lookup, database-backed file metadata, filesystem collaborators, transient pools and persistent PHP-array caches.

Imports and dependencies

use database\DatabaseManager;
use model\exceptions\ModelCRUDException;
use vfs\storage\interfaces\IStorageRepository;
use vfs\storage\enums\EOwnerType;

StorageRepository

Declaration and architectural role

class StorageRepository implements IStorageRepository

Source description (behavioral corrections are recorded in Known discrepancies):

Class StorageRepository

Implements IStorageRepository (DIP). All database access is encapsulated here so higher-level classes depend on the interface, not this class. Method names are also standardised to match the interface contract.

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$database private DatabaseManager $database; Used by __construct(), fetchAll(), fetchById(), fetchByOwner(), fetchByName(), fetchByOwnerType(), fetchByOwnerTypeAndOwner(), fetchByOwnerAndName(), create(), update(), delete(), hydrateCollection().
Constructor-promoted public function __construct(?DatabaseManager $database = null) Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct(?DatabaseManager $database = null)
fetchAll public function fetchAll(): array
fetchById public function fetchById(int $id): StorageEntry
fetchByOwner public function fetchByOwner(int $ownerId): array
fetchByName public function fetchByName(string $name): array
fetchByOwnerType public function fetchByOwnerType(EOwnerType $ownerType): array
fetchByOwnerTypeAndOwner public function fetchByOwnerTypeAndOwner(EOwnerType $ownerType, int $ownerId): array
fetchByOwnerAndName public function fetchByOwnerAndName(int $ownerId, string $name): ?StorageEntry
create public function create(StorageEntry $entry): StorageEntry
update public function update(StorageEntry $entry): bool
delete public function delete(StorageEntry $entry): bool
data private function data(StorageEntry $entry): array
hydrateCollection private function hydrateCollection(array $rows): array

__construct

Source line 21

public function __construct(?DatabaseManager $database = null)

Parameters

Parameter Type, modifiers and default Purpose
$database ?DatabaseManager $database = null Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->database.

Implementation, validation guards and interaction order
public function __construct(?DatabaseManager $database = null)
    {
        $this->database = $database ?? new DatabaseManager();
    }

fetchAll

Source line 30

public function fetchAll(): array

Return expressions (including local callbacks): $this->hydrateCollection($rows).

Implementation, validation guards and interaction order
// ----------------------------------------------------------------
    // IStorageRepository implementation
    // ----------------------------------------------------------------

    public function fetchAll(): array
    {
        $rows = $this->database
            ->table('awt_storage')
            ->select()
            ->get();

        return $this->hydrateCollection($rows);
    }

fetchById

Source line 40

public function fetchById(int $id): StorageEntry

Parameters

Parameter Type, modifiers and default Purpose
$id int $id Record or runtime identifier selected by this operation.

Return expressions (including local callbacks): $this->hydrateCollection($rows)[0].

Explicit exceptions: \OutOfBoundsException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
public function fetchById(int $id): StorageEntry
    {
        $rows = $this->database->table('awt_storage')->select()->where(['id' => $id])->get(1);
        if ($rows === []) throw new \OutOfBoundsException("Storage entry not found: {$id}");
        return $this->hydrateCollection($rows)[0];
    }

fetchByOwner

Source line 47

public function fetchByOwner(int $ownerId): array

Parameters

Parameter Type, modifiers and default Purpose
$ownerId int $ownerId Owning record ID or null.

Return expressions (including local callbacks): $this->hydrateCollection($rows).

Implementation, validation guards and interaction order
public function fetchByOwner(int $ownerId): array
    {
        $rows = $this->database
            ->table('awt_storage')
            ->select()
            ->where(['ownerId' => $ownerId])
            ->get();

        return $this->hydrateCollection($rows);
    }

fetchByName

Source line 58

public function fetchByName(string $name): array

Parameters

Parameter Type, modifiers and default Purpose
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.

Return expressions (including local callbacks): $this->hydrateCollection($rows).

Implementation, validation guards and interaction order
public function fetchByName(string $name): array
    {
        $rows = $this->database
            ->table('awt_storage')
            ->select()
            ->where(['name' => $name])
            ->get();

        return $this->hydrateCollection($rows);
    }

fetchByOwnerType

Source line 69

public function fetchByOwnerType(EOwnerType $ownerType): array

Parameters

Parameter Type, modifiers and default Purpose
$ownerType EOwnerType $ownerType Storage owner category; a matching registered strategy is required for registration.

Return expressions (including local callbacks): $this->hydrateCollection($rows).

Implementation, validation guards and interaction order
public function fetchByOwnerType(EOwnerType $ownerType): array
    {
        $rows = $this->database
            ->table('awt_storage')
            ->select()
            ->where(['ownerType' => $ownerType->value])
            ->get();

        return $this->hydrateCollection($rows);
    }

fetchByOwnerTypeAndOwner

Source line 80

public function fetchByOwnerTypeAndOwner(EOwnerType $ownerType, int $ownerId): array

Parameters

Parameter Type, modifiers and default Purpose
$ownerType EOwnerType $ownerType Storage owner category; a matching registered strategy is required for registration.
$ownerId int $ownerId Owning record ID or null.

Return expressions (including local callbacks): $this->hydrateCollection($rows).

Implementation, validation guards and interaction order
public function fetchByOwnerTypeAndOwner(EOwnerType $ownerType, int $ownerId): array
    {
        $rows = $this->database
            ->table('awt_storage')
            ->select()
            ->where(['ownerType' => $ownerType->value, 'ownerId' => $ownerId])
            ->get();

        return $this->hydrateCollection($rows);
    }

fetchByOwnerAndName

Source line 91

public function fetchByOwnerAndName(int $ownerId, string $name): ?StorageEntry

Parameters

Parameter Type, modifiers and default Purpose
$ownerId int $ownerId Owning record ID or null.
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.

Return expressions (including local callbacks): null; $this->hydrateCollection($rows)[0].

Implementation, validation guards and interaction order
public function fetchByOwnerAndName(int $ownerId, string $name): ?StorageEntry
    {
        $rows = $this->database
            ->table('awt_storage')
            ->select()
            ->where(['ownerId' => $ownerId, 'name' => $name])
            ->get();

        if (empty($rows)) {
            return null;
        }

        return $this->hydrateCollection($rows)[0];
    }

create

Source line 109

public function create(StorageEntry $entry): StorageEntry

Parameters

Parameter Type, modifiers and default Purpose
$entry StorageEntry $entry Operation input; the exact use, validation and propagation are shown in the implementation trace.

@throws ModelCRUDException

Return expressions (including local callbacks): $this->database->transaction(function () use ($entry, $oldUrl): StorageEntry { $id = $this->database->table('awt_storage')->insert($this->data($entry))->executeInsert(); $entry.

Explicit exceptions: \RuntimeException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Failure handling catches \Throwable.

Implementation, validation guards and interaction order
/**
     * @throws ModelCRUDException
     */
    public function create(StorageEntry $entry): StorageEntry
    {
        $oldId = $entry->id;
        $oldUrl = $entry->url;
        try {
            return $this->database->transaction(function () use ($entry, $oldUrl): StorageEntry {
                $id = $this->database->table('awt_storage')->insert($this->data($entry))->executeInsert();
                if ($id === null) throw new \RuntimeException('Failed to register storage entry.');
                $entry->id = $id;
                $entry->setUrl($oldUrl);
                // Explicit URLs were persisted by the insert; only generated URLs need an update.
                if ($entry->url !== $oldUrl && !$this->update($entry)) {
                    throw new \RuntimeException('Failed to persist storage URL.');
                }
                $entry->setModelId($id);
                return $entry;
            });
        } catch (\Throwable $e) {
            $entry->id = $oldId;
            $entry->url = $oldUrl;
            if ($oldId !== null) $entry->setModelId($oldId);
            throw $e;
        }
    }

update

Source line 134

public function update(StorageEntry $entry): bool

Parameters

Parameter Type, modifiers and default Purpose
$entry StorageEntry $entry Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return expressions (including local callbacks): $this->database->table('awt_storage')->where(['id' => $entry->id])->update($this->data($entry)).

Explicit exceptions: \LogicException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
public function update(StorageEntry $entry): bool
    {
        if ($entry->id === null) throw new \LogicException('Cannot update an unregistered storage entry.');
        return $this->database->table('awt_storage')->where(['id' => $entry->id])->update($this->data($entry));
    }

delete

Source line 140

public function delete(StorageEntry $entry): bool

Parameters

Parameter Type, modifiers and default Purpose
$entry StorageEntry $entry Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return expressions (including local callbacks): false; $this->database->table('awt_storage')->where(['id' => $entry->id])->delete().

Implementation, validation guards and interaction order
public function delete(StorageEntry $entry): bool
    {
        if ($entry->id === null) return false;
        return $this->database->table('awt_storage')->where(['id' => $entry->id])->delete();
    }

data

Source line 146

private function data(StorageEntry $entry): array

Parameters

Parameter Type, modifiers and default Purpose
$entry StorageEntry $entry Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return expressions (including local callbacks): [ 'name' => $entry->name, 'path' => $entry->path, 'url' => $entry->url, 'size' => $entry->size, 'middleware' => $entry->middleware, 'lastModified' => $entry->lastModified, 'ownerId' => $entry->ownerId, 'ownerType' => $entry->ownerType instanceof EOwnerType ? $entry->ownerType->value : $entry->ownerType, ].

Implementation, validation guards and interaction order
private function data(StorageEntry $entry): array
    {
        return [
            'name' => $entry->name, 'path' => $entry->path, 'url' => $entry->url,
            'size' => $entry->size, 'middleware' => $entry->middleware,
            'lastModified' => $entry->lastModified, 'ownerId' => $entry->ownerId,
            'ownerType' => $entry->ownerType instanceof EOwnerType ? $entry->ownerType->value : $entry->ownerType,
        ];
    }

hydrateCollection

Source line 166

private function hydrateCollection(array $rows): array

Parameters

Parameter Type, modifiers and default Purpose
$rows array $rows * @return StorageEntry[]

Hydrates complete rows without additional database lookups.

@param array $rows @return StorageEntry[]

Return expressions (including local callbacks): array_map( fn(array $row): StorageEntry => (new StorageEntry())->useConnectionFrom($this->database)->hydrateRow($row, 'awt_storage', 'id'), $rows ).

Implementation, validation guards and interaction order
// ----------------------------------------------------------------
    // Private helpers
    // ----------------------------------------------------------------

    /**
     * Hydrates complete rows without additional database lookups.
     *
     * @param array $rows
     * @return StorageEntry[]
     */
    private function hydrateCollection(array $rows): array
    {
        return array_map(
            fn(array $row): StorageEntry => (new StorageEntry())->useConnectionFrom($this->database)->hydrateRow($row, 'awt_storage', 'id'),
            $rows
        );
    }

Related types

Clone this wiki locally