Andrii Sukhoi
Andrii Sukhoi

Reputation: 881

Write compiled symfony service container to file

I want to put my compiled container to file. So as symfony docs says it can be done on a various ways. The docs have this code example:

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;

$file = __DIR__ .'/cache/container.php';

if (file_exists($file)) {
    require_once $file;
    $container = new ProjectServiceContainer();
} else {
    $container = new ContainerBuilder();
    // ...
    $container->compile();

    $dumper = new PhpDumper($container);
    file_put_contents($file, $dumper->dump());
}

Anyway docs also say that file_put_contents is not atomic and it's better to use symfony's built-in functions:

Citate from documentation

The file_put_contents() function is not atomic. That could cause issues in a production environment with multiple concurrent requests. Instead, use the dumpFile() method from Symfony Filesystem component or other methods provided by Symfony (e.g. $containerConfigCache->write()) which are atomic.

My question is how may I use $containerConfigCache->write() ? I can't find any mentions about it anyware

Upvotes: 1

Views: 76

Answers (1)

Andrii Sukhoi
Andrii Sukhoi

Reputation: 881

ConfigCache is part of Config component in symfony. Generally you may use it that way:

use Symfony\Component\Config\ConfigCache;
...
$configCache = new ConfigCache($file, false);
$configCache->write((new PhpDumper($container))->dump(['class' => 'MyCachedContainer']));

Upvotes: 0

Related Questions