italystrap / empress-cache
File based cache for the providers configuration aggregated by italystrap/empress
Requires
- php: >=8.2
- italystrap/empress: ^3.0
- webimpress/safe-writer: ^2.2
Requires (Dev)
- brick/varexporter: ^0.5 || ^0.6 || ^0.7
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- infection/infection: ^0.29 || ^0.35
- laminas/laminas-config-aggregator: ^1.9
- phpcompatibility/php-compatibility: ^9.3
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- squizlabs/php_codesniffer: ^3.7
Suggests
- brick/varexporter: Only if the cached configuration contains closures or objects, see VarExporterConfigExporter
Provides
None
Conflicts
None
Replaces
None
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