Skip to content

Commit

Permalink
Adding pushComposers and unshiftComposers #194
Browse files Browse the repository at this point in the history
- Added new methods on the engine to help
  with adding and unshifting composers

Signed-off-by: RJ Garcia <rj@bighead.net>
  • Loading branch information
ragboyjr committed Jan 15, 2018
1 parent bce7243 commit 7954b5f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
15 changes: 14 additions & 1 deletion src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function __construct($config = []) {
if ($c->get('config')['validate_paths']) {
$rt = new RenderTemplate\ValidatePathRenderTemplate($rt, $c->get('fileExists'));
}
// $rt = new RenderTemplate\LayoutRenderTemplate($rt);
$rt = array_reduce($c->get('renderTemplate.factories'), function($rt, $create) {
return $create($rt);
}, $rt);
Expand All @@ -56,6 +55,20 @@ public function __construct($config = []) {
$this->container->add('renderTemplate.factories', function() {
return [];
});

$this->addMethods([
'pushComposers' => function(Engine $e, $def_composer) {
$e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) {
return array_merge($composed, $def_composer($c));
});
},
'unshiftComposers' => function(Engine $e, $def_composer) {
$e->getContainer()->wrapComposed('compose', function($composed, $c) use ($def_composer) {
return array_merge($def_composer($c), $composed);
});
},
]);

$this->register(new Extension\Data\DataExtension());
$this->register(new Extension\Path\PathExtension());
$this->register(new Extension\RenderContext\RenderContextExtension());
Expand Down
7 changes: 4 additions & 3 deletions src/Extension/Data/DataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ public function register(Plates\Engine $plates) {
$c->add('data.globals', []);
$c->add('data.template_data', []);
$c->merge('config', ['merge_parent_data' => true]);
$c->wrapComposed('compose', function($composers, $c) {
return array_merge(array_filter([

$plates->unshiftComposers(function($c) {
return array_filter([
'data.perTemplateData' => $c->get('data.template_data') ? perTemplateDataCompose($c->get('data.template_data')) : null,
'data.mergeParentData' => $c->get('config')['merge_parent_data'] ? mergeParentDataCompose() : null,
'data.addGlobals' => $c->get('data.globals') ? addGlobalsCompose($c->get('data.globals')) : null,
]), $composers);
]);
});

$plates->addMethods([
Expand Down
6 changes: 3 additions & 3 deletions src/Extension/Path/PathExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public function register(Plates\Engine $plates) {
'path.id' => idResolvePath(),
]);
});
$c->wrapComposed('compose', function($composed, $c) {
return array_merge([
$plates->unshiftComposers(function($c) {
return [
'path.resolvePath' => resolvePathCompose($c->get('path.resolvePath')),
'path.normalizeName' => normalizeNameCompose($c->get('path.normalizeName'))
], $composed);
];
});
}
}

0 comments on commit 7954b5f

Please sign in to comment.