Skip to content

Commit

Permalink
Easy custom template class usage
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed May 25, 2015
1 parent 747e1bb commit c2bf40a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Bootstrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Bootstrapper
'folders' => [],
'section_def_mode' => SectionInterface::MODE_APPEND,
'html_tags_functions' => false,
'template_class' => false
];

/**
Expand Down
10 changes: 6 additions & 4 deletions src/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,13 @@ public function render($template, array $data = [])
*
* @param string $path
* @param array $data
* @param string $class
* @return string
*/
public function renderTemplate($path, array $data = [])
public function renderTemplate($path, array $data = [], $class = null)
{
if (file_exists($path)) {
return $this->doRender($path, $data);
return $this->doRender($path, $data, $class);
}
throw new RuntimeException(__METHOD__.' needs a valid template path as first argument.');
}
Expand All @@ -200,14 +201,15 @@ public function renderTemplate($path, array $data = [])
*
* @param string $path
* @param array $data
* @param null $class
* @return string
*/
private function doRender($path, array $data = [])
private function doRender($path, array $data = [], $class = null)
{
if ($this->status() === self::STATUS_IDLE) {
$this->statusTransitions();
}
$template = $this->stack()->factory($path);
$template = $this->stack()->factory($path, $class);
$this->api()->fire('f.template.render', $template, $data);
$output = trim($template->render($data));
$this->api()->fire('f.template.renderered', $template, $output);
Expand Down
9 changes: 8 additions & 1 deletion src/Template/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class Factory implements APIAware
*/
private $sections;

/**
* @var string
*/
private $defaultClass;

/**
* @param \ArrayAccess $templates
* @param \ArrayAccess $sections
Expand All @@ -58,6 +63,8 @@ public function __construct(AA $templates, AA $sections, API $api, $contract = n
$this->contract = $contract;
$this->templates = $templates;
$this->sections = $sections;
$class = $api->option('template_class');
$this->defaultClass = $class && class_exists($class) ? $class : self::DEFAULT_CLASS;
$this->setAPI($api);
}

Expand Down Expand Up @@ -97,7 +104,7 @@ public function getClass($class)
|| ! class_exists($class)
|| ! in_array(ltrim($this->contract, '\\'), class_implements($class), true)
) {
return self::DEFAULT_CLASS;
return $this->defaultClass;
}

return $class;
Expand Down
52 changes: 42 additions & 10 deletions tests/src/unit/Template/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

use Foil\Tests\TestCase;
use Foil\Template\Factory;
use Foil\API;
use Foil\Tests\API;
use ArrayObject;
use Pimple\Container;
use Mockery;

/**
* @author Giuseppe Mazzapica <[email protected]>
Expand All @@ -21,15 +23,45 @@
*/
class FactoryTest extends TestCase
{
public function testFactory()

public function testFactoryStandardClass()
{
$container = new Container(['options' => ['template_class' => false]]);
$e = new Factory(new ArrayObject(), new ArrayObject(), new API($container));
$instance = $e->factory('one');
assertInstanceOf('Foil\Template\Template', $instance);
}

public function testFactoryCustomDefaultClass()
{
$mock = Mockery::mock('Foil\\Contracts\\TemplateInterface');
$class = get_class($mock);
$container = new Container(['options' => ['template_class' => $class]]);
$e = new Factory(new ArrayObject(), new ArrayObject(), new API($container));
$instance = $e->factory('one');
assertInstanceOf($class, $instance);
}

public function testFactoryCustomClass()
{
$mock = Mockery::mock('Foil\\Contracts\\TemplateInterface');
$class = get_class($mock);
$container = new Container(['options' => ['template_class' => false]]);
$e = new Factory(new ArrayObject(), new ArrayObject(), new API($container));
$instance1 = $e->factory('one', $class);
$instance2 = $e->factory('two');
assertInstanceOf($class, $instance1);
assertInstanceOf('Foil\Template\Template', $instance2);
}

public function testFactorySameInstance()
{
$e = new Factory(new ArrayObject(), new ArrayObject(), new API());
$t_1 = $e->factory('one');
$t_2 = $e->factory('two');
$t_3 = $e->factory('one');
assertInstanceOf('Foil\Template\Template', $t_1);
assertInstanceOf('Foil\Template\Template', $t_2);
assertSame($t_1, $t_3);
assertFalse($t_2 === $t_3);
$container = new Container(['options' => ['template_class' => false]]);
$e = new Factory(new ArrayObject(), new ArrayObject(), new API($container));
$instance1 = $e->factory('one');
$instance2 = $e->factory('two');
$instance3 = $e->factory('one');
assertSame($instance1, $instance3);
assertNotSame($instance1, $instance2);
}
}

0 comments on commit c2bf40a

Please sign in to comment.