Merge pull request #99 from Mibew/helpers_merge

Add ability to merge Helpers collections
This commit is contained in:
Dmitriy S. Simushev 2014-12-15 11:50:27 +03:00
commit b3fcfe1339
2 changed files with 35 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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);
}
/**