Repository navigation
Internals 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.
use installer\interfaces\package\IPackageStorageTreeGenerator;
use vfs\storage\interfaces\IFileSystemService;
use vfs\storage\interfaces\IStorageRepository;
use vfs\storage\StorageEntry;
use vfs\storage\enums\EOwnerType;class PackageStorageTreeGenerator implements IPackageStorageTreeGeneratorDeclarations 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 | 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 |
public function __construct(
private readonly IFileSystemService $fsService,
private readonly IStorageRepository $repository,
private string $source = '',
private string $destination = '',
private string $baseUrl = '',
private int $packageId = 0,
)| 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 = [];
}public function setSource(string $source): void| 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;
}public function setDestination(string $destination): void| 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;
}public function setPackageId(int $packageId): void| 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;
}public function setBaseUrl(string $baseUrl): void| 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;
}public function buildStorageTree(): IPackageStorageTreeGeneratorState 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;
}public function generate(): IPackageStorageTreeGeneratorState 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;
}private function replaceAsset(string $source, string $destination): void| 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}");
}
}
}public function getStorageTree(): arrayReturn expressions (including local callbacks): $this->storageTree.
Implementation, validation guards and interaction order
/**
* @inheritDoc
*/
public function getStorageTree(): array
{
return $this->storageTree;
}public function registerItems(): boolReturn 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;
}private function scanDirectory(string $dir): void| 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;
}
}
}AWT Framework Wiki · Home · Installation Guide · Developer Guide · Framework Internals
Philosophy · Architecture · Known discrepancies · Verification
Installation Guide
Developer Guide
- Learning sequence
- First package
- Package structure
- Lifecycle
- Runtimes
- Events and objects
- Routing and controllers
- Views and resources
- Database and models
- Storage and caching
- Configuration and CLI
- Responses and errors
- Extension points
Framework Internals
- Architecture
- Bootstrap
- Packages
- Runtime
- Events and context
- Objects
- HTTP routing and controllers
- Rendering
- Blade integration
- Database
- Models
- Storage and caching
- Settings
- CLI
- Identity and sessions
- Responses
- Errors
- Complete source inventory
Documentation notes