Skip to content

Commit

Permalink
Merge pull request #195 from thephpleague/normalize-name
Browse files Browse the repository at this point in the history
Normalize Name
  • Loading branch information
ragboyjr committed Jan 10, 2018
2 parents 91af3e1 + 12c7752 commit e066cc2
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 65 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ We accept contributions via Pull Requests on [Github](https://github.com/thephpl
## Running Tests

``` bash
$ phpunit
$ make test
```
**Happy coding**!
**Happy coding**!
4 changes: 1 addition & 3 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public function __construct($config = []) {
'php_extensions' => ['php', 'phtml'],
'image_extensions' => ['png', 'jpg'],
]);
$this->container->add('compose', function($c) {
return Util\id();
});
$this->container->addComposed('compose', function() { return []; });
$this->container->add('fileExists', function($c) {
return 'file_exists';
});
Expand Down
13 changes: 6 additions & 7 deletions src/Extension/Data/DataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ public function register(Plates\Engine $plates) {
$c->add('data.globals', []);
$c->add('data.template_data', []);
$c->merge('config', ['merge_parent_data' => true]);
$c->wrap('compose', function($compose, $c) {
return Plates\Util\compose(...array_filter([
$c->get('config')['merge_parent_data'] ? mergeParentDataCompose() : null,
$c->get('data.globals') ? globalResolveData($c->get('data.globals')) : null,
$c->get('data.template_data') ? perTemplateResolveData($c->get('data.template_data')) : null,
$compose
]));
$c->wrapComposed('compose', function($composers, $c) {
return array_merge(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/Data/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

function addGlobalsCompose(array $globals) {
return function(Template $template) use ($globals) {
return $template->withAddedData($globals);
return $template->withData(array_merge($globals, $template->data));
};
}

Expand All @@ -20,10 +20,10 @@ function mergeParentDataCompose() {

function perTemplateDataCompose(array $template_data_map) {
return function(Template $template) use ($template_data_map) {
$name = $template->name;
$name = $template->get('normalized_name', $template->name);

return isset($template_data_map[$name])
? $template->withAddedData($template_data_map[$name])
? $template->withData(array_merge($template_data_map[$name], $template->data))
: $template;
};
}
7 changes: 6 additions & 1 deletion src/Extension/Folders/FoldersExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function register(Plates\Engine $plates) {
'folder_separator' => '::',
]);
$c->add('folders.folders', []);
$c->wrap('path.resolvePath.stack', function($stack, $c) {
$c->wrapStack('path.resolvePath', function($stack, $c) {
$config = $c;
return array_merge([
'folders' => foldersResolvePath(
Expand All @@ -22,6 +22,11 @@ public function register(Plates\Engine $plates) {
)
], $stack);
});
$c->wrapComposed('path.normalizeName', function($composed, $c) {
return array_merge($composed, [
'folders.stripFolders' => stripFoldersNormalizeName($c->get('folders.folders'))
]);
});
$plates->addMethods([
'addFolder' => function($plates, $folder, $prefixes, $fallback = false) {
$prefixes = is_string($prefixes) ? [$prefixes] : $prefixes;
Expand Down
14 changes: 14 additions & 0 deletions src/Extension/Folders/folders.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ function foldersResolvePath(array $folders, $sep = '::', $file_exists = 'file_ex
return $path;
};
}

function stripFoldersNormalizeName(array $folders, $sep = '::') {
return function($name) use ($folders, $sep) {
foreach ($folders as $folder) {
foreach (array_filter($folder['prefixes']) as $prefix) {
if (strpos($name, $prefix) === 0) {
return $folder['folder'] . $sep . substr($name, strlen($prefix) + 1);
}
}
}

return $name;
};
}
4 changes: 2 additions & 2 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->wrap('compose', function($compose, $c) {
return Plates\Util\compose($compose, sectionsCompose());
$c->wrapComposed('compose', function($composed, $c) {
return array_merge($composed, ['layoutSections.sections' => sectionsCompose()]);
});
$c->wrap('renderTemplate.factories', function($factories, $c) {
$default_layout_path = $c->get('config')['default_layout_path'];
Expand Down
14 changes: 0 additions & 14 deletions src/Extension/NormalizeName/NormalizeNameExtension.php

This file was deleted.

3 changes: 0 additions & 3 deletions src/Extension/NormalizeName/normalize-name.php

This file was deleted.

35 changes: 21 additions & 14 deletions src/Extension/Path/PathExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,35 @@ final class PathExtension implements Plates\Extension
{
public function register(Plates\Engine $plates) {
$c = $plates->getContainer();
$c->add('path.resolvePath.stack', function($c) {
$c->add('path.resolvePath.prefixes', function($c) {
$config = $c->get('config');

// wrap base dir in an array if not already
$base_dir = isset($config['base_dir']) ? $config['base_dir'] : null;
$base_dir = $base_dir ? (is_string($base_dir) ? [$base_dir] : $base_dir) : $base_dir;

return $base_dir;
});
$c->addComposed('path.normalizeName', function($c) {
return [
'path.stripExt' => stripExtNormalizeName(),
'path.stripPrefix' => stripPrefixNormalizeName($c->get('path.resolvePath.prefixes'))
];
});
$c->addStack('path.resolvePath', function($c) {
$config = $c->get('config');
$prefixes = $c->get('path.resolvePath.prefixes');
return array_filter([
'relative' =>relativeResolvePath(),
'ext' => isset($config['ext']) ? extResolvePath($config['ext']) : null,
'prefix' => $base_dir ? prefixResolvePath($base_dir, $c->get('fileExists')) : null,
'id' => idResolvePath(),
'path.relative' =>relativeResolvePath(),
'path.ext' => isset($config['ext']) ? extResolvePath($config['ext']) : null,
'path.prefix' => $prefixes ? prefixResolvePath($prefixes, $c->get('fileExists')) : null,
'path.id' => idResolvePath(),
]);
});
$c->add('path.resolvePath', function($c) {
return Plates\Util\stack($c->get('path.resolvePath.stack'));
});
$c->wrap('compose', function($compose, $c) {
return Plates\Util\compose(
$compose,
resolvePathCompose($c->get('path.resolvePath'))
);
$c->wrapComposed('compose', function($composed, $c) {
return array_merge([
'path.resolvePath' => resolvePathCompose($c->get('path.resolvePath')),
'path.normalizeName' => normalizeNameCompose($c->get('path.normalizeName'))
], $composed);
});
}
}
40 changes: 35 additions & 5 deletions src/Extension/Path/path.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@ function resolvePathCompose(callable $resolve_path) {
};
}

function normalizeNameCompose(callable $normalize_name) {
return function(Plates\Template $template) use ($normalize_name) {
return $template->with(
'normalized_name',
Plates\Util\isPath($template->name) ? $normalize_name($template->get('path')) : $template->name
);
};
}

function stripExtNormalizeName() {
return function($name) {
$ext = pathinfo($name, PATHINFO_EXTENSION);
if (!$ext) {
return $name;
}

return substr($name, 0, (strlen($ext) + 1) * -1); // +1 for the leading `.`
};
}

function stripPrefixNormalizeName(array $prefixes) {
$prefixes = array_filter($prefixes);
return function($name) use ($prefixes) {
foreach ($prefixes as $prefix) {
if (strpos($name, $prefix . '/') === 0) {
return substr($name, strlen($prefix) + 1); // +1 for the trailing `/`
}
}

return $name;
};
}

/** appends an extension to the name */

function extResolvePath($ext = 'phtml') {
Expand All @@ -32,7 +65,7 @@ function prefixResolvePath(array $prefixes, $file_exists = 'file_exists') {
}

foreach ($prefixes as $cur_prefix) {
$path = strpos($args->path, '/') === 0
$path = Plates\Util\isAbsolutePath($args->path)
? $next($args)
: $next($args->withPath(
Plates\Util\joinPath([$cur_prefix, $args->path])
Expand Down Expand Up @@ -69,10 +102,7 @@ function prefixResolvePath(array $prefixes, $file_exists = 'file_exists') {
/** Figures out the path based off of the parent templates current path */
function relativeResolvePath() {
return function(ResolvePathArgs $args, $next) {
$is_relative = (
strpos($args->path, './') === 0
|| strpos($args->path, '../') === 0
) && $args->template->parent;
$is_relative = Plates\Util\isRelativePath($args->path) && $args->template->parent;

if (!$is_relative) {
return $next($args); // nothing to do
Expand Down
17 changes: 8 additions & 9 deletions src/Extension/RenderContext/RenderContextExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ final class RenderContextExtension implements Plates\Extension
{
public function register(Plates\Engine $plates) {
$c = $plates->getContainer();
$c->add('renderContext.func', function($c) {
return Plates\Util\stack($c->get('renderContext.func.stack'));
});
$c->add('renderContext.func.stack', function($c) {
$c->addStack('renderContext.func', function($c) {
return [
'plates' => Plates\Util\stackGroup([
aliasNameFunc($c->get('renderContext.func.aliases')),
Expand Down Expand Up @@ -48,11 +45,13 @@ public function register(Plates\Engine $plates) {
];
});

$c->wrap('compose', function($compose, $c) {
return Plates\Util\compose($compose, renderContextCompose(
$c->get('renderContext.factory'),
$c->get('config')['render_context_var_name']
));
$c->wrapComposed('compose', function($composed, $c) {
return array_merge($composed, [
'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
22 changes: 20 additions & 2 deletions src/Util/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,31 @@ public function add($id, $value) {
}
$this->boxes[$id] = [$value, $value instanceof \Closure ? true : false];
}
public function addComposed($id, callable $define_composers) {
$this->add($id, function($c) use ($id) {
return compose(...array_values($c->get($id . '.composers')));
});
$this->add($id . '.composers', $define_composers);
}
public function wrapComposed($id, callable $wrapped) {
$this->wrap($id . '.composers', $wrapped);
}
public function addStack($id, callable $define_stack) {
$this->add($id, function($c) use ($id) {
return stack($c->get($id . '.stack'));
});
$this->add($id . '.stack', $define_stack);
}
public function wrapStack($id, callable $wrapped) {
$this->wrap($id . '.stack', $wrapped);
}
public function merge($id, array $values) {
$old = $this->get($id);
$this->add($id, array_merge($old, $values));
}
public function wrap($id, $wrapper) {
if (!$this->has($id)) {
throw new \LogicException('Cannot wrap a service that does not exist.');
throw new \LogicException('Cannot wrap service ' . $id . ' that does not exist.');
}
$box = $this->boxes[$id];
$this->boxes[$id] = [function($c) use ($box, $wrapper) {
Expand All @@ -31,7 +49,7 @@ public function get($id) {
return $this->cached[$id];
}
if (!$this->has($id)) {
throw new \LogicException('Cannot retrieve service that does exist.');
throw new \LogicException('Cannot retrieve service ' . $id . ' that does exist.');
}
$result = $this->unbox($this->boxes[$id], $this);
if ($this->boxes[$id][1]) { // only cache services
Expand Down
13 changes: 13 additions & 0 deletions src/Util/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ function joinPath(array $parts, $sep = DIRECTORY_SEPARATOR) {
}, null);
}

function isAbsolutePath($path) {
return strpos($path, '/') === 0;
}
function isRelativePath($path) {
return strpos($path, './') === 0 || strpos($path, '../') === 0;
}
function isResourcePath($path) {
return strpos($path, '://') !== false;
}
function isPath($path) {
return isAbsolutePath($path) || isRelativePath($path) || isResourcePath($path);
}

/** returns the debug type of an object as string for exception printing */
function debugType($v) {
if (is_object($v)) {
Expand Down
1 change: 1 addition & 0 deletions test/integration/fixtures/normalize-name/main.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name: <?=$name?>
16 changes: 16 additions & 0 deletions test/integration/path.spec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use League\Plates;

describe('Path Extension', function() {
it('will normalize any path type names', function() {
$plates = new Plates\Engine([
'base_dir' => __DIR__ . '/fixtures/normalize-name'
]);
$plates->addGlobals(['name' => 'Bar']);
$plates->assignTemplateData('main', ['name' => 'Foo']);

$expected = "name: Foo";
expect($plates->render(__DIR__ . '/fixtures/normalize-name/main'))->equal($expected);
});
});

0 comments on commit e066cc2

Please sign in to comment.