fix phpcs and also add mustache.php copyright notice

This commit is contained in:
fzerorubigd 2015-01-29 22:28:38 +03:30
parent ccaa5b1d21
commit 382938b82e
No known key found for this signature in database
GPG Key ID: D6EE858AF9D2999A
2 changed files with 75 additions and 44 deletions

View File

@ -1,5 +1,8 @@
<?php <?php
/** /**
* This file is part of Handlebars-php
* Base on mustache-php https://github.com/bobthecow/mustache.php
*
* Handlebars Inline Template string Loader implementation. * Handlebars Inline Template string Loader implementation.
* *
* With the InlineLoader, templates can be defined at the end of any PHP source * With the InlineLoader, templates can be defined at the end of any PHP source
@ -18,18 +21,40 @@
* Goodbye, cruel {{ planet }} * Goodbye, cruel {{ planet }}
* *
* Templates are deliniated by lines containing only `@@ name`. * Templates are deliniated by lines containing only `@@ name`.
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Hiroyuki Toda <mai7star@gmail.com>
* @copyright 2010-2015 (c) Justin Hileman
* @copyright 2015 (c) fzerorubigd
* @license MIT <http://opensource.org/licenses/MIT>
* @version Release: @package_version@
* @link http://xamin.ir
*/ */
namespace Handlebars\Loader; namespace Handlebars\Loader;
use Handlebars\Loader; use Handlebars\Loader;
use Handlebars\String;
/**
* The inline loader
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Hiroyuki Toda <mai7star@gmail.com>
* @copyright 2010-2015 (c) Justin Hileman
* @copyright 2015 (c) fzerorubigd
* @license MIT <http://opensource.org/licenses/MIT>
* @version Release: @package_version@
* @link http://xamin.ir *
*/
class InlineLoader implements Loader class InlineLoader implements Loader
{ {
protected $_fileName; protected $fileName;
protected $_offset; protected $offset;
protected $_templates; protected $templates;
/** /**
* The InlineLoader requires a filename and offset to process templates. * The InlineLoader requires a filename and offset to process templates.
@ -57,43 +82,45 @@ class InlineLoader implements Loader
throw new \InvalidArgumentException('InlineLoader expects a valid file offset.'); throw new \InvalidArgumentException('InlineLoader expects a valid file offset.');
} }
$this->_fileName = $fileName; $this->fileName = $fileName;
$this->_offset = $offset; $this->offset = $offset;
} }
/** /**
* Load a Template by name. * Load a Template by name.
* *
* @param string $name * @param string $name template name
* *
* @return string Mustache Template source * @return string Handlebars Template source
*/ */
public function load($name) public function load($name)
{ {
$this->loadTemplates(); $this->loadTemplates();
if (!array_key_exists($name, $this->_templates)) { if (!array_key_exists($name, $this->templates)) {
throw new \InvalidArgumentException("Template {$name} not found."); throw new \InvalidArgumentException("Template {$name} not found.");
} }
return $this->_templates[$name]; return $this->templates[$name];
} }
/** /**
* Parse and load templates from the end of a source file. * Parse and load templates from the end of a source file.
*
* @return void
*/ */
protected function loadTemplates() protected function loadTemplates()
{ {
if (!is_null($this->_templates)) { if (!is_null($this->templates)) {
return; return;
} }
$this->_templates = array(); $this->templates = array();
$data = file_get_contents($this->_fileName, false, null, $this->_offset); $data = file_get_contents($this->fileName, false, null, $this->offset);
foreach (preg_split('/^@@(?= [\w\d\.]+$)/m', $data, -1) as $chunk) { foreach (preg_split('/^@@(?= [\w\d\.]+$)/m', $data, -1) as $chunk) {
if (trim($chunk)) { if (trim($chunk)) {
list($name, $content) = explode("\n", $chunk, 2); list($name, $content) = explode("\n", $chunk, 2);
$this->_templates[trim($name)] = trim($content); $this->templates[trim($name)] = trim($content);
} }
} }
} }

View File

@ -16,7 +16,7 @@
*/ */
/** /**
* Class AutoloaderTest * Class HandlebarsTest
*/ */
class HandlebarsTest extends \PHPUnit_Framework_TestCase class HandlebarsTest extends \PHPUnit_Framework_TestCase
{ {
@ -339,7 +339,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
array('data' => array(1, 2, 3, 4)), array('data' => array(1, 2, 3, 4)),
'1234' '1234'
), ),
array( '{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}', array('{{#if first}}The first{{else}}{{#if second}}The second{{/if}}{{/if}}',
array('first' => false, 'second' => true), array('first' => false, 'second' => true),
'The second' 'The second'
) )
@ -352,7 +352,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
public function testHelpersManagement() public function testHelpersManagement()
{ {
$helpers = new \Handlebars\Helpers(array('test' => function () { $helpers = new \Handlebars\Helpers(array('test' => function () {
}), false); }), false);
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers)); $engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
$this->assertTrue(is_callable($engine->getHelper('test'))); $this->assertTrue(is_callable($engine->getHelper('test')));
$this->assertTrue($engine->hasHelper('test')); $this->assertTrue($engine->hasHelper('test'));
@ -509,11 +509,14 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($helpers->has('test')); $this->assertFalse($helpers->has('test'));
$this->assertFalse(isset($helpers->test)); $this->assertFalse(isset($helpers->test));
$this->assertTrue($helpers->isEmpty()); $this->assertTrue($helpers->isEmpty());
$helpers->add('test', function() {}); $helpers->add('test', function () {
});
$this->assertCount(0, array_diff(array_keys($helpers->getAll()), array('test'))); $this->assertCount(0, array_diff(array_keys($helpers->getAll()), array('test')));
$extraHelpers = new \Handlebars\Helpers(); $extraHelpers = new \Handlebars\Helpers();
$extraHelpers->add('test', function() {}); $extraHelpers->add('test', function () {
$extraHelpers->add('test2', function() {}); });
$extraHelpers->add('test2', function () {
});
$helpers->addHelpers($extraHelpers); $helpers->addHelpers($extraHelpers);
$this->assertTrue($helpers->has('test2')); $this->assertTrue($helpers->has('test2'));
$this->assertEquals($helpers->test, $extraHelpers->test); $this->assertEquals($helpers->test, $extraHelpers->test);
@ -1135,10 +1138,10 @@ EOM;
$this->assertEquals("(test)Test.", $engine->render("{{test '(test)'}}", array())); $this->assertEquals("(test)Test.", $engine->render("{{test '(test)'}}", array()));
$this->assertEquals(")Test.Test.", $engine->render("{{test (test ')')}}", array())); $this->assertEquals(")Test.Test.", $engine->render("{{test (test ')')}}", array()));
$engine->addHelper('concat', function(\Handlebars\Template $template,\Handlebars\Context $context, $args) { $engine->addHelper('concat', function (\Handlebars\Template $template, \Handlebars\Context $context, $args) {
$result = ''; $result = '';
foreach($template->parseArguments($args) as $arg) { foreach ($template->parseArguments($args) as $arg) {
$result .= $context->get($arg); $result .= $context->get($arg);
} }
@ -1172,28 +1175,29 @@ EOM;
$args = new \Handlebars\Arguments($argsString); $args = new \Handlebars\Arguments($argsString);
$this->assertEquals($argsString, (string)$args); $this->assertEquals($argsString, (string)$args);
} }
public function stringLiteralsInIfAndUnlessHelpersProvider() { public function stringLiteralsInIfAndUnlessHelpersProvider()
return array( {
return array(
// IfHelper // IfHelper
array('{{#if "truthyString"}}true{{else}}false{{/if}}', array(), "true"), array('{{#if "truthyString"}}true{{else}}false{{/if}}', array(), "true"),
array("{{#if 'truthyStringSingleQuotes'}}true{{else}}false{{/if}}", array(), "true"), array("{{#if 'truthyStringSingleQuotes'}}true{{else}}false{{/if}}", array(), "true"),
array("{{#if ''}}true{{else}}false{{/if}}", array(), "false"), array("{{#if ''}}true{{else}}false{{/if}}", array(), "false"),
array("{{#if '0'}}true{{else}}false{{/if}}", array(), "false"), array("{{#if '0'}}true{{else}}false{{/if}}", array(), "false"),
array("{{#if (add 0 1)}}true{{else}}false{{/if}}", array(), "true"), array("{{#if (add 0 1)}}true{{else}}false{{/if}}", array(), "true"),
array("{{#if (add 1 -1)}}true{{else}}false{{/if}}", array(), "false"), array("{{#if (add 1 -1)}}true{{else}}false{{/if}}", array(), "false"),
// UnlessHelper // UnlessHelper
array('{{#unless "truthyString"}}true{{else}}false{{/unless}}', array(), "false"), array('{{#unless "truthyString"}}true{{else}}false{{/unless}}', array(), "false"),
array("{{#unless 'truthyStringSingleQuotes'}}true{{else}}false{{/unless}}", array(), "false"), array("{{#unless 'truthyStringSingleQuotes'}}true{{else}}false{{/unless}}", array(), "false"),
array("{{#unless ''}}true{{else}}false{{/unless}}", array(), "true"), array("{{#unless ''}}true{{else}}false{{/unless}}", array(), "true"),
array("{{#unless '0'}}true{{else}}false{{/unless}}", array(), "true"), array("{{#unless '0'}}true{{else}}false{{/unless}}", array(), "true"),
array("{{#unless (add 0 1)}}true{{else}}false{{/unless}}", array(), "false"), array("{{#unless (add 0 1)}}true{{else}}false{{/unless}}", array(), "false"),
array("{{#unless (add 1 -1)}}true{{else}}false{{/unless}}", array(), "true"), array("{{#unless (add 1 -1)}}true{{else}}false{{/unless}}", array(), "true"),
); );
} }
/** /**
* Test string literals in the context of if and unless helpers * Test string literals in the context of if and unless helpers
* *
* @param string $template template text * @param string $template template text
@ -1207,21 +1211,21 @@ EOM;
public function testStringLiteralsInIfAndUnlessHelpers($template, $data, $results) public function testStringLiteralsInIfAndUnlessHelpers($template, $data, $results)
{ {
$engine = new \Handlebars\Handlebars(); $engine = new \Handlebars\Handlebars();
$engine->addHelper('add', function ($template, $context, $args) { $engine->addHelper('add', function ($template, $context, $args) {
$sum = 0; $sum = 0;
foreach ($template->parseArguments($args) as $value) { foreach ($template->parseArguments($args) as $value) {
$sum += intval($context->get($value)); $sum += intval($context->get($value));
} }
return $sum; return $sum;
}); });
$res = $engine->render($template, $data); $res = $engine->render($template, $data);
$this->assertEquals($res, $results); $this->assertEquals($res, $results);
} }
} }
/** /**