Js
Registers public JavaScript resources, collects application files and inline code, and supports context-specific caching and minification.
Purpose
Core\Js manages JavaScript resources for the active APPNETOS runtime context. Public
URLs
can be registered directly, while private application files are resolved and either collected as
inline
code or published as generated cache files.
Resources from multiple applications can be accumulated through the same controller. The controller returns resource data; the layout or rendering templates decide where to output file tags and inline code (for example, in the document head or at the end of the DOM).
Registering resources
add()registers a public or external source directly, with a priority and script type and crossorigin metadata.addFile()detects public URLs and root-relative public paths; local or private paths are delegated toappendFile().appendFile()resolves a local file and processes it through the cache or inline pipeline.append()stores inline code without creating a generated cache file.
The add() method supports type (default text/javascript) and
crossOrigin (default false). Deduplication uses source plus type;
registering
the same combination again replaces its metadata.
Public resources are returned in ascending priority order. The code does not automatically download, bundle or minify directly registered external URLs.
Private application files
Private files are resolved in this order: an existing physical file path,
Core\Files::getJs(), then a path relative to APPNETOS_ROOT. Missing or
unreadable files are logged; unresolved files are not registered.
When caching is disabled, each resolved file is read and appended to the inline collection. With caching enabled, APPNETOS first looks for a valid generated public file and creates one when needed. If generation fails, it falls back to inline code.
Caching and minification
Each private source file is processed independently, not merged into one bundle. Generated resources
are
written to public/appnetos/cache/<context>/js/ and exposed at /appnetos/cache/<context>/js/.
Filenames contain a newly generated random hash; the generic Core\Cache stores the
source-to-public-file mapping under Cache::TYPE_JS.
If the generated file is missing, its cache mapping is removed so it can be generated again. An existing generated file is reused while its cache mapping remains valid; these base classes do not compare source modification times or content hashes before reuse.
When minification is enabled, new generated files and inline code are passed to
Core\Js\Minifier. Previously generated cached files are reused without being minified
again. Directly registered public or external sources are not processed by the minifier.
Example registration
The following PHP illustrates the resource registration API. Replace the example paths with files available in your application.
$js = Registry::get(\Core\Js::class);
$js->add('/src/example/js/vendor.js', priority: -10);
$js->add('https://cdn.example.org/widget.js', type: 'module', crossOrigin: true);
$js->addFile('example/app/js/component.js');
$js->append('console.log("ready");');
$resources = $js->getAll();
Public API Reference
Core\Js
Class & Registry
Class:
Core\Js
Extends:
Core\Js\Base
use Core\Js;
use Core\Registry;
$js = Registry::get(Js::class);
| Method | Behavior |
|---|---|
init(): void |
Resolve Core services and register context-specific static files once. |
add(string $source, int $priority = 0, string $type = 'text/javascript',
bool $crossOrigin = false): void |
Register a public or external resource directly; does not read or minify the URL. |
append(string $script): void |
Collect inline code; minify first when enabled. |
appendFile(string $file, int $priority = 0): void |
Resolve a private file; reuse or generate a public cached file, otherwise append inline. |
addFile(string $file, int $priority = 0): void |
Route public/external sources to add() and local/private files to appendFile(). |
getFiles(): array |
Return deduplicated public resources ordered by ascending priority. |
getInline(): array |
Return collected inline code snippets in insertion order. |
getAll(): array |
Return an array with files and inline collections. |
getAll() returns files and inline. The file entries contain
their
source, priority and script type and crossorigin metadata; inline entries are strings.
Context configuration and output
Context-specific implementations can override getStaticFiles(),
getJsCache()
and getJsMinify(). Static sources are registered during init(). The shared
base implementations default to no generated-file caching and no minification.
APPNETOS collects resources independently of their HTML placement. The application layout is responsible for rendering public file references and inline snippets at the desired positions.