Skip to content

Internals Vfs Resource Resource

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

vfs\resource\Resource

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/vfs/resource/Resource.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 vfs\resource\event\ContextRequestEvent;
use vfs\resource\exceptions\ResourceException;

Resource

Declaration and architectural role

class Resource

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
$context public string $context; Used by __construct(), parseAlias().
$map private array $map = []; Used by buildResourceMap(), getResourceMap(), get().
$path private string $path; Used by __construct(), buildResourceMap(), get().
Constructor-promoted public function __construct(string $context = '' Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct(string $context = '', string $path = PACKAGES)
buildResourceMap public function buildResourceMap(): self
getResourceMap public function getResourceMap(): array
get public function get(string $alias, bool $must = false): ?string
findInArray private function findInArray(array $array, string $name): ?string
parseAlias private function parseAlias(string $alias): ?array

__construct

Source line 14

public function __construct(string $context = '', string $path = PACKAGES)

Parameters

Parameter Type, modifiers and default Purpose
$context string $context = '' Package context object or package name according to the declared type.
$path string $path = PACKAGES Filesystem or route path as defined by the owning component; no universal path normalization is implied.

State writes: $this->context, $this->path.

Explicit exceptions: ResourceException. 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 __construct(string $context = '', string $path = PACKAGES)
    {
        if ($context === '') {
            global $eventDispatcher;
            if (!isset($eventDispatcher)) throw new ResourceException('No resource context dispatcher is available.');
            $event = new ContextRequestEvent();
            $eventDispatcher->dispatch($event);
            $context = $event->context ?? '';
            if ($context === '') throw new ResourceException('No resource context was supplied.');
        }
        $this->context = $context;
        $this->path = rtrim($path, DIRECTORY_SEPARATOR);
    }

buildResourceMap

Source line 28

public function buildResourceMap(): self

State writes: $this->map, $this->map[$package].

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function buildResourceMap(): self
    {
        $this->map = [];
        foreach (ResourceBuilder::directoryIterator($this->path) as $package) {
            $directory = $this->path . DIRECTORY_SEPARATOR . $package;
            if (!is_dir($directory)) continue;
            $map = [$package => (new ResourceBuilder($directory))->build()];
            $this->map[$package] = $map[$package];
            ResourceCache::cache($package, [$directory], $map, $this->path);
        }
        return $this;
    }

getResourceMap

Source line 41

public function getResourceMap(): array

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

Implementation, validation guards and interaction order
public function getResourceMap(): array { return $this->map; }

get

Source line 44

public function get(string $alias, bool $must = false): ?string

Parameters

Parameter Type, modifiers and default Purpose
$alias string $alias Resource alias or route metadata alias according to this component.
$must bool $must = false When true, a missing required resource is raised as a failure instead of a nullable result.

Resolve filename, relative/path, or Package:relative/path. Required resources throw when missing.

State writes: $this->map.

Return expressions (including local callbacks): $found.

Explicit exceptions: ResourceException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
/** Resolve filename, relative/path, or Package:relative/path. Required resources throw when missing. */
    public function get(string $alias, bool $must = false): ?string
    {
        $parts = $this->parseAlias($alias);
        $found = null;
        if ($parts !== null) {
            [$package, $relative] = $parts;
            $cached = ResourceCache::get($package, $this->path);
            if (is_array($cached)) $this->map = $cached;
            else $this->buildResourceMap();
            $map = $this->map[$package] ?? [];
            if (!str_contains($relative, '/')) $found = $this->findInArray($map, $relative);
            else {
                $node = $map;
                foreach (explode('/', $relative) as $segment) {
                    if (!is_array($node) || !array_key_exists($segment, $node)) { $node = null; break; }
                    $node = $node[$segment];
                }
                if (is_string($node)) $found = $node;
            }
            if ($found !== null && !is_file($found)) $found = null;
        }
        if ($found === null && $must) throw new ResourceException("Resource not found: {$alias}");
        return $found;
    }

findInArray

Source line 69

private function findInArray(array $array, string $name): ?string

Parameters

Parameter Type, modifiers and default Purpose
$array array $array Operation input; the exact use, validation and propagation are shown in the implementation trace.
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.

Return expressions (including local callbacks): $array[$name]; $found; null.

Implementation, validation guards and interaction order
private function findInArray(array $array, string $name): ?string
    {
        if (isset($array[$name]) && is_string($array[$name])) return $array[$name];
        foreach ($array as $item) {
            if (is_array($item)) {
                $found = $this->findInArray($item, $name);
                if ($found !== null) return $found;
            }
        }
        return null;
    }

parseAlias

Source line 81

private function parseAlias(string $alias): ?array

Parameters

Parameter Type, modifiers and default Purpose
$alias string $alias Resource alias or route metadata alias according to this component.

Return expressions (including local callbacks): null; [$package, $alias].

Implementation, validation guards and interaction order
private function parseAlias(string $alias): ?array
    {
        $package = $this->context;
        if (str_contains($alias, ':')) {
            [$package, $alias] = explode(':', $alias, 2);
            $alias = ltrim($alias, '/');
        }
        if ($package === '' || str_contains($package, '/') || str_contains($package, '\\')
            || $package === '.' || $package === '..' || str_contains($package, "\0")) return null;
        if ($alias === '' || str_contains($alias, '\\') || str_contains($alias, "\0")) return null;
        foreach (explode('/', $alias) as $segment) {
            if ($segment === '' || $segment === '.' || $segment === '..') return null;
        }
        return [$package, $alias];
    }

Related types

Clone this wiki locally