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

Commit b5e3758

Browse files
committed
Implement log helper
1 parent 248a5e4 commit b5e3758

File tree

9 files changed

+147
-48
lines changed

9 files changed

+147
-48
lines changed

Diff for: compat/Options.php

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ class Options implements ArrayAccess
6161
public $contexts;
6262
public $args;
6363
public $partial;
64+
65+
/**
66+
* @var Runtime
67+
*/
68+
public $runtime;
6469

6570
/**
6671
* Constructor

Diff for: composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"require" : {
2828
"php" : ">=5.3.0",
2929
"ext-handlebars": "0.6.*",
30-
"ext-spl" : "*"
30+
"ext-spl" : "*",
31+
"psr/log": "~1.0.0"
3132
},
3233
"require-dev": {
3334
"jbboehr/coding-standard": "1.0.*@dev",
@@ -40,6 +41,9 @@
4041
"scrutinizer/ocular": "~1.1",
4142
"squizlabs/php_codesniffer": "~2.3.2"
4243
},
44+
"suggest": {
45+
"ext-psr": "*"
46+
},
4347
"scripts": {
4448
"test": "phpunit"
4549
},

Diff for: composer.lock

+43-42
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/Compiler/CompilerImpl.php

+3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ private function setupBuiltins()
122122
if( !isset($this->helpers['helperMissing']) ) {
123123
$this->helpers['helperMissing'] = new \Handlebars\Helper\HelperMissing();
124124
}
125+
if( !isset($this->helpers['log']) ) {
126+
$this->helpers['log'] = new \Handlebars\Helper\Log();
127+
}
125128
if( !isset($this->helpers['lookup']) ) {
126129
$this->helpers['lookup'] = new \Handlebars\Helper\Lookup();
127130
}

Diff for: src/Helper/Log.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Handlebars\Helper;
4+
5+
use Psr\Log\LoggerInterface;
6+
use Handlebars\Impl;
7+
use Handlebars\Runtime;
8+
9+
class Log
10+
{
11+
/**
12+
* Log builtin
13+
*
14+
* @return mixed
15+
*/
16+
public function __invoke()
17+
{
18+
$args = func_get_args();
19+
$options = array_pop($args);
20+
21+
$str = '';
22+
foreach( $args as $arg ) {
23+
$str .= (is_scalar($arg) ? $arg : print_r($arg, true));
24+
}
25+
26+
$level = isset($options->hash['level']) ? $options->hash['level'] : 'info';
27+
28+
if( $options->runtime instanceof Runtime &&
29+
($impl = $options->runtime->getImpl()) &&
30+
($logger = $impl->getLogger()) instanceof LoggerInterface ) {
31+
$logger->log($level, $str, array());
32+
} else {
33+
error_log('[handlebars] [' . $level . '] ' . $str);
34+
}
35+
}
36+
}

Diff for: src/Runtime.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class Runtime extends Utils
5454
protected $options;
5555

5656
/**
57-
* @var \Handlebars\Handlebars
57+
* @var Impl
5858
*/
59-
protected $handlebars;
59+
protected $impl;
6060

6161
/**
6262
* Constructor
@@ -65,7 +65,7 @@ class Runtime extends Utils
6565
*/
6666
public function __construct(Impl $handlebars)
6767
{
68-
$this->handlebars = $handlebars;
68+
$this->impl = $handlebars;
6969
$this->helpers = $handlebars->getHelpers();
7070
$this->partials = $handlebars->getPartials();
7171
$this->decorators = $handlebars->getDecorators();
@@ -138,6 +138,14 @@ public function getPartials()
138138
return $this->partials;
139139
}
140140

141+
/**
142+
* @return Impl
143+
*/
144+
public function getImpl()
145+
{
146+
return $this->impl;
147+
}
148+
141149
/**
142150
* Invoke partial runtime helper
143151
*
@@ -160,7 +168,7 @@ public function invokePartial($partial, $context, $options)
160168
if( !$partial ) {
161169
$options['partials'][$options['name']] = $this->noop();
162170
} else if( is_string($partial) ) {
163-
$options['partials'][$options['name']] = $this->handlebars->compile($partial, $this->options);
171+
$options['partials'][$options['name']] = $this->impl->compile($partial, $this->options);
164172
}
165173
$result = call_user_func($options['partials'][$options['name']], $context, $options);
166174
}
@@ -289,7 +297,9 @@ public function setPartials($partials)
289297
*/
290298
public function setupOptions(array $options)
291299
{
292-
return new Options($options);
300+
$obj = new Options($options);
301+
$obj->runtime = $this;
302+
return $obj;
293303
}
294304

295305
private function resolvePartial($partial, &$options)

Diff for: src/VM/VMImpl.php

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ private function setupBuiltins()
8080
if( !isset($this->helpers['helperMissing']) ) {
8181
$this->helpers['helperMissing'] = new \Handlebars\Helper\HelperMissing();
8282
}
83+
if( !isset($this->helpers['log']) ) {
84+
$this->helpers['log'] = new \Handlebars\Helper\Log();
85+
}
8386
if( !isset($this->helpers['lookup']) ) {
8487
$this->helpers['lookup'] = new \Handlebars\Helper\Lookup();
8588
}

Diff for: tests/HandlebarsTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,26 @@ public function testConsecutiveMultilineComments()
9999
$actual = trim($handlebars->render($tmpl));
100100
$this->assertEquals('foo', $actual);
101101
}
102+
103+
public function testLog()
104+
{
105+
$tmpl = '{{log "test"}}';
106+
$logger = new MockLogger();
107+
$handlebars = new Handlebars();
108+
$handlebars->setLogger($logger);
109+
$handlebars->render($tmpl);
110+
$this->assertEquals('info', $logger->logs[0][0]);
111+
$this->assertEquals('test', $logger->logs[0][1]);
112+
}
113+
114+
public function testLogWithNewVM()
115+
{
116+
$tmpl = '{{log "test"}}';
117+
$logger = new MockLogger();
118+
$handlebars = new \Handlebars\VM();
119+
$handlebars->setLogger($logger);
120+
$handlebars->render($tmpl);
121+
$this->assertEquals('info', $logger->logs[0][0]);
122+
$this->assertEquals('string(test)', $logger->logs[0][1]);
123+
}
102124
}

Diff for: tests/MockLogger.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Handlebars\Tests;
4+
5+
use Psr\Log\AbstractLogger;
6+
7+
class MockLogger extends AbstractLogger
8+
{
9+
public $logs = array();
10+
11+
public function log($level, $message, array $context = array())
12+
{
13+
$this->logs[] = array($level, $message, $context);
14+
}
15+
}

0 commit comments

Comments
 (0)