From a257a2af6d84db8ba4a3fb20d7cf3d5804ec6893 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Wed, 20 Nov 2024 15:33:49 +0300 Subject: [PATCH] Adding more flexible support for plugins --- src/bind.inc.php | 49 ++++++++++++++++++++++++++------------- src/plugin.inc.php | 1 + tpl/codegen_autobind.twig | 2 ++ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/bind.inc.php b/src/bind.inc.php index 0b165fc..283d4f9 100644 --- a/src/bind.inc.php +++ b/src/bind.inc.php @@ -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); diff --git a/src/plugin.inc.php b/src/plugin.inc.php index 9aa17d6..489c73e 100644 --- a/src/plugin.inc.php +++ b/src/plugin.inc.php @@ -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); } diff --git a/tpl/codegen_autobind.twig b/tpl/codegen_autobind.twig index b54659b..9c400cb 100644 --- a/tpl/codegen_autobind.twig +++ b/tpl/codegen_autobind.twig @@ -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);