We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Yaml config with file cache

Some people asked about loading configurations yaml. There is incubator class that use yaml extension. I wanted use yaml without yaml php extension and wrote this solutions with simple cache:

$di->setShared('fs', 'Symfony\Component\Filesystem\Filesystem');
$di->setShared('yaml_parser', 'Symfony\Component\Yaml\Parser');

$di->setShared('config', function () use ($di, $appPath) {
    $cachePath = $appPath . "/cache/config/config.php";

    if (false === file_exists($cachePath)) {
        $yamlPath = $appPath . "/config/config.yml";
        $yaml = file_get_contents($yamlPath);
        $config = $di['yaml_parser']->parse($yaml);

        $di['fs']->dumpFile($configPath, "<?php" . "\n" . "return " . var_export($config, true) . ";");
    } else {
        $config = include $path . '/cache/config/config.php';
    }

    return new \Phalcon\Config($config);
});

$cachePath should be $configPath Please correct and I remove my comment