Skip to content

Commit a806e99

Browse files
Merge pull request #13 from Lorti/update
Transition from LightnCandy v0.89 to v0.94
2 parents 9a179c2 + 8a72266 commit a806e99

17 files changed

+319
-140
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"require": {
1414
"php": ">=5.4.0",
1515
"illuminate/view": "5.*",
16-
"zordius/lightncandy": "0.89"
16+
"zordius/lightncandy": "0.94"
17+
},
18+
"require-dev": {
19+
"orchestra/testbench": "~3.2"
1720
},
1821
"autoload": {
1922
"psr-4": {

config/handlebars.php

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
| File Extensions
2222
|--------------------------------------------------------------------------
2323
|
24-
| All file extensions that should be compiled with the Handlebars
25-
| template engine.
24+
| All file extensions that should be compiled with the Handlebars template
25+
| engine. Unless you specify your own partial resolver the package will
26+
| look for files in Laravel's view storage paths.
2627
|
2728
*/
28-
29+
2930
'fileext' => [
3031
'.handlebars',
3132
'.hbs',
@@ -36,53 +37,24 @@
3637
| Partials
3738
|--------------------------------------------------------------------------
3839
|
39-
| LightnCandy supports partial when compile time. You can provide partials
40-
| by partials option when compile()
40+
| https://github.com/zordius/lightncandy#partial-support
4141
|
4242
*/
4343

4444
'partials' => [],
45+
'partialresolver' => false,
4546

4647
/*
4748
|--------------------------------------------------------------------------
48-
| Custom Helpers
49+
| Helpers
4950
|--------------------------------------------------------------------------
5051
|
51-
| Custom helper can help you deal with common template tasks, for example:
52-
| provide URL and text then generate a link. To know more about custom
53-
| helper, you can read original handlebars.js document here:
54-
| http://handlebarsjs.com/expressions.html .
52+
| https://github.com/zordius/lightncandy#custom-helper
5553
|
5654
*/
5755

5856
'helpers' => [],
59-
60-
/*
61-
|--------------------------------------------------------------------------
62-
| Block Custom Helpers
63-
|--------------------------------------------------------------------------
64-
|
65-
| Block custom helper must be used as a section, the section is started
66-
| with {{#helper_name ...}} and ended with {{/helper_name}}.
67-
|
68-
*/
69-
70-
'blockhelpers' => [],
71-
72-
/*
73-
|--------------------------------------------------------------------------
74-
| Handlebars.js' Custom Helpers
75-
|--------------------------------------------------------------------------
76-
|
77-
| You can implement helpers more like Handlebars.js way with hbhelpers
78-
| option, all matched single custom helper and block custom helper will be
79-
| handled. In Handlebars.js, a block custom helper can rendener child block
80-
| by executing options.fn; or change context by send new context as first
81-
| parameter.
82-
|
83-
*/
84-
85-
'hbhelpers' => [],
57+
'helperresolver' => false,
8658

8759
/*
8860
|--------------------------------------------------------------------------

examples/handlebars-example-include.hbs

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/handlebars-example.hbs

Lines changed: 0 additions & 36 deletions
This file was deleted.

phpunit.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
13+
<testsuites>
14+
<testsuite name="Laravel Handlebars">
15+
<directory suffix="Test.php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<php>
20+
<env name="APP_ENV" value="testing"/>
21+
</php>
22+
23+
<filter>
24+
<whitelist addUncoveredFilesFromWhitelist="false">
25+
<directory suffix=".php">src/</directory>
26+
</whitelist>
27+
</filter>
28+
29+
</phpunit>

src/Compilers/BladeCompiler.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
use Illuminate\View\Compilers\BladeCompiler as BaseBladeCompiler;
44
use Illuminate\View\Compilers\CompilerInterface;
55

6-
class BladeCompiler extends BaseBladeCompiler implements CompilerInterface {
6+
class BladeCompiler extends BaseBladeCompiler implements CompilerInterface
7+
{
78

89
/**
910
* Compile the include statements into valid PHP.
1011
*
11-
* @param string $expression
12+
* @param string $expression
1213
* @return string
1314
*/
1415
protected function compileRaw($expression)
1516
{
16-
if (starts_with($expression, '('))
17-
{
17+
if (starts_with($expression, '(')) {
1818
$expression = substr($expression, 1, -1);
1919
}
2020

2121
return "<?php echo \$__env->make($expression, ['raw' => true], array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>";
2222
}
2323

24-
}
24+
}

src/Compilers/HandlebarsCompiler.php

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php namespace ProAI\Handlebars\Compilers;
22

3+
use Illuminate\Support\Facades\Lang;
34
use Illuminate\View\Compilers\Compiler;
45
use Illuminate\View\Compilers\CompilerInterface;
56
use Illuminate\Filesystem\Filesystem;
67
use ProAI\Handlebars\Support\LightnCandy;
78

8-
class HandlebarsCompiler extends Compiler implements CompilerInterface {
9+
class HandlebarsCompiler extends Compiler implements CompilerInterface
10+
{
911

1012
/**
1113
* LightnCandy instance.
@@ -38,10 +40,9 @@ class HandlebarsCompiler extends Compiler implements CompilerInterface {
3840
/**
3941
* Create a new compiler instance.
4042
*
41-
* @param \Illuminate\Filesystem\Filesystem $files
42-
* @param \ProAI\Handlebars\Support\LightnCandy $lightncandy
43-
* @param string $cachePath
44-
* @return void
43+
* @param \Illuminate\Filesystem\Filesystem $files
44+
* @param \ProAI\Handlebars\Support\LightnCandy $lightncandy
45+
* @param string $cachePath
4546
*/
4647
public function __construct(Filesystem $files, LightnCandy $lightncandy, $cachePath)
4748
{
@@ -57,7 +58,9 @@ public function __construct(Filesystem $files, LightnCandy $lightncandy, $cacheP
5758
$this->options['basedir'] = $app['config']->get('view.paths');
5859

5960
// make sure helpers array is set
60-
if ( ! isset($this->options['helpers'])) $this->options['helpers'] = [];
61+
if (!isset($this->options['helpers'])) {
62+
$this->options['helpers'] = [];
63+
}
6164

6265
// set language helpers option
6366
$this->languageHelpers = (isset($this->options['language_helpers']))
@@ -78,7 +81,7 @@ public function __construct(Filesystem $files, LightnCandy $lightncandy, $cacheP
7881
/**
7982
* Compile the view at the given path.
8083
*
81-
* @param string $path
84+
* @param string $path
8285
* @return void
8386
*/
8487
public function compile($path)
@@ -95,27 +98,24 @@ public function compile($path)
9598
/**
9699
* Compile the view at the given path.
97100
*
98-
* @param string $path
99-
* @param array $options
100-
* @param bool $raw
101-
* @return void
101+
* @param string $path
102+
* @param bool $raw
102103
*/
103104
public function compileString($path, $raw = false)
104105
{
105106
$options = $this->options;
106-
107+
107108
// set partials directory
108-
if ( ! $raw) {
109+
if (!$raw) {
109110
$options['basedir'][] = dirname($path);
110111
}
111112

112113
// set raw option
113114
array_set($options, 'compile_helpers_only', $raw);
114115

115116
// set language helper functions
116-
if($this->languageHelpers)
117-
{
118-
if ( ! $raw) {
117+
if ($this->languageHelpers) {
118+
if (!$raw) {
119119
$helpers = array_merge($this->getLanguageHelpers(), $options['helpers']);
120120
} elseif ($this->translateRawOutput) {
121121
$helpers = $this->getLanguageHelpers();
@@ -126,26 +126,43 @@ public function compileString($path, $raw = false)
126126
array_set($options, 'helpers', $helpers);
127127
}
128128

129-
// compile with Handlebars compiler
129+
// As of LightnCandy v0.91 resolving via `basedir` and `fileext` options has been stripped from LightnCandy.
130+
if (!$options['partialresolver']) {
131+
$options['partialresolver'] = function ($context, $name) use ($options) {
132+
foreach ($options['basedir'] as $dir) {
133+
foreach ($options['fileext'] as $ext) {
134+
$path = sprintf('%s/%s.%s', rtrim($dir, DIRECTORY_SEPARATOR), $name, ltrim($ext, '.'));
135+
if (file_exists($path)) {
136+
return file_get_contents($path);
137+
}
138+
}
139+
}
140+
return "[Partial $path not found]";
141+
};
142+
}
143+
130144
$contents = $this->lightncandy->compile($this->files->get($path), $options);
131145

132-
if ( ! is_null($this->cachePath)) {
133-
$this->files->put($this->getCompiledPath($path, $raw), $contents);
146+
if (!is_null($this->cachePath)) {
147+
// As of LightnCandy v0.90 generated PHP code will not includes `<?php`.
148+
$this->files->put($this->getCompiledPath($path, $raw), "<?php $contents");
134149
}
135150
}
136151

137152
/**
138153
* Get the path to the compiled version of a view.
139154
*
140-
* @param string $path
141-
* @param bool $raw
155+
* @param string $path
156+
* @param bool $raw
142157
* @return string
143158
*/
144159
public function getCompiledPath($path, $raw = false)
145160
{
146-
if ($raw) $path .= '-raw';
161+
if ($raw) {
162+
$path .= '-raw';
163+
}
147164

148-
return $this->cachePath.'/'.md5($path.'-raw');
165+
return $this->cachePath . '/' . md5($path . '-raw');
149166
}
150167

151168
/**
@@ -156,13 +173,13 @@ public function getCompiledPath($path, $raw = false)
156173
protected function getLanguageHelpers()
157174
{
158175
return [
159-
'lang' => function($args, $named) {
160-
return \Illuminate\Support\Facades\Lang::get($args[0], $named);
176+
'lang' => function ($args, $named) {
177+
return Lang::get($args[0], $named);
161178
},
162-
'choice' => function($args, $named) {
163-
return \Illuminate\Support\Facades\Lang::choice($args[0], $args[1], $named);
179+
'choice' => function ($args, $named) {
180+
return Lang::choice($args[0], $args[1], $named);
164181
}
165182
];
166183
}
167184

168-
}
185+
}

src/Engines/HandlebarsEngine.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
use Illuminate\View\Engines\EngineInterface;
44
use Illuminate\View\Engines\CompilerEngine;
55

6-
class HandlebarsEngine extends CompilerEngine implements EngineInterface {
6+
class HandlebarsEngine extends CompilerEngine implements EngineInterface
7+
{
78

89
/**
910
* Get the evaluated contents of the view.
1011
*
11-
* @param string $path
12-
* @param array $data
12+
* @param string $path
13+
* @param array $data
1314
* @return string
1415
*/
1516
public function get($path, array $data = array())
@@ -19,8 +20,7 @@ public function get($path, array $data = array())
1920
// If this given view has expired, which means it has simply been edited since
2021
// it was last compiled, we will re-compile the views so we can evaluate a
2122
// fresh copy of the view. We'll pass the compiler the path of the view.
22-
if ($this->compiler->isExpired($path))
23-
{
23+
if ($this->compiler->isExpired($path)) {
2424
$this->compiler->compile($path);
2525
}
2626

@@ -43,8 +43,8 @@ public function get($path, array $data = array())
4343
/**
4444
* Get the evaluated contents of the view at the given path.
4545
*
46-
* @param string $__path
47-
* @param array $__data
46+
* @param string $__path
47+
* @param array $__data
4848
* @return string
4949
*/
5050
protected function evaluatePath($__path, $__data)
@@ -70,4 +70,4 @@ protected function convertObjectToArray($item)
7070
return $item;
7171
}
7272

73-
}
73+
}

0 commit comments

Comments
 (0)