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; } )); $twig->addFunction(new \Twig\TwigFunction('get_all_fields', function($meta, $o) { return \mtg_get_all_fields($meta, $o); } )); $twig->addFunction(new \Twig\TwigFunction('count_optional', function($units) { $opts = 0; foreach($units as $u) if($u->hasToken('optional')) ++$opts; return $opts; } )); $twig->addFilter(new \Twig\TwigFilter('go_type', function($type, $tokens) { return go_type($type, $tokens); } )); $twig->addFilter(new \Twig\TwigFilter('ucfirst', function($str) { return ucfirst($str); } )); } function go_type(\mtgType $type, array $tokens = array()) { if($type instanceof \mtgArrType) { $vtype = $type->getValue(); $native = go_type($vtype); $str = "[]"; if(array_key_exists("virtual", $tokens)) $str .= "I"; else $str .= $vtype instanceof mtgMetaStruct ? "*" : ""; $str .= $native; return $str; } else if($type instanceof \mtgBuiltinType) { if($type->isFloat()) return "float32"; else if($type->isDouble()) return "float64"; else if($type->isBlob()) return "[]byte"; else return $type->getName(); } else if($type instanceof \mtgMetaEnum) return $type->getName(); else if($type instanceof \mtgMetaStruct) { if(array_key_exists("virtual", $tokens)) return "I{$type->getName()}"; else return $type->getName(); } else throw new Exception("Unknown type '$type'"); }