Skip to content

Internals Vfs Storage Strategies PackageOwnerStorageStrategy

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

vfs\storage\strategies\PackageOwnerStorageStrategy

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/vfs/storage/strategies/PackageOwnerStorageStrategy.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 vfs\storage\interfaces\IFileSystemService;
use vfs\storage\interfaces\IOwnerStorageStrategy;
use vfs\storage\interfaces\IStorageRepository;
use vfs\storage\enums\EOwnerType;
use vfs\storage\StorageEntry;

PackageOwnerStorageStrategy

Declaration and architectural role

class PackageOwnerStorageStrategy implements IOwnerStorageStrategy

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

Class PackageOwnerStorageStrategy

Handles file registration for the PACKAGE owner type (OCP). Adding new EOwnerType strategies never requires touching StorageManager.

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
const PACKAGES_DIR private const PACKAGES_DIR = 'packages'; State or definition consumed by the methods below; reads and writes are preserved in their implementation traces.
Constructor-promoted private readonly IFileSystemService $fileSystem Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private readonly IStorageRepository $repository Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private readonly string $storageBasePath Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct( private readonly IFileSystemService $fileSystem, private readonly IStorageRepository $repository, private readonly string $storageBasePath )
supports public function supports(): EOwnerType
register public function register(StorageEntry $entry, string $ownerName): bool

__construct

Source line 22

public function __construct(
        private readonly IFileSystemService $fileSystem,
        private readonly IStorageRepository $repository,
        private readonly string             $storageBasePath
    );

Parameters

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

Concrete no-op/default hook: this method performs no work. Override it where the lifecycle or extension contract calls for behavior.

supports

Source line 28

public function supports(): EOwnerType

Return expressions (including local callbacks): EOwnerType::PACKAGE.

Implementation, validation guards and interaction order
public function supports(): EOwnerType
    {
        return EOwnerType::PACKAGE;
    }

register

Source line 33

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

Return expressions (including local callbacks): true.

Explicit exceptions: \InvalidArgumentException, \OutOfBoundsException, \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 register(StorageEntry $entry, string $ownerName): bool
    {
        if ($ownerName === '' || $ownerName === '.' || $ownerName === '..' || preg_match('/[\\\\\/\x00]/', $ownerName)) {
            throw new \InvalidArgumentException('Owner name must be a single directory name.');
        }
        if ($entry->getOwnerId() === null) {
            $rows = (new DatabaseManager())->table('awt_package')->select(['id'])->where(['name' => $ownerName])->get(1);
            if ($rows === []) throw new \OutOfBoundsException("Package not found: {$ownerName}");
            $entry->setOwnerId($rows[0]['id']);
        }
        $directory = rtrim($this->storageBasePath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR
            . self::PACKAGES_DIR . DIRECTORY_SEPARATOR . $ownerName;
        if (!$this->fileSystem->makeDirectory($directory)) throw new \RuntimeException("Could not create directory: {$directory}");
        $oldPath = $entry->getPath();
        $oldUrl = $entry->url;
        $destination = \vfs\storage\FileName::unique($directory, $oldPath);
        if (!$this->fileSystem->move($oldPath, $destination)) throw new \RuntimeException('Could not move storage file.');
        try {
            $entry->setPath($destination)->setSize($this->fileSystem->fileSize($destination))
                ->setLastModified($this->fileSystem->lastModified($destination));
            $created = $this->repository->create($entry);
            if ($created->id === null) throw new \RuntimeException('Failed to register storage entry.');
            return true;
        } catch (\Throwable $e) {
            if (!$this->fileSystem->move($destination, $oldPath)) {
                throw new \RuntimeException("Could not restore failed registration; file remains at {$destination}.", previous: $e);
            }
            $entry->setPath($oldPath);
            $entry->url = $oldUrl;
            throw $e;
        }
    }

Related types

Clone this wiki locally