Skip to content

Internals Vfs Storage StorageManager

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

vfs\storage\StorageManager

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/vfs/storage/StorageManager.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 context\events\GetContextEvent;
use vfs\storage\interfaces\IFileSystemService;
use vfs\storage\interfaces\IOwnerStorageStrategy;
use vfs\storage\interfaces\IStorageManager;
use vfs\storage\interfaces\IStorageRepository;

StorageManager

Declaration and architectural role

class StorageManager implements IStorageManager

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
$strategies private array $strategies = []; Used by registerStrategy(), registerLocal().
Constructor-promoted private readonly IStorageRepository $repository Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private readonly IFileSystemService $fileSystem Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct( private readonly IStorageRepository $repository, private readonly IFileSystemService $fileSystem, array $strategies = [] )
registerStrategy public function registerStrategy(IOwnerStorageStrategy $strategy): void
get public function get(int $id): StorageEntry
move public function move(int $id, string $location): bool
rename public function rename(int $id, string $name): bool
relocate private function relocate(StorageEntry $entry, string $destination, ?string $name = null): bool
restore private function restore(string $source, string $destination): void
delete public function delete(int $id): bool
copy public function copy(int $id): StorageEntry
registerLocal public function registerLocal(StorageEntry $entry, ?string $ownerName = null): bool

__construct

Source line 15

public function __construct(
        private readonly IStorageRepository $repository,
        private readonly IFileSystemService $fileSystem,
        array $strategies = []
    )

Parameters

Parameter Type, modifiers and default Purpose
$repository private readonly IStorageRepository $repository Operation input; the exact use, validation and propagation are shown in the implementation trace.
$fileSystem private readonly IFileSystemService $fileSystem Operation input; the exact use, validation and propagation are shown in the implementation trace.
$strategies array $strategies = [] Operation input; the exact use, validation and propagation are shown in the implementation trace.

Delegates to registerStrategy().

Implementation, validation guards and interaction order
public function __construct(
        private readonly IStorageRepository $repository,
        private readonly IFileSystemService $fileSystem,
        array $strategies = []
    ) {
        foreach ($strategies as $strategy) $this->registerStrategy($strategy);
    }

registerStrategy

Source line 23

public function registerStrategy(IOwnerStorageStrategy $strategy): void

Parameters

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

State writes: $this->strategies[$strategy->supports()->value].

Implementation, validation guards and interaction order
public function registerStrategy(IOwnerStorageStrategy $strategy): void
    {
        $this->strategies[$strategy->supports()->value] = $strategy;
    }

get

Source line 28

public function get(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->repository->fetchById($id).

Implementation, validation guards and interaction order
public function get(int $id): StorageEntry { return $this->repository->fetchById($id); }

move

Source line 30

public function move(int $id, string $location): bool

Parameters

Parameter Type, modifiers and default Purpose
$id int $id Record or runtime identifier selected by this operation.
$location string $location Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return expressions (including local callbacks): false; $this->relocate($entry, FileName::unique($location, $entry->getPath())).

Implementation, validation guards and interaction order
public function move(int $id, string $location): bool
    {
        $entry = $this->get($id);
        if (!$this->fileSystem->makeDirectory($location)) return false;
        return $this->relocate($entry, FileName::unique($location, $entry->getPath()));
    }

rename

Source line 37

public function rename(int $id, string $name): bool

Parameters

Parameter Type, modifiers and default Purpose
$id int $id Record or runtime identifier selected by this operation.
$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->relocate($entry, FileName::unique(dirname($entry->getPath()), $entry->getPath()), $name).

Implementation, validation guards and interaction order
public function rename(int $id, string $name): bool
    {
        $entry = $this->get($id);
        return $this->relocate($entry, FileName::unique(dirname($entry->getPath()), $entry->getPath()), $name);
    }

relocate

Source line 43

private function relocate(StorageEntry $entry, string $destination, ?string $name = null): 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.
$destination string $destination Operation input; the exact use, validation and propagation are shown in the implementation trace.
$name ?string $name = null Named key/label selected by this API; its exact scope is shown by the implementation trace.

Return expressions (including local callbacks): false; true.

Failure handling catches \Throwable.

Implementation, validation guards and interaction order
private function relocate(StorageEntry $entry, string $destination, ?string $name = null): bool
    {
        $oldPath = $entry->getPath();
        $oldName = $entry->getName();
        $oldUrl = $entry->url;
        if (!$this->fileSystem->move($oldPath, $destination)) return false;
        try {
            $entry->setPath($destination);
            if ($name !== null) {
                $entry->setName($name);
                if ($oldUrl === null || $oldUrl === '/storage/' . $entry->id . '/' . rawurlencode($oldName)
                    || $oldUrl === '/storage/' . $entry->id . '/' . $oldName) $entry->setUrl();
            }
            if ($this->repository->update($entry)) return true;
        } catch (\Throwable $e) {
            $this->restore($destination, $oldPath);
            $entry->setPath($oldPath)->setName($oldName);
            $entry->url = $oldUrl;
            throw $e;
        }
        $this->restore($destination, $oldPath);
        $entry->setPath($oldPath)->setName($oldName);
        $entry->url = $oldUrl;
        return false;
    }

restore

Source line 69

private function restore(string $source, string $destination): void

Parameters

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

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

Implementation, validation guards and interaction order
private function restore(string $source, string $destination): void
    {
        if (!$this->fileSystem->move($source, $destination)) {
            throw new \RuntimeException("Could not restore file to {$destination}; file remains at {$source}.");
        }
    }

delete

Source line 76

public function delete(int $id): bool

Parameters

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

Return expressions (including local callbacks): false; true.

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
public function delete(int $id): bool
    {
        $entry = $this->get($id);
        $path = $entry->getPath();
        $backup = FileName::unique(dirname($path), $path);
        if (!$this->fileSystem->move($path, $backup)) return false;
        try {
            $deleted = $this->repository->delete($entry);
        } catch (\Throwable $e) {
            $this->restore($backup, $path);
            throw $e;
        }
        if (!$deleted) { $this->restore($backup, $path); return false; }
        if (!$this->fileSystem->delete($backup)) {
            throw new \RuntimeException("Storage record deleted, but file cleanup failed: {$backup}");
        }
        return true;
    }

copy

Source line 95

public function copy(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->repository->create($copy).

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.

External effects include copy.

Implementation, validation guards and interaction order
public function copy(int $id): StorageEntry
    {
        $entry = $this->get($id);
        $path = FileName::unique(dirname($entry->getPath()), $entry->getPath());
        if (!$this->fileSystem->copy($entry->getPath(), $path)) throw new \RuntimeException('Could not copy storage file.');
        try {
            $copy = (new StorageEntry())->setPath($path)->setName('copy_' . $entry->getName())
                ->setSize($this->fileSystem->fileSize($path))->setLastModified($this->fileSystem->lastModified($path))
                ->setOwnerType($entry->getOwnerType())->setOwnerId($entry->getOwnerId())
                ->setMiddleware($entry->getMiddleware());
            return $this->repository->create($copy);
        } catch (\Throwable $e) {
            if (!$this->fileSystem->delete($path)) throw new \RuntimeException("Could not clean up failed copy: {$path}", previous: $e);
            throw $e;
        }
    }

registerLocal

Source line 112

public function registerLocal(StorageEntry $entry, ?string $ownerName = null): 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.
$ownerName ?string $ownerName = null Operation input; the exact use, validation and propagation are shown in the implementation trace.

Return expressions (including local callbacks): $this->strategies[$ownerType->value]->register($entry, $ownerName).

Explicit exceptions: \RuntimeException, \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 registerLocal(StorageEntry $entry, ?string $ownerName = null): bool
    {
        $ownerType = $entry->getOwnerType();
        if ($ownerType === null || !isset($this->strategies[$ownerType->value])) {
            throw new \RuntimeException('No storage strategy registered for owner type: ' . ($ownerType?->value ?? 'null'));
        }
        if ($ownerName === null) {
            global $eventDispatcher;
            if (!isset($eventDispatcher)) throw new \LogicException('Supply an owner name when no context dispatcher is available.');
            $event = new GetContextEvent();
            $eventDispatcher->dispatch($event);
            $context = $event->getContext();
            $ownerName = $context->contextName;
            if ($entry->getOwnerId() === null) $entry->setOwnerId($context->contextId);
        }
        return $this->strategies[$ownerType->value]->register($entry, $ownerName);
    }

Related types

Clone this wiki locally