Skip to content

Commit

Permalink
Merge pull request #208 from thephpleague/reverse-ordering
Browse files Browse the repository at this point in the history
Reverse Ordering #194
  • Loading branch information
ragboyjr committed Jan 19, 2018
2 parents 068ee66 + 2f81330 commit 6ab9127
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 61 deletions.
6 changes: 3 additions & 3 deletions src/Extension/Data/DataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public function register(Plates\Engine $plates) {
$c->add('data.template_data', []);
$c->merge('config', ['merge_parent_data' => true]);

$plates->unshiftComposers(function($c) {
$plates->pushComposers(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,
'data.mergeParentData' => $c->get('config')['merge_parent_data'] ? mergeParentDataCompose() : null,
'data.perTemplateData' => $c->get('data.template_data') ? perTemplateDataCompose($c->get('data.template_data')) : null,
]);
});

Expand Down
4 changes: 2 additions & 2 deletions src/Extension/Folders/FoldersExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public function register(Plates\Engine $plates) {
$c->add('folders.folders', []);
$c->wrapStack('path.resolvePath', function($stack, $c) {
$config = $c;
return array_merge([
return array_merge($stack, [
'folders' => foldersResolvePath(
$c->get('folders.folders'),
$c->get('config')['folder_separator'],
$c->get('fileExists')
)
], $stack);
]);
});
$c->wrapComposed('path.normalizeName', function($composed, $c) {
return array_merge($composed, [
Expand Down
14 changes: 7 additions & 7 deletions src/Extension/LayoutSections/LayoutSectionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public function register(Plates\Engine $plates) {
$c = $plates->getContainer();

$c->merge('config', ['default_layout_path' => null]);
$c->wrapComposed('compose', function($composed, $c) {
return array_merge($composed, ['layoutSections.sections' => sectionsCompose()]);
$plates->pushComposers(function($c) {
return ['layoutSections.sections' => sectionsCompose()];
});
$c->wrap('renderTemplate.factories', function($factories, $c) {
$default_layout_path = $c->get('config')['default_layout_path'];
Expand All @@ -27,11 +27,11 @@ public function register(Plates\Engine $plates) {
$one_arg = RenderContext\assertArgsFunc(1);

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)],
'layout' => [layoutFunc(), $template_args],
'section' => [sectionFunc(), $one_arg],
'start' => [startFunc(), $one_arg],
'push' => [startFunc(START_APPEND), $one_arg],
'unshift' => [startFunc(START_PREPEND), $one_arg],
];
});
}
Expand Down
10 changes: 5 additions & 5 deletions src/Extension/Path/PathExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public function register(Plates\Engine $plates) {
$config = $c->get('config');
$prefixes = $c->get('path.resolvePath.prefixes');
return array_filter([
'path.relative' =>relativeResolvePath(),
'path.ext' => isset($config['ext']) ? extResolvePath($config['ext']) : null,
'path.prefix' => $prefixes ? prefixResolvePath($prefixes, $c->get('fileExists')) : null,
'path.id' => idResolvePath(),
'path.prefix' => $prefixes ? prefixResolvePath($prefixes, $c->get('fileExists')) : null,
'path.ext' => isset($config['ext']) ? extResolvePath($config['ext']) : null,
'path.relative' => relativeResolvePath(),
]);
});
$plates->unshiftComposers(function($c) {
$plates->pushComposers(function($c) {
return [
'path.normalizeName' => normalizeNameCompose($c->get('path.normalizeName')),
'path.resolvePath' => resolvePathCompose($c->get('path.resolvePath')),
'path.normalizeName' => normalizeNameCompose($c->get('path.normalizeName'))
];
});
}
Expand Down
23 changes: 11 additions & 12 deletions src/Extension/RenderContext/RenderContextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public function register(Plates\Engine $plates) {
$c = $plates->getContainer();
$c->addStack('renderContext.func', function($c) {
return [
'notFound' => notFoundFunc(),
'plates' => Plates\Util\stackGroup([
splitByNameFunc($c->get('renderContext.func.funcs')),
aliasNameFunc($c->get('renderContext.func.aliases')),
splitByNameFunc($c->get('renderContext.func.funcs'))
]),
'notFound' => notFoundFunc(),
];
});
$c->add('renderContext.func.aliases', [
Expand All @@ -29,29 +29,28 @@ public function register(Plates\Engine $plates) {
$config = $c->get('config');

return [
'insert' => [$template_args, insertFunc()],
'insert' => [insertFunc(), $template_args],
'escape' => [
$one_arg,
isset($config['escape_flags'], $config['escape_encoding'])
? escapeFunc($config['escape_flags'], $config['escape_encoding'])
: escapeFunc()
: escapeFunc(),
$one_arg,
],
'data' => [assertArgsFunc(0, 1), templateDataFunc()],
'data' => [templateDataFunc(), assertArgsFunc(0, 1)],
'name' => [accessTemplatePropFunc('name')],
'context' => [accessTemplatePropFunc('context')],
'component' => [$template_args, componentFunc()],
'slot' => [$one_arg, slotFunc()],
'component' => [componentFunc(), $template_args],
'slot' => [slotFunc(), $one_arg],
'end' => [endFunc()]
];
});

$c->wrapComposed('compose', function($composed, $c) {
return array_merge($composed, [
$plates->pushComposers(function($c) {
return [
'renderContext.renderContext' => renderContextCompose(
$c->get('renderContext.factory'),
$c->get('config')['render_context_var_name']
)
]);
];
});
$c->add('include.bind', function($c) {
return renderContextBind($c->get('config')['render_context_var_name']);
Expand Down
21 changes: 9 additions & 12 deletions src/Util/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function phpEcho() {

/** stack a set of functions into each other and returns the stacked func */
function stack(array $funcs) {
return array_reduce(array_reverse($funcs), function($next, $func) {
return array_reduce($funcs, function($next, $func) {
return function(...$args) use ($next, $func) {
$args[] = $next;
return $func(...$args);
Expand All @@ -31,28 +31,25 @@ function stack(array $funcs) {
});
}

/** takes a structured array and sorts them by priority. This allows for prioritized stacks.
The structure is just an associative array where the indexes are numeric and the values
are array of stack handlers. The array is sorted by key and then all inner arrays are merged
together */
function sortStacks($stacks) {
ksort($stacks);
return array_merge(...$stacks);
}

function stackGroup(array $funcs) {
$end_next = null;
$funcs[] = function(...$args) use (&$end_next) {
array_unshift($funcs, function(...$args) use (&$end_next) {
return $end_next(...array_slice($args, 0, -1));
};
});
$next = stack($funcs);
return function(...$args) use ($next, &$end_next) {
$end_next = end($args);
return $next(...array_slice($args, 0, -1));
};
}

/** compose(f, g)(x) = f(g(x)) */
function compose(...$funcs) {
return pipe(...array_reverse($funcs));
}

/** pipe(f, g)(x) = g(f(x)) */
function pipe(...$funcs) {
return function($arg) use ($funcs) {
return array_reduce($funcs, function($acc, $func) {
return $func($acc);
Expand Down
10 changes: 5 additions & 5 deletions test/unit/extension-path.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
describe('relativeResolvePath', function() {
beforeEach(function() {
$this->args->template = new Template('a', [], [], null, (new Template('b', [], ['path' => 'foo/b.phtml']))->reference);
$this->resolve = stack([relativeResolvePath(), idResolvePath()]);
$this->resolve = stack([idResolvePath(), relativeResolvePath()]);
});
it('resolves the name relative to the current_directory in context', function() {
$resolve = $this->resolve;
Expand All @@ -54,9 +54,9 @@
});
describe('prefixResolvePath', function() {
beforeEach(function() {
$this->resolve = stack([prefixResolvePath(['/foo', '/bar'], function() {
$this->resolve = stack([idResolvePath(), prefixResolvePath(['/foo', '/bar'], function() {
return true;
}), idResolvePath()]);
})]);
});
it('prefixes non absolute paths with a base path', function() {
$path = ($this->resolve)($this->args->withPath('bar'));
Expand All @@ -71,12 +71,12 @@
});
describe('extResolvePath', function() {
it('appends an extension to the name', function() {
$resolve = stack([extResolvePath('bar'), idResolvePath()]);
$resolve = stack([idResolvePath(), extResolvePath('bar')]);
$path = $resolve($this->args->withPath('foo'));
expect($path)->equal('foo.bar');
});
it('does not append the name if ext already exists', function() {
$resolve = stack([extResolvePath('bar'), idResolvePath()]);
$resolve = stack([idResolvePath(), extResolvePath('bar')]);
$path = $resolve($this->args->withPath('foo.bar'));
expect($path)->equal('foo.bar');
});
Expand Down
18 changes: 3 additions & 15 deletions test/unit/util.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
describe('stack', function() {
it('can stack a group of functions', function() {
$handler = Util\stack([
Util\id(),
function($v, $next) {
return $next($v + 1) * 2;
},
function($v) {
return $v;
}
]);
expect($handler(2))->equal(6);
Expand All @@ -38,23 +36,13 @@ function($v) {
};
};
$group = Util\stackGroup([
$add(2),
$add(1),
$add(2)
]);
$stack = Util\stack([$group, Util\id()]);
$stack = Util\stack([Util\id(), $group]);
expect($stack(0))->equal(3);
});
});
describe('prioritizeStacks', function() {
it('prioritizes stacks by index', function() {
$stacks = [
1 => [3],
-1 => [1],
0 => [2],
];
expect(Util\sortStacks($stacks))->equal([1,2,3]);
});
});
describe('joinPath', function() {
it('joins paths together', function() {
expect(Util\joinPath(['a', 'b'], '/'))->equal('a/b');
Expand Down

0 comments on commit 6ab9127

Please sign in to comment.