From 9a5288ba4b8241edda394bf61962d0ca7d5faf08 Mon Sep 17 00:00:00 2001 From: Chris Cornutt Date: Wed, 8 Aug 2012 09:20:01 -0500 Subject: [PATCH] updating Template and tests for new injection method --- Shield/Template.php | 34 +++++++++++++++++++++++++--- Shield/tests/Shield/TemplateTest.php | 7 ++++-- Shield/tests/Shield/ViewTest.php | 7 +++++- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Shield/Template.php b/Shield/Template.php index 0d91d90..86eee02 100644 --- a/Shield/Template.php +++ b/Shield/Template.php @@ -4,11 +4,13 @@ class Template extends Base { - private $config = null; + private $config = null; + private $templateDir = null; public function __construct(\Shield\Config $config) { $this->config = $config; + $this->setTemplateDir(); } /** @@ -43,6 +45,33 @@ public function __set($name,$value) $this->_properties[$name] = $value; } + /** + * Get the current templates directory + * + * @return string Full path to templates directory + */ + public function getTemplateDir() + { + return $this->templateDir; + } + + /** + * Set the directory where the templates live + * + * @param string $dir Directory path + * + * @return null + */ + public function setTemplateDir($dir=null) + { + // see if the path is valid + $templatePath = ($dir !== null) ? $dir : __DIR__.'/../app/views'; + + if (realpath($templatePath) !== false) { + $this->templateDir = realpath($templatePath); + } + } + /** * Render the template - either using a file (views/) or as a string * Checks to see if the $template references a file first @@ -54,8 +83,7 @@ public function __set($name,$value) public function render($template) { // first see if what we've been given is a file - $templateFile = $this->di->get('View')->getViewDir(). - '/'.$template.'.php'; + $templateFile = $this->getTemplateDir().'/'.$template.'.php'; if (is_file($templateFile)) { extract($this->_properties); diff --git a/Shield/tests/Shield/TemplateTest.php b/Shield/tests/Shield/TemplateTest.php index fed97f6..d75505c 100644 --- a/Shield/tests/Shield/TemplateTest.php +++ b/Shield/tests/Shield/TemplateTest.php @@ -6,18 +6,21 @@ class TemplateTest extends \PHPUnit_Framework_TestCase { private $_template = null; private $_di = null; + private $_config = null; public function setUp() { $this->_di = new Di(); - $this->_di->register(new View($this->_di)); + $this->_config = new Config($this->_di); + $this->_template = new Template($this->_config); - $this->_template = new Template($this->_di); + $this->_di->register(new View($this->_config, $this->_template)); } public function tearDown() { $this->_di = null; $this->_template = null; + $this->_config = null; } /** diff --git a/Shield/tests/Shield/ViewTest.php b/Shield/tests/Shield/ViewTest.php index ed463a1..8386d5c 100644 --- a/Shield/tests/Shield/ViewTest.php +++ b/Shield/tests/Shield/ViewTest.php @@ -6,16 +6,21 @@ class ViewTest extends \PHPUnit_Framework_TestCase { private $_view = null; private $_di = null; + private $_config = null; public function setUp() { $this->_di = new Di(); - $this->_view = new View($this->_di); + $this->_config = new Config($this->_di); + $template = new Template($this->_config); + + $this->_view = new View($this->_config, $template); } public function tearDown() { $this->_di = null; $this->_view = null; + $this->_config = null; } /**