diff --git a/src/Handlebars/Helpers.php b/src/Handlebars/Helpers.php index 216bcb7..8dfb516 100644 --- a/src/Handlebars/Helpers.php +++ b/src/Handlebars/Helpers.php @@ -106,6 +106,21 @@ class Helpers $this->helpers[$name] = $helper; } + /** + * Add all helpers from the specified collection to the current one. + * + * The method will override helpers from the current collections with same + * named helpers from the specified collection. + * + * @param Helpers $helpers A collection which helpers should be added. + * + * @return void + */ + public function addHelpers(Helpers $helpers) + { + $this->helpers = $helpers->getAll() + $this->helpers; + } + /** * Calls a helper, whether it be a Closure or Helper instance * @@ -238,4 +253,15 @@ class Helpers { return empty($this->helpers); } + + /** + * Returns all helpers from the collection. + * + * @return array Associative array of helpers which keys are helpers names + * and the values are the helpers. + */ + public function getAll() + { + return $this->helpers; + } } diff --git a/tests/Xamin/HandlebarsTest.php b/tests/Xamin/HandlebarsTest.php index c476933..58f60cb 100644 --- a/tests/Xamin/HandlebarsTest.php +++ b/tests/Xamin/HandlebarsTest.php @@ -487,7 +487,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase } /** - * Test add/get/has/clear functions on helper class + * Test add/addHelpers/get/getAll/has/clear functions on helper class */ public function testHelpersClass() { @@ -509,6 +509,14 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase $this->assertFalse($helpers->has('test')); $this->assertFalse(isset($helpers->test)); $this->assertTrue($helpers->isEmpty()); + $helpers->add('test', function() {}); + $this->assertCount(0, array_diff(array_keys($helpers->getAll()), array('test'))); + $extraHelpers = new \Handlebars\Helpers(); + $extraHelpers->add('test', function() {}); + $extraHelpers->add('test2', function() {}); + $helpers->addHelpers($extraHelpers); + $this->assertTrue($helpers->has('test2')); + $this->assertEquals($helpers->test, $extraHelpers->test); } /**