Search by

italystrap / empress-cache

overclokk

File based cache for the providers configuration aggregated by italystrap/empress

Package info

github.com/ItalyStrap/empress-cache

pkg:composer/italystrap/empress-cache

Statistics

Installs: 12

Dependents: 0

Suggesters: 1

Stars: 0

Open Issues: 0

1.0.0 2026-11-21 09:10 UTC

This package is auto-updated.

Last update: 2026-11-21 09:11:08 UTC


README

File based cache for the providers configuration aggregated by italystrap/empress.

Empress does not cache anything by default (NullProvidersCache). Install this package when you want to write the aggregated configuration to a PHP file and skip the providers on the next requests.

Installation

composer require italystrap/empress-cache

Usage

use Auryn\Injector;
use ItalyStrap\Config\Config;
use ItalyStrap\Empress\Cache\ProvidersCache;
use ItalyStrap\Empress\ProvidersCollection;

$cache = new ProvidersCache(
    __DIR__ . '/var/cache/providers.php', // Cache file
    0666,                                  // File mode
    true                                   // Enabled
);

$config = new Config();
$collection = new ProvidersCollection(new Injector(), $config, $cache, $providers);
$collection->aggregate();

The cache must be passed to ProvidersCollection before calling aggregate(). Delete the cache file to invalidate it.

Disable writing from the configuration

When the cache is enabled in the constructor, the aggregated configuration can still skip writing it, for example from a local configuration file used only in development:

// config/autoload/development.local.php
use ItalyStrap\Empress\Cache\ProvidersCache;

return [
    ProvidersCache::ENABLE_CACHE => false,
];

Only false disables writing, the key cannot enable a cache disabled in the constructor. An existing cache file is still read, delete it after changing the configuration.

Errors

Export and write failures throw ConfigCannotBeCachedException, the original error is available with getPrevious().

Exporters

The configuration is written with a ConfigExporterInterface, passed as the fourth constructor argument.

Exporter Dependencies Supported values
NativeConfigExporter (default) none, uses var_export() scalars, null, arrays
VarExporterConfigExporter brick/varexporter the above plus closures and objects

With the default exporter, a configuration that contains a closure or an object throws a ConfigCannotBeCachedException with the type and the path of the value. Use class strings or invokable factory classes instead of closures:

// Not cacheable with NativeConfigExporter
AurynConfig::DELEGATIONS => [
    Logger::class => static fn(): Logger => new Logger('app'),
],

// Cacheable
AurynConfig::DELEGATIONS => [
    Logger::class => LoggerFactory::class, // invokable class
],

If you need closures, install brick/varexporter (it pulls nikic/php-parser) and use its exporter:

composer require brick/varexporter
use ItalyStrap\Empress\Cache\ProvidersCache;
use ItalyStrap\Empress\Cache\VarExporterConfigExporter;

$cache = new ProvidersCache($file, 0666, true, new VarExporterConfigExporter());

Credits

License

MIT