Repository navigation
Internals 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.
use database\DatabaseManager;
use model\exceptions\ModelCRUDException;
use vfs\storage\interfaces\IStorageRepository;
use vfs\storage\enums\EOwnerType;class StorageRepository implements IStorageRepositorySource 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.
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 | 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 |
public function __construct(?DatabaseManager $database = null)| 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();
}public function fetchAll(): arrayReturn 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);
}public function fetchById(int $id): StorageEntry| 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];
}public function fetchByOwner(int $ownerId): array| 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);
}public function fetchByName(string $name): array| 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);
}public function fetchByOwnerType(EOwnerType $ownerType): array| 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);
}public function fetchByOwnerTypeAndOwner(EOwnerType $ownerType, int $ownerId): array| 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);
}public function fetchByOwnerAndName(int $ownerId, string $name): ?StorageEntry| 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];
}public function create(StorageEntry $entry): StorageEntry| 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;
}
}public function update(StorageEntry $entry): bool| 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));
}public function delete(StorageEntry $entry): bool| 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();
}private function data(StorageEntry $entry): array| 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,
];
}private function hydrateCollection(array $rows): array| 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
);
}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