/**
* Loads a container and returns it.
*
* If the cache file for the service container exists and is current, it
* will be loaded and returned. Otherwise, a new container will be built
* using the configuration file and the provided optional builder. The
* builder will be used to make changes to the service container before
* it is compiled and cached.
*
* It may be important to note that debug mode for the `ConfigCache` class
* is enabled by default. This will ensure that cached configuration files
* are updated whenever they are changed.
*
* @param string $containerCacheFilePath The container cache file path.
* @param callable $containerBuilderCallable The new container builder callable.
* @param string $compiledContainerClassName The compiled container class name.
* @param boolean $debug Is debugging mode enabled?
*
* @return Jarvis The loaded application.
*/
public static function create($containerCacheFilePath, callable $containerBuilderCallable = null, $compiledContainerClassName = 'AppCachedContainer', $debug = true)
{
$cacheManager = new ConfigCache($containerCacheFilePath, $debug);
if (!$cacheManager->isFresh()) {
$container = static::createContainer();
if (null !== $containerBuilderCallable) {
$containerBuilderCallable($container);
}
if ($debug) {
$filename = pathinfo($containerCacheFilePath, PATHINFO_DIRNAME) . '/' . pathinfo($containerCacheFilePath, PATHINFO_FILENAME) . '.xml';
$container->setParameter('debug.container.dump', $filename);
}
$container->compile();
$dumper = new PhpDumper($container);
$cacheManager->write($dumper->dump(array('class' => $compiledContainerClassName)), $container->getResources());
if ($debug) {
$filename = $container->getParameter('debug.container.dump');
$dumper = new XmlDumper($container);
$filesystem = new Filesystem();
$filesystem->dumpFile($filename, $dumper->dump(), null);
try {
$filesystem->chmod($filename, 0666, umask());
} catch (IOException $e) {
// discard chmod failure (some filesystem may not support it)
}
}
}
if (!class_exists($compiledContainerClassName)) {
/** @noinspection PhpIncludeInspection */
require $containerCacheFilePath;
}
return new Jarvis(new $compiledContainerClassName());
}