Adding more flexible support for plugins
Publish PHP Package / docker (push) Successful in 5s Details

This commit is contained in:
Pavel Shevaev 2024-11-20 15:33:49 +03:00
parent 75cd2c8c30
commit a257a2af6d
3 changed files with 36 additions and 16 deletions

View File

@ -162,6 +162,17 @@ function add_twig_support(\Twig\Environment $twig)
return $code;
}
));
$twig->addFunction(new \Twig\TwigFunction('plugins_register_decls',
function() use ($twig)
{
$globals = $twig->getGlobals();
$plugins = $globals['plugins'];
$code = '';
foreach($plugins as $p)
$code .= $p->codegenRegisterDecls($twig);
return $code;
}
));
$twig->addFunction(new \Twig\TwigFunction('plugins_codegen_register',
function() use ($twig)
{
@ -305,22 +316,7 @@ function add_twig_support(\Twig\Environment $twig)
$twig->addFunction(new \Twig\TwigFunction('slice_units',
function(array $units, $max)
{
$pages = paginate(sizeof($units), $max);
$sliced = array();
$units_keys = array_keys($units);
foreach($pages as $idx => $page)
{
$slice = array();
for($i = $page[0];$i<$page[1];++$i)
{
$slice[] = $units[$units_keys[$i]];
}
$sliced[$idx] = $slice;
}
return $sliced;
return slice_units($units, $max);
}
));
$twig->addFunction(new \Twig\TwigFunction('get_modules',
@ -382,6 +378,27 @@ function paginate($total, $step)
return $pages;
}
//slices array like this: [[idx0,[..]], [idx1,[..]], ...]
function slice_units(array $units, $max)
{
$pages = paginate(sizeof($units), $max);
$sliced = array();
$units_keys = array_keys($units);
foreach($pages as $idx => $page)
{
$slice = array();
for($i = $page[0];$i<$page[1];++$i)
{
$slice[] = $units[$units_keys[$i]];
}
$sliced[$idx] = $slice;
}
return $sliced;
}
function ns_last($name)
{
$items = explode('.', $name);

View File

@ -6,5 +6,6 @@ interface BindPlugin
{
function init(\mtgMetaInfo $meta, \Twig\Environment $twig);
function codegenDecls(\Twig\Environment $twig);
function codegenRegisterDecls(\Twig\Environment $twig);
function codegenRegister(\Twig\Environment $twig);
}

View File

@ -38,6 +38,8 @@ static public class {{register_class}} {
static public Module Module{{mv}};
{%- endfor ~%}
{{ plugins_register_decls() }}
public static void Register(Types types)
{
RegisterBegin(types);