gCore

ResourceManager Documentation

Overview

ResourceManager provides resource management including asset bundling, template management, and resource optimization. It uses gNode's native asset bundling (batched in a single round-trip) and template fragment management.

Namespace: gCore\Modules\Managers\Base\ResourceManager Implements: ModuleInterface Pattern: Singleton (accessed via gCore::getService())

Architecture

ResourceManager operates in two modes:

  1. Enhanced Mode (gNode): Full asset bundling, template management, batch operations
  2. Default Tier Mode: Basic caching and optimization (limited features)

Key Features

Initialization

// Get ResourceManager instance via gCore
$resources = gCore::getService('ResourceManager');

// Configuration (passed during gCore initialization)
$config = [
    'site_id' => 'my_site',
    'node_id' => 'node1',
    'use_gnode' => true,
    'gnode_client' => $gNodeClient,
    'cache_enabled' => true,
    'optimization_enabled' => true,
    'default_bundle_type' => 'mixed',
    'default_minify' => true,
    'default_ttl' => 3600,
    'max_bundle_size' => 1048576,  // 1MB
    'debug' => false
];

Public API Reference

Singleton & Lifecycle

getInstance(): ModuleInterface

Returns the singleton instance of ResourceManager.

initialize(array $config = []): void

Initializes the resource system with configuration.

isInitialized(): bool

Check if manager is initialized.

getStatus(): array

Get full status including mode, statistics, and registry counts.

[
    'initialized' => true,
    'site_id' => 'my_site',
    'node_id' => 'node1',
    'mode' => 'full',           // full | stub
    'storage_type' => 'gnode',
    'storage' => 'available',
    'gnode_enabled' => true,
    'cache_enabled' => true,
    'optimization_enabled' => true,
    'statistics' => [...],
    'capabilities' => [...],
    'registry_counts' => [
        'assets' => 0,
        'templates' => 0,
        'bundles' => 0
    ]
]

getConfig(): array

Get current configuration.

updateConfig(array $config): void

Update configuration at runtime.

getCapabilityVector(): array

Get capability vector for gNode registration.

getStatistics(): array

Get resource statistics.

Asset Management

createAssetBundle(string $bundleId, array $assets, string $bundleType = 'mixed', bool $minify = true, ?int $ttl = null): array

Create an asset bundle using native gNode assetBundle method.

$bundle = $resources->createAssetBundle('main-css', [
    'style.css' => $cssContent,
    'theme.css' => $themeContent,
    'custom.css' => $customContent
], 'css', true, 3600);

// Returns:
// [
//     'success' => true,
//     'bundle_id' => 'main-css',
//     'bundled_size' => 15234,
//     'original_size' => 45678,
//     'compression_ratio' => 0.67
// ]

loadAsset(string $assetId, bool $useCache = true): ?array

Load an asset by identifier.

batchLoadAssets(array $assetIds): array

Batch load multiple assets using gNode executeBatch.

$assets = $resources->batchLoadAssets(['style.css', 'script.js', 'theme.css']);

optimizeAsset(string $content, string $type, array $options = []): string

Optimize an asset (minify, compress).

invalidateAsset(string $assetId): bool

Invalidate an asset from cache.

Template Management

storeTemplateFragment(string $templateId, string $content, array $dependencies = [], array $variables = [], ?int $ttl = null): array

Store template fragment using native gNode templateFragment method.

$result = $resources->storeTemplateFragment('header', $headerContent,
    ['logo', 'nav'],           // dependencies
    ['site_name', 'menu_items'] // variables
);

discoverTemplatesByCapability(array $capabilities, int $limit = 10): array

Discover templates by capability using gNode geometric discovery.

$templates = $resources->discoverTemplatesByCapability([
    'e_commerce' => 0.8,
    'responsive' => 0.9
]);

renderTemplateString(string $template, array $variables = [], array $config = []): string

Render template string with variables using gNode.

$rendered = $resources->renderTemplateString(
    'Hello {{name}}, your order #{{order_id}} is ready!',
    ['name' => 'John', 'order_id' => '12345']
);

getAllTemplateMetadata(array $config = []): array

Get all template metadata from gNode.

getTemplateStatistics(): array

Get template statistics from gNode.

cacheTemplate(string $templateId, string $rendered, ?int $ttl = null): bool

Cache a rendered template.

invalidateTemplate(string $templateId): bool

Invalidate a template from cache.

Resource Loading

loadResource(string $url, string $type = 'auto'): ?array

Load a resource by URL or identifier.

preloadResources(array $resources): array

Preload resources for performance.

setupLazyLoad(string $url, string $trigger = 'viewport'): array

Setup lazy-loading for a resource.

getBundleManifest(string $bundleId): ?array

Get bundle manifest.

warmupCache(array $resources): int

Warmup cache with resources.

Performance Optimization

minifyAsset(string $content, string $type): string

Minify asset content.

compressBundle(array $bundle): array

Compress bundle data.

generateSourceMap(string $bundleId): ?array

Generate source map for bundle.

analyzeBundle(string $bundleId): array

Analyze bundle performance.

$analysis = $resources->analyzeBundle('main-css');
// [
//     'bundle_id' => 'main-css',
//     'type' => 'css',
//     'minified' => true,
//     'created_at' => 1704672000,
//     'size_estimate' => 15234,
//     'compression_ratio' => 0.67
// ]

Usage Examples

Asset Bundling

$resources = gCore::getService('ResourceManager');

// Bundle CSS files
$cssBundle = $resources->createAssetBundle('site-styles', [
    'reset' => file_get_contents('css/reset.css'),
    'theme' => file_get_contents('css/theme.css'),
    'custom' => file_get_contents('css/custom.css')
], 'css', true);

// Bundle JS files
$jsBundle = $resources->createAssetBundle('site-scripts', [
    'vendor' => file_get_contents('js/vendor.js'),
    'app' => file_get_contents('js/app.js')
], 'js', true);

echo "CSS compressed: " . round($cssBundle['compression_ratio'] * 100) . "%\n";
echo "JS compressed: " . round($jsBundle['compression_ratio'] * 100) . "%\n";

Template Management

// Store a reusable template
$resources->storeTemplateFragment('product-card', '
<div class="product">
    <img src="https://raw.githubusercontent.com/geodineum/gCore/main/image_url" alt="{{name}}">
    <h3>{{name}}</h3>
    <p class="price">{{price}}</p>
    <button data-id="{{id}}">Add to Cart</button>
</div>
', [], ['image_url', 'name', 'price', 'id']);

// Render template with data
$rendered = $resources->renderTemplateString(
    file_get_contents('templates/product-card.tpl'),
    [
        'image_url' => '/images/product.jpg',
        'name' => 'Widget',
        'price' => '$29.99',
        'id' => '12345'
    ]
);

Batch Operations

// Batch load assets (single round-trip)
$assets = $resources->batchLoadAssets([
    'header.css',
    'main.css',
    'footer.css',
    'sidebar.css'
]);

foreach ($assets as $assetId => $data) {
    if ($data !== null) {
        echo "Loaded: $assetId\n";
    }
}

Resource Type Detection

$type = $resources->loadResource('https://example.com/styles/main.css');
// Auto-detected as 'stylesheet'

$type = $resources->loadResource('https://example.com/scripts/app.js');
// Auto-detected as 'script'

$type = $resources->loadResource('https://example.com/images/hero.jpg');
// Auto-detected as 'image'

Capability Vector

[
    'resource_loading' => 1.0,
    'asset_bundling' => 0.95,
    'template_management' => 0.9,
    'optimization' => 0.8,
    'caching' => 0.85
]

Resource Type Mapping

Extension Type
css stylesheet
js script
jpg, jpeg, png, gif, svg image
woff, woff2, ttf, eot font

Statistics Tracked

[
    'assets_loaded' => 0,
    'templates_rendered' => 0,
    'bundles_created' => 0,
    'cache_hits' => 0,
    'cache_misses' => 0
]

Last Updated: January 2026