Skip to content
This repository was archived by the owner on May 5, 2019. It is now read-only.

Commit 85d85d4

Browse files
committed
Refactoring for extension utils
1 parent d1f92d8 commit 85d85d4

File tree

6 files changed

+68
-41
lines changed

6 files changed

+68
-41
lines changed

Diff for: src/Helper/Lookup.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ class Lookup
1515
*/
1616
public function __invoke($obj, $field)
1717
{
18-
return Utils::lookup($obj, $field);
18+
return Utils::nameLookup($obj, $field);
1919
}
2020
}

Diff for: src/Runtime.php

+40-10
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function expression($value)
114114
*/
115115
public function escapeExpression($value)
116116
{
117-
return Utils::escapeExpression($value, false);
117+
return Utils::escapeExpression($value);
118118
}
119119

120120
/**
@@ -127,7 +127,7 @@ public function escapeExpression($value)
127127
*/
128128
public function escapeExpressionCompat($value)
129129
{
130-
return Utils::escapeExpression($value);
130+
return Utils::escapeExpressionCompat($value);
131131
}
132132

133133
/**
@@ -195,14 +195,14 @@ public function invokePartial($partial, $context, $options)
195195

196196
if( null === $result ) {
197197
if( !$partial ) {
198-
$options['partials'][$options['name']] = Utils::noop();
198+
$options['partials'][$options['name']] = $this->noop();
199199
} else if( is_string($partial) ) {
200200
$options['partials'][$options['name']] = $this->handlebars->compile($partial, $this->options);
201201
}
202202
$result = call_user_func($options['partials'][$options['name']], $context, $options);
203203
}
204204
if( $result != null && !empty($options['indent']) ) {
205-
$result = Utils::indent($result, $options['indent']);
205+
$result = $this->indent($result, $options['indent']);
206206
}
207207
return $result;
208208
}
@@ -216,7 +216,7 @@ private function invokePartialInner($partial, $context, &$options)
216216
}
217217

218218
$partialBlock = null;
219-
if( !empty($options['fn']) && $options['fn'] !== Utils::noop() ) {
219+
if( !empty($options['fn']) && $options['fn'] !== $this->noop() ) {
220220
$partialBlock = $options['data']['partial-block'] = $options['fn'];
221221
$options['fn'] = ClosureWrapper::wrap($options['fn']);
222222

@@ -273,15 +273,45 @@ public function lookupData($depths, $name)
273273
}
274274

275275
/**
276-
* Alias for Utils::lookup()
276+
* Alias for Utils::nameLookup()
277277
*
278278
* @param mixed $objOrArray
279279
* @param string $field
280280
* @return mixed
281281
*/
282282
public function nameLookup($objOrArray, $field)
283283
{
284-
return Utils::lookup($objOrArray, $field);
284+
return Utils::nameLookup($objOrArray, $field);
285+
}
286+
287+
public function noop()
288+
{
289+
static $noop;
290+
if( null === $noop ) {
291+
$noop = function () {
292+
293+
};
294+
}
295+
return $noop;
296+
}
297+
298+
/**
299+
* Indent a multi-line string
300+
*
301+
* @param string $str
302+
* @param string $indent
303+
* @return string
304+
*/
305+
public function indent($str, $indent)
306+
{
307+
$lines = explode("\n", $str);
308+
for( $i = 0, $l = count($lines); $i < $l; $i++ ) {
309+
if( empty($lines[$i]) && $i + 1 == $l ) {
310+
break;
311+
}
312+
$lines[$i] = $indent . $lines[$i];
313+
}
314+
return implode("\n", $lines);
285315
}
286316

287317
/**
@@ -333,11 +363,11 @@ private function resolvePartial($partial, &$options)
333363
if( $options['name'] === '@partial-block' ) {
334364
$partial = $options['data']['partial-block'];
335365
} else {
336-
$partial = Utils::lookup($options['partials'], $options['name']);
366+
$partial = Utils::nameLookup($options['partials'], $options['name']);
337367
}
338368
} else if( !Utils::isCallable($partial) && empty($options['name']) ) {
339369
$options['name'] = $partial;
340-
$partial = Utils::lookup($options['partials'], $partial);
370+
$partial = Utils::nameLookup($options['partials'], $partial);
341371
}
342372
return $partial;
343373
}
@@ -362,7 +392,7 @@ private function wrapProgram($fn, $data = null, $declaredBlockParams = null, $bl
362392
}
363393
if( null !== $blockParams ) {
364394
$blockParams = array_merge(array(
365-
Utils::lookup($options, 'blockParams'),
395+
Utils::nameLookup($options, 'blockParams'),
366396
), $blockParams);
367397
}
368398

Diff for: src/Utils.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ public static function appendContextPath($contextPath, $id)
3232

3333
public static function createFrame($object)
3434
{
35-
if( is_object($object) ) {
36-
$frame = clone $object;
37-
$frame->_parent = $object;
38-
} else if( is_scalar($object) ) {
35+
if( is_scalar($object) ) {
3936
$frame = array($object);
40-
} else {
37+
} else if( is_array($object) ) {
4138
$frame = $object;
4239
$frame['_parent'] = $object;
4340
}
@@ -78,6 +75,11 @@ public static function escapeExpression($value, $compat = true)
7875
return $value;
7976
}
8077

78+
public static function escapeExpressionCompat($value)
79+
{
80+
return self::escapeExpression($value, true);
81+
}
82+
8183
/**
8284
* Indent a multi-line string
8385
*
@@ -140,7 +142,7 @@ public static function isIntArray($array)
140142
* @param string $field
141143
* @return mixed
142144
*/
143-
public static function lookup($objOrArray, $field)
145+
public static function nameLookup($objOrArray, $field)
144146
{
145147
if( is_array($objOrArray) || $objOrArray instanceof ArrayAccess ) {
146148
return isset($objOrArray[$field]) ? $objOrArray[$field] : null;

Diff for: src/VM/VM.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ private function setupParams($helperName, $paramSize, &$params, $useRegister = f
430430
private function wrapProgram($program)
431431
{
432432
if( $program === null ) {
433-
return Utils::noop();
433+
return $this->runtime->noop();
434434
}
435435

436436
$self = $this;

Diff for: tests/RuntimeTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,21 @@ public function testHelperMissingMissingThrows()
3030
$runtime = new Runtime(new Handlebars(), array());
3131
$runtime->helperMissingMissing();
3232
}
33+
34+
public function testIndent()
35+
{
36+
$runtime = new Runtime(new Handlebars(), array());
37+
$this->assertEquals(
38+
" blah",
39+
$runtime->indent("blah", ' ')
40+
);
41+
$this->assertEquals(
42+
" blah\n blah",
43+
$runtime->indent("blah\nblah", ' ')
44+
);
45+
$this->assertEquals(
46+
" \n \n \n",
47+
$runtime->indent("\n\n\n", ' ')
48+
);
49+
}
3350
}

Diff for: tests/UtilsTest.php

+1-23
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,10 @@ public function testAppendContextPath()
2929

3030
public function testCreateFrame()
3131
{
32-
$obj1 = new \stdClass;
33-
$obj2 = Utils::createFrame($obj1);
34-
$this->assertSame($obj1, $obj2->_parent);
35-
$this->assertInstanceOf('\\stdClass', $obj1);
36-
$this->assertNotSame($obj1, $obj2);
37-
3832
$arr1 = array();
3933
$arr2 = Utils::createFrame($arr1);
4034
$this->assertSame($arr1, $arr2['_parent']);
41-
$this->assertNotSame($obj1, $obj2);
42-
}
43-
44-
public function testIndent()
45-
{
46-
$this->assertEquals(
47-
" blah",
48-
Utils::indent("blah", ' ')
49-
);
50-
$this->assertEquals(
51-
" blah\n blah",
52-
Utils::indent("blah\nblah", ' ')
53-
);
54-
$this->assertEquals(
55-
" \n \n \n",
56-
Utils::indent("\n\n\n", ' ')
57-
);
35+
$this->assertNotSame($arr1, $arr2);
5836
}
5937

6038
public function testIsIntArray()

0 commit comments

Comments
 (0)