Skip to content

Internals Installer Package PackageStorageTreeGenerator

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

installer\package\PackageStorageTreeGenerator

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/installer/package/PackageStorageTreeGenerator.php · Snapshot 8411f4287706.

Installation validates archives and manifests, moves files, persists metadata, copies data into storage, invokes an optional hook and emits an event. These operations do not form one atomic transaction.

Imports and dependencies

use installer\interfaces\package\IPackageStorageTreeGenerator;
use vfs\storage\interfaces\IFileSystemService;
use vfs\storage\interfaces\IStorageRepository;
use vfs\storage\StorageEntry;
use vfs\storage\enums\EOwnerType;

PackageStorageTreeGenerator

Declaration and architectural role

class PackageStorageTreeGenerator implements IPackageStorageTreeGenerator

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
$storageTree private array $storageTree; Used by __construct(), buildStorageTree(), generate(), getStorageTree(), scanDirectory().
$entries private array $entries; Used by __construct(), generate(), registerItems().
Constructor-promoted private readonly IFileSystemService $fsService 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 string $source = '' Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private string $destination = '' Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private string $baseUrl = '' Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private int $packageId = 0 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 $fsService, private readonly IStorageRepository $repository, private string $source = '', private string $destination = '', private string $baseUrl = '', private int $packageId = 0, )
setSource public function setSource(string $source): void
setDestination public function setDestination(string $destination): void
setPackageId public function setPackageId(int $packageId): void
setBaseUrl public function setBaseUrl(string $baseUrl): void
buildStorageTree public function buildStorageTree(): IPackageStorageTreeGenerator
generate public function generate(): IPackageStorageTreeGenerator
replaceAsset private function replaceAsset(string $source, string $destination): void
getStorageTree public function getStorageTree(): array
registerItems public function registerItems(): bool
scanDirectory private function scanDirectory(string $dir): void

__construct

Source line 16

public function __construct(
        private readonly IFileSystemService $fsService,
        private readonly IStorageRepository $repository,
        private string $source = '',
        private string $destination = '',
        private string $baseUrl = '',
        private int $packageId = 0,
    )

Parameters

Parameter Type, modifiers and default Purpose
$fsService private readonly IFileSystemService $fsService 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.
$source private string $source = '' Operation input; the exact use, validation and propagation are shown in the implementation trace.
$destination private string $destination = '' Operation input; the exact use, validation and propagation are shown in the implementation trace.
$baseUrl private string $baseUrl = '' Operation input; the exact use, validation and propagation are shown in the implementation trace.
$packageId private int $packageId = 0 Installed package ID used for metadata/schema/storage ownership.

State writes: $this->storageTree, $this->entries.

Implementation, validation guards and interaction order
public function __construct(
        private readonly IFileSystemService $fsService,
        private readonly IStorageRepository $repository,
        private string $source = '',
        private string $destination = '',
        private string $baseUrl = '',
        private int $packageId = 0,
    ) {
        $this->storageTree = ['directories' => [], 'files' => []];
        $this->entries = [];
    }

setSource

Source line 31

public function setSource(string $source): 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.

State writes: $this->source.

Implementation, validation guards and interaction order
/**
     * @inheritDoc
     */
    public function setSource(string $source): void
    {
        $this->source = $source;
    }

setDestination

Source line 39

public function setDestination(string $destination): void

Parameters

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

State writes: $this->destination.

Implementation, validation guards and interaction order
/**
     * @inheritDoc
     */
    public function setDestination(string $destination): void
    {
        $this->destination = $destination;
    }

setPackageId

Source line 47

public function setPackageId(int $packageId): void

Parameters

Parameter Type, modifiers and default Purpose
$packageId int $packageId Installed package ID used for metadata/schema/storage ownership.

State writes: $this->packageId.

Implementation, validation guards and interaction order
/**
     * @inheritDoc
     */
    public function setPackageId(int $packageId): void
    {
        $this->packageId = $packageId;
    }

setBaseUrl

Source line 55

public function setBaseUrl(string $baseUrl): void

Parameters

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

State writes: $this->baseUrl.

Implementation, validation guards and interaction order
/**
     * @inheritDoc
     */
    public function setBaseUrl(string $baseUrl): void
    {
        $this->baseUrl = $baseUrl;
    }

buildStorageTree

Source line 63

public function buildStorageTree(): IPackageStorageTreeGenerator

State writes: $this->storageTree.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * @inheritDoc
     */
    public function buildStorageTree(): IPackageStorageTreeGenerator
    {
        $this->storageTree = ['directories' => [], 'files' => []];

        if (is_dir($this->source)) {
            $this->scanDirectory($this->source);
        }

        return $this;
    }

generate

Source line 77

public function generate(): IPackageStorageTreeGenerator

State writes: $this->entries, $this->entries[].

Return expressions (including local callbacks): $this.

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
/**
     * @inheritDoc
     */
    public function generate(): IPackageStorageTreeGenerator
    {
        $this->entries = [];
        if (!$this->fsService->makeDirectory($this->destination)) throw new \RuntimeException('Could not create package storage directory.');

        foreach ($this->storageTree['directories'] as $dir) {
            if (!$this->fsService->makeDirectory($this->destination . DIRECTORY_SEPARATOR . $dir)) throw new \RuntimeException('Could not create package asset directory.');
        }

        foreach ($this->storageTree['files'] as $relPath) {
            $srcPath  = $this->source . DIRECTORY_SEPARATOR . $relPath;
            $destPath = $this->destination . DIRECTORY_SEPARATOR . $relPath;

            $this->replaceAsset($srcPath, $destPath);

            $entry = new StorageEntry();
            $entry->setName(basename($relPath))
                  ->setPath($destPath)
                  ->setUrl($this->baseUrl . '/' . str_replace(DIRECTORY_SEPARATOR, '/', $relPath))
                  ->setSize($this->fsService->fileSize($destPath))
                  ->setLastModified($this->fsService->lastModified($destPath))
                  ->setOwnerType(EOwnerType::PACKAGE)
                  ->setOwnerId($this->packageId);

            $this->entries[] = $entry;
        }

        return $this;
    }

replaceAsset

Source line 107

private function replaceAsset(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.

External effects include copy.

Implementation, validation guards and interaction order
private function replaceAsset(string $source, string $destination): void
    {
        $staged = \vfs\storage\FileName::unique(dirname($destination), $destination);
        if (!$this->fsService->copy($source, $staged)) throw new \RuntimeException("Could not copy package asset: {$source}");
        $backup = null;
        try {
            if ($this->fsService->exists($destination)) {
                $backup = \vfs\storage\FileName::unique(dirname($destination), $destination);
                if (!$this->fsService->move($destination, $backup)) throw new \RuntimeException("Could not back up package asset: {$destination}");
            }
            if (!$this->fsService->move($staged, $destination)) {
                if ($backup !== null && !$this->fsService->move($backup, $destination)) {
                    throw new \RuntimeException("Could not restore package asset; backup remains at {$backup}");
                }
                throw new \RuntimeException("Could not replace package asset: {$destination}");
            }
            if ($backup !== null && !$this->fsService->delete($backup)) throw new \RuntimeException("Could not clean up package asset backup: {$backup}");
        } finally {
            if ($this->fsService->exists($staged) && !$this->fsService->delete($staged)) {
                throw new \RuntimeException("Could not clean up staged package asset: {$staged}");
            }
        }
    }

getStorageTree

Source line 134

public function getStorageTree(): array

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

Implementation, validation guards and interaction order
/**
     * @inheritDoc
     */
    public function getStorageTree(): array
    {
        return $this->storageTree;
    }

registerItems

Source line 142

public function registerItems(): bool

Return expressions (including local callbacks): true.

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
/**
     * @inheritDoc
     */
    public function registerItems(): bool
    {
        $existing = [];
        foreach ($this->repository->fetchByOwnerTypeAndOwner(EOwnerType::PACKAGE, $this->packageId) as $entry) {
            $existing[$entry->getPath()] = $entry;
        }
        foreach ($this->entries as $entry) {
            $previous = $existing[$entry->getPath()] ?? null;
            if ($previous === null) {
                $this->repository->create($entry);
            } else {
                $entry->id = $previous->id;
                $entry->setModelId($previous->id);
                $changed = false;
                foreach (['name', 'path', 'url', 'size', 'middleware', 'lastModified', 'ownerId', 'ownerType'] as $field) {
                    $before = $previous->$field;
                    $after = $entry->$field;
                    if ($before instanceof EOwnerType) $before = $before->value;
                    if ($after instanceof EOwnerType) $after = $after->value;
                    if ($before !== $after) { $changed = true; break; }
                }
                if ($changed && !$this->repository->update($entry)) throw new \RuntimeException('Failed to update package asset registration.');
            }
        }

        return true;
    }

scanDirectory

Source line 170

private function scanDirectory(string $dir): void

Parameters

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

State writes: $this->storageTree['directories'][], $this->storageTree['files'][].

Implementation, validation guards and interaction order
private function scanDirectory(string $dir): void
    {
        $scan = scandir($dir);
        unset($scan[0], $scan[1]);

        foreach ($scan as $item) {
            $fullPath = $dir . DIRECTORY_SEPARATOR . $item;
            $relPath  = substr($fullPath, strlen($this->source) + 1);

            if (is_dir($fullPath)) {
                $this->storageTree['directories'][] = $relPath;
                $this->scanDirectory($fullPath);
            } else {
                $this->storageTree['files'][] = $relPath;
            }
        }
    }

Related types

Clone this wiki locally