Repository navigation
Developer Guide Storage and Caching
Developer Guide · Resources · Database
Internals: Storage, Storage facade, StorageManager, CachePool, TransientStorage.
Storage manages files with database metadata and owner IDs; Resource resolves files in package source trees; transient storage manages cache/temp files without persistent awt_storage records. Choose the mechanism based on ownership and lifetime.
Excerpt in an initialized multipart POST controller action, with an actual uploaded attachment file and known package context:
$entry = (new \vfs\storage\Storage())->upload(
$_FILES['attachment'],
\vfs\storage\enums\EOwnerType::PACKAGE,
ownerId: (int) $this->getContext()->contextId,
ownerName: $this->packageName
);
return \response\Response::make(201)->data([
'id' => $entry->getId(), 'url' => $entry->getUrl(),
])->asJson();The upload must have name/tmp_name/error/size, UPLOAD_ERR_OK and a genuine PHP uploaded temporary file. Storage stages it, builds a StorageEntry, invokes the owner strategy and persists metadata. Exceptions attempt staging cleanup. Do not pass ordinary local paths as uploaded files; use manager registration with a StorageEntry for trusted local files.
StorageEntry exposes name, path, URL, size, modification time, owner type/ID and middleware through getters/setters. Fetch by ID or by owner/name through StorageRepository. getUrl() produces the framework's /storage/<id>/<encoded-name> route; its name component is cosmetic, while the ID selects current metadata. The default owner strategy supports PACKAGE. USER and SYSTEM enum cases exist but require your own registered strategy.
normaliseMultiFileArray() converts PHP's multi-file form layout to a list; pass that list to uploadMultiple(). It stops on the first failure without rolling back already completed uploads. No built-in MIME/type/quota policy is applied by Storage; validate those application requirements before registration.
StorageManager provides move(), rename(), copy(), delete(), get(), registerLocal() and registerStrategy(). File/record operations attempt restoration on metadata failure, but are not a full filesystem transaction. Supply IStorageManager/IFileSystemService or a custom basePath to Storage when replacing behavior. Review default package strategy paths and URL behavior before using a custom storage root.
A registered middleware class name is instantiated without arguments and must implement IMiddleware. StorageAccess calls it before the /storage route or Storage::download() serves a file. An explicit false return denies; thrown failures propagate. No middleware means access is allowed. Missing or invalid middleware raises a resolution failure rather than silently serving the file.
Storage::download(id, disposition) emits download headers, streams and exits; it does not return a Response. Only attachment/inline dispositions are honored. Package resource URLs use PublicResource's allowlist instead and do not inherit storage-entry middleware. Keep protected files outside public resource directories and require authorization on their registered storage access.
Excerpt in an initialized package service:
$temp = (new \vfs\transient\TransientStorage())
->setPool('temp')->setSubPool('DemoSite');
$file = $temp->createFile('receipt', \vfs\transient\enums\ETransientType::JSON, ['id' => 42]);
$file->loadContent();
$receipt = json_decode($file->content, true, 512, JSON_THROW_ON_ERROR);createFile() appends the selected extension. getFile() needs the full filename (receipt.json) and returns null when absent. Only cache and temp pools exist. setSubPool() and filenames must be relative without traversal/backslashes/null bytes; create nested subpools before writing into them. Use getFiles(), deleteFile(), renameFile(), moveFile() and copyFile() as needed; moves/copies return a new entry so later operations use its new path.
TransientStorageEntry refreshes metadata, supports loadContent()/write()/delete(), and stringifies to its path. Text/JSON files load as strings; PHP/CACHE files are included and must be trusted generated data. Array writes to JSON encode JSON, while other array writes generate a PHP return file. Writes use an exclusive random sibling temporary file, flush and rename, with opcache invalidation; readers avoid partially written content. Do not allow untrusted actors to replace executable cache files.
Excerpt in an initialized package service:
$pool = \vfs\cache\Cache::pool('DemoSite')
->createConfig(\vfs\cache\enums\ECacheValidation::EXPIRE, []);
$data = $pool->getCache('dashboard');
if ($data === false) {
$data = ['messages' => 12];
$pool->setCache('dashboard', $data);
}Cache values are arrays. A miss is false, distinct from cached []. Cache::get/set/delete are pool/key shortcuts. setCache() returns a CacheEntry with name, content and backing file/metadata. Pool names map to transient subdirectories; cache keys are hashed to filenames, not literal paths.
| Validation | Current behavior |
|---|---|
| NONE | Never expires merely by time. |
| EXPIRE | Entry age must be less than 3600 seconds. |
| EXPIRE_LONGER | Entry age must be less than 86400 seconds. |
| MODIFIED | Recursive file/directory snapshot with mtime/ctime/size and link metadata. |
| HASH | Snapshot includes file SHA-256 hashes. |
For filesystem watching, supply absolute watched paths to createConfig(). Entry payloads preserve their own roots/mode; a later pool configuration change does not retroactively change old entries. Directory additions/removals and missing paths affect snapshots. Corrupt/obsolete/invalid entries are deleted and treated as misses. There is no custom per-entry TTL parameter, background eviction worker or AWT cache-clear command; delete a specific key or manage the generated cache files intentionally.
DatabaseCache is a separate collaborator indexed by table, query and binding-derived key; use DatabaseManager's mutation APIs to trigger table invalidation. See Database for transaction-aware invalidation.
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