84 lines
1.6 KiB
PHP
84 lines
1.6 KiB
PHP
|
<?php
|
||
|
namespace metagen_go;
|
||
|
use Exception;
|
||
|
|
||
|
function get_twig(array $inc_path = [])
|
||
|
{
|
||
|
array_unshift($inc_path, __DIR__ . "/../tpl/");
|
||
|
$loader = new \Twig\Loader\FilesystemLoader($inc_path);
|
||
|
|
||
|
$twig = new \Twig\Environment($loader, [
|
||
|
'debug' => true,
|
||
|
'autoescape' => false,
|
||
|
'strict_variables' => true]
|
||
|
);
|
||
|
$twig->addExtension(new \Twig\Extension\DebugExtension());
|
||
|
|
||
|
_add_twig_support($twig);
|
||
|
|
||
|
return $twig;
|
||
|
}
|
||
|
|
||
|
function supported_tokens()
|
||
|
{
|
||
|
return [
|
||
|
'POD',
|
||
|
'default',
|
||
|
'optional',
|
||
|
'bitfields',
|
||
|
'cloneable',
|
||
|
'virtual',
|
||
|
|
||
|
'table',
|
||
|
'id',
|
||
|
'owner',
|
||
|
'pkey',
|
||
|
|
||
|
'statist',
|
||
|
'statist_skip',
|
||
|
];
|
||
|
}
|
||
|
|
||
|
function _add_twig_support(\Twig\Environment $twig)
|
||
|
{
|
||
|
$twig->addTest(new \Twig\TwigTest('instanceof',
|
||
|
function($obj, $class)
|
||
|
{
|
||
|
return (new \ReflectionClass($class))->isInstance($obj);
|
||
|
}
|
||
|
));
|
||
|
$twig->addFunction(new \Twig\TwigFunction('Error',
|
||
|
function($e)
|
||
|
{
|
||
|
throw new Exception($e);
|
||
|
}
|
||
|
));
|
||
|
$twig->addFunction(new \Twig\TwigFunction('has_token',
|
||
|
function($o, $token)
|
||
|
{
|
||
|
return $o->hasToken($token);
|
||
|
}
|
||
|
));
|
||
|
$twig->addFunction(new \Twig\TwigFunction('has_token_in_parent',
|
||
|
function($o, $token)
|
||
|
{
|
||
|
return $o->hasTokenInParent($token);
|
||
|
}
|
||
|
));
|
||
|
$twig->addFunction(new \Twig\TwigFunction('token',
|
||
|
function($o, $name)
|
||
|
{
|
||
|
return $o->getToken($name);
|
||
|
}
|
||
|
));
|
||
|
$twig->addFunction(new \Twig\TwigFunction('token_or',
|
||
|
function($o, $name, $v)
|
||
|
{
|
||
|
if($o->hasToken($name))
|
||
|
return $o->getToken($name);
|
||
|
else
|
||
|
return $v;
|
||
|
}
|
||
|
));
|
||
|
}
|