Creating automatic Volt functions


Jan '15

None

0

777

1


RompePC
865
edited Jan '15
Jan '15

I think this'll help for those how are as lazy as me and want a less dirty code in their index.php (or class for configuring Volt). It's very simple. Tested and working on my app (unless I forgotted to write something).

In an INI file ($ it's just a separator, change as you desire):

[myVoltFunctionName]
function             = MyClass::myClassMethod
isAnonymous          = (true | blank)
aditionalArgs       = (var1 | blank) or (var1$var2$var3... | blank)

Now, when configuring Volt:

$voltFunctions  = newPhalcon\Config\Adapter\Ini('voltFunctions.ini')->toArray();
$aditionalArg   = function($arg)
{
    /*
     * This will check if you used a normal string or a constant class.
     */
    $var = '';

    if (stristr($arg, '::'))
    {
        $varData    = explode('::', $arg);
        $var        .= constant($varData[0] . '::' . $varData[1]);
    }
    else
    {
        $var .= $arg;
    }

    return $var;
};

foreach ($voltFunctions as $name => $data)
{
    $function = '';

                    if (strcmp($data['isAnonymous'], '1') === 0)
                    {
                        $aditionalArgs = NULL;

                        if (strcmp($data['aditionalArgs'], '') !== 0)
                        {
                            $aditionalArgs = "'";

                            if (stristr($data['aditionalArgs'], '$'))
                            {
                                $args = explode('$', $data['aditionalArgs']);

                                for ($i = 0; $i < count($args); $i++)
                                {
                                    $aditionalArgs .= (
                                        $aditionalArg($args[$i]) .
                                        "'"
                                    );

                                    if ($i < count($args) - 1)
                                    {
                                        $aditionalArgs .= ", '";
                                    }
                                }
                            }
                            else
                            {
                                $aditionalArgs .= (
                                    $aditionalArg($data['aditionalArgs']) .
                                    "'"
                                );
                            }
                        }

                        if (!is_null($aditionalArgs))
                        {
                            $aditionalArgs .= ", ";
                        }

                        $function = function($args) use ($data, $aditionalArgs)
                        {
                            return (
                                $data['function'] . 
                                '(' .
                                $aditionalArgs .
                                $args .
                                ')'
                            );
                        };
                    }
                    else
                    {
                        $function = $data['function'];
                    }

    $volt->getCompiler()->addFunction($name, $function);
}

RompePC
865

Edited to work with functions that have optional parameters.