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

Adding events before and after Volt compilation /

Hello,

I would like to encrypt each compiled Volt php files and then decrypt when PHP read it.

So I'm asking, is there any way to add events before reading files in cache and replace writing behavior just like "extensions" ? ( https://docs.phalcon.io/fr/latest/reference/volt.html#extensions )

Thanks !



1.2k
edited May '14

Looks like there is no way to handle this kind of events: https://github.com/phalcon/cphalcon/blob/master/ext/mvc/view/engine/volt/compiler.c#L3528

Phalcon put directly compiled files in the cache directory :/



1.2k
edited Jun '14

Suggestion: The code should be in this:

class PhpFunctionExtension
{
    /**
     * This method is called when Volt want to read cached file
     */
    public function readCompiledFileBehavior($filename, $content)
    {
        // Read & decrypt file
        return true;
    }

    /**
     * This method is called when Volt want to write a cached file
     */
    public function writeCompiledFileBehavior($filename, $content)
    {
        // Encrypt and write file
        return true;
    }
}

// ...

$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$compiler = $volt->getCompiler();

//Register the extension in the compiler
$compiler->addExtension(new PhpFunctionExtension());
edited May '14

Hi,

I have a small function to compile volt templates in app/views_compiled directory (I use gettext, I need php template to generate *.pot file). Here is function:

<?php
$view = null;
$volt = new \Phalcon\Mvc\View\Engine\Volt($view);

$compiler = $volt->getCompiler();
$compiler->addFunction('_', '_');
$compiler->addFunction('md5', 'md5');
$compiler->addFunction('format', 'sprintf');

$_path = realpath(__DIR__ . '/../views/');

function compile_directory($source, $destination) {
    global $compiler;
    if (is_dir($source)) {
        @mkdir($destination);
        $directory = dir($source);
        while ( FALSE !== ( $readdirectory = $directory->read() ) ) {
            if ( $readdirectory == '.' || $readdirectory == '..' || $readdirectory == '.DS_Store') {
                continue;
            }
            $PathDir = $source . '/' . $readdirectory; 
            if (is_dir($PathDir)) {
                compile_directory($PathDir, $destination . '/' . $readdirectory);
                continue;
            }

            $compiled = $compiler->compileString(file_get_contents($PathDir));
            file_put_contents($destination . '/' . str_replace('.volt', '.volt.php', $readdirectory), $compiled);
        }
        $directory->close();
    } else {
        $compiled = $compiler->compileString(file_get_contens($source));
        file_put_contents(str_replace('.volt', '.volt.php', $destination), $compiled);
    }
}

compile_directory($_path, __DIR__ . '/compiled/');


1.2k

Not bad ! But I'm looking for something transparent while using render(), I maybe have something, I will keep the topic updated.

You could write a new class that extended \Phalcon\Mvc\View\Engine\Volt, then simply specify that new class as the engine that powers the view.



1.2k

This is exactly what I am doing. It is a bit hard because I have to rewrite the same behavior for the render and compile method but when it will be finished I will share the class here :)

Any updates here ?