Skip to content

Commit

Permalink
Merge pull request #201 from thephpleague/dx
Browse files Browse the repository at this point in the history
Added helpful funcs for DX #194
  • Loading branch information
ragboyjr committed Jan 15, 2018
2 parents bdb4c5f + 9fd98b7 commit bce7243
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Extension/LayoutSections/LayoutSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ public function register(Plates\Engine $plates) {
$factories[] = LayoutRenderTemplate::factory();
return $factories;
});
$c->wrap('renderContext.func.funcs', function($funcs, $c) {
$plates->addFuncs(function($c) {
$template_args = RenderContext\assertTemplateArgsFunc();
$one_arg = RenderContext\assertArgsFunc(1);

return array_merge($funcs, [
return [
'layout' => [$template_args, layoutFunc()],
'section' => [$one_arg, sectionFunc()],
'start' => [$one_arg, startFunc()],
'push' => [$one_arg, startFunc(START_APPEND)],
'unshift' => [$one_arg, startFunc(START_PREPEND)],
]);
];
});
}
}
25 changes: 24 additions & 1 deletion src/Extension/RenderContext/RenderContextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function register(Plates\Engine $plates) {
? escapeFunc($config['escape_flags'], $config['escape_encoding'])
: escapeFunc()
],
'data' => [accessTemplatePropFunc('data')],
'data' => [assertArgsFunc(0, 1), templateDataFunc()],
'name' => [accessTemplatePropFunc('name')],
'context' => [accessTemplatePropFunc('context')],
'component' => [$template_args, componentFunc()],
Expand All @@ -62,5 +62,28 @@ function() use ($c) { return $c->get('renderTemplate'); },
$c->get('renderContext.func')
);
});

$plates->addMethods([
'registerFunction' => function(Plates\Engine $e, $name, callable $func, callable $assert_args = null, $simple = true) {
$c = $e->getContainer();
$func = $simple ? wrapSimpleFunc($func) : $func;

$c->wrap('renderContext.func.funcs', function($funcs, $c) use ($name, $func, $assert_args) {
$funcs[$name] = $assert_args ? [$assert_args, $func] : [$func];
return $funcs;
});
},
'addFuncs' => function(Plates\Engine $e, callable $add_funcs, $simple = false) {
$e->getContainer()->wrap('renderContext.func.funcs', function($funcs, $c) use ($add_funcs, $simple) {
$new_funcs = $simple
? array_map(wrapSimpleFunc::class, $add_funcs($c))
: $add_funcs($c);
return array_merge($funcs, $new_funcs);
});
},
'wrapFuncs' => function(Plates\Engine $e, callable $wrap_funcs) {
$e->getContainer()->wrap('renderContext.func.funcs', $wrap_funcs);
}
]);
}
}
18 changes: 18 additions & 0 deletions src/Extension/RenderContext/func.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ function insertFunc($echo = null) {
};
}

function templateDataFunc() {
return function(FuncArgs $args) {
list($data) = $args->args;
return array_merge($args->template()->data, $data);
};
}

/** Enables the backwards compatibility with the old extension functions */
function wrapSimpleFunc(callable $func, $enable_bc = false) {
return function(FuncArgs $args) use ($func, $enable_bc) {
if ($enable_bc && is_array($func) && isset($func[0]) && $func[0] instanceof Plates\Extension\ExtensionInterface) {
$func[0]->template = $args->template();
}

return $func(...$args->args);
};
}

function accessTemplatePropFunc($prop) {
return function(FuncArgs $args) use ($prop) {
return $args->template()->{$prop};
Expand Down

0 comments on commit bce7243

Please sign in to comment.