final class ResourceLoader
Public API: explicitly declared with @visibility public.

Reads source documents from HTTP(S) URLs or local paths, with pinned snapshots.

Reads are limited to 16 MiB and remembered per URI, failures included. A missing local snapshot is fetched from the source URI, verified against the digest and cached; a document whose digest differs is rejected.

Reading a local document relative to the configuration directorydoctest
$directory = sys_get_temp_dir() . '/requirements-example-' . bin2hex(random_bytes(4));
    mkdir($directory);
    file_put_contents($directory . '/notes.txt', 'Names start with a letter.');
    (new \Requirements\Source\ResourceLoader())->read(new \Requirements\Model\Source('notes', 'notes.txt', 'text', 'lines:1'), $directory, false) // => 'Names start with a letter.'
Rejecting a document whose digest differsdoctest
$directory = sys_get_temp_dir() . '/requirements-example-' . bin2hex(random_bytes(4));
    mkdir($directory);
    file_put_contents($directory . '/notes.txt', 'Names start with a digit.');
    (new \Requirements\Source\ResourceLoader())->read(new \Requirements\Model\Source('notes', 'notes.txt', 'text', 'lines:1', sha256: hash('sha256', 'Names start with a letter.')), $directory, false) // throws \RuntimeException: SHA-256 mismatch

Methods§

public function __construct(private HttpClientInterface|null $client = null)

Parameters

$clientHttpClientInterface|nullThe client for HTTP(S) sources; a default client when null
public function read(Source $source, string $directory, bool $live): string

Reads the document of a source.

Parameters

$sourceSourceThe source declaration
$directorystringThe configuration directory that relative paths resolve against
$liveboolWhether to read the source URI instead of its pinned snapshot

Returns

string The document

Throws

RuntimeException When the document cannot be read, exceeds the limit or its digest differs
Test cases 17
Called from 4
Calls 12
public function fetch(string $uri): string

Reads a resolved URI once and remembers the document or the failure.

Parameters

$uristringAn HTTP(S) URL or an absolute local path

Returns

string The document

Throws

RuntimeException When the resource cannot be read or exceeds 16 MiB
Test cases 8
Called from 1
Calls 6

Private surface 3§

Implementation details, listed for orientation only.

private array<string, string> $cache = []
private array<string, RuntimeException> $failures = []
private HttpClientInterface|null $client = null

Test cases 40§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 22
Other tests reaching this symbol 18

Relations§

Instantiated in 4
Method calls 3
Type declarations 3