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.
composer require italystrap/empress-cacheuse 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.
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.
Export and write failures throw ConfigCannotBeCachedException, the original error is available with
getPrevious().
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/varexporteruse ItalyStrap\Empress\Cache\ProvidersCache;
use ItalyStrap\Empress\Cache\VarExporterConfigExporter;
$cache = new ProvidersCache($file, 0666, true, new VarExporterConfigExporter());MIT