From 3b2e997fb815176b546fa56ce04c3a88aa6d25e6 Mon Sep 17 00:00:00 2001 From: Joao Da Silva Date: Tue, 5 Apr 2011 19:32:58 +0200 Subject: [PATCH 1/3] searchpath now accepts an array, need to add the tests --- h2o.php | 11 +++++---- h2o/loaders.php | 62 +++++++++++++++++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/h2o.php b/h2o.php index 12d93ed..6ea94ed 100644 --- a/h2o.php +++ b/h2o.php @@ -63,13 +63,14 @@ function __construct($file = null, $options = array()) { if (!class_exists($loader, false)) throw new Exception('Invalid template loader'); - if (isset($options['searchpath'])) - $this->searchpath = realpath($options['searchpath']).DS; - elseif ($file) + if (isset($options['searchpath'])){ + $this->searchpath = $options['searchpath']; + } else if ($file) { $this->searchpath = dirname(realpath($file)).DS; - else + } else { $this->searchpath = getcwd().DS; - + } + $this->loader = new $loader($this->searchpath, $this->options); } $this->loader->runtime = $this; diff --git a/h2o/loaders.php b/h2o/loaders.php index 055426d..30dc5b8 100644 --- a/h2o/loaders.php +++ b/h2o/loaders.php @@ -18,14 +18,19 @@ function cache_read($file, $object, $ttl = 3600) {} class H2o_File_Loader extends H2o_Loader { function __construct($searchpath, $options = array()) { - if (is_file($searchpath)) { - $searthpath = dirname($searchpath).DS; - } - if (!is_dir($searchpath)) - throw new TemplateNotFound($filename); - - $this->searchpath = realpath($searchpath) . DS; - $this->setOptions($options); + // if (is_file($searchpath)) { + // $searthpath = dirname($searchpath).DS; + // } + // if (!is_dir($searchpath)) + // throw new TemplateNotFound($filename); + // + + if (!is_array($searchpath)) + throw new Exception("searchpath must be an array"); + + + $this->searchpath = (array) $searchpath; + $this->setOptions($options); } function setOptions($options = array()) { @@ -35,8 +40,9 @@ function setOptions($options = array()) { } function read($filename) { + if (!is_file($filename)) - $filename = $this->searchpath . $filename; + $filename = $this->get_template_path($this->searchpath,$filename); if (is_file($filename)) { $source = file_get_contents($filename); @@ -46,14 +52,40 @@ function read($filename) { } } - function read_cache($filename) { - if (!$this->cache) - return $this->read($filename); + function get_template_path($search_path, $filename){ - if (!is_file($filename)) - $filename = $this->searchpath . $filename; + + for ($i=0 ; $i < count($search_path) ; $i++) + { + + if(file_exists($search_path[$i] . $filename)) { + $filename = $search_path[$i] . $filename; + return $filename; + break; + } else { + continue; + } + } + + throw new Exception('TemplateNotFound - Looked for template: ' . $filename); + + + + } + + function read_cache($filename) { + if (!$this->cache){ + $filename = $this->get_template_path($this->searchpath,$filename); + return $this->read($filename); + } + + if (!is_file($filename)){ + $filename = $this->get_template_path($this->searchpath,$filename); + } + $filename = realpath($filename); + $cache = md5($filename); $object = $this->cache->read($cache); $this->cached = $object && !$this->expired($object); @@ -86,7 +118,7 @@ function expired($object) { $files = array_merge(array($object->filename), $object->templates); foreach ($files as $file) { if (!is_file($file)) - $file = $this->searchpath.$file; + $file = $this->get_template_path($this->searchpath,$filename); if ($object->created < filemtime($file)) return true; From 5c57fbf4e54129393f636c67df141c3a25fb33cc Mon Sep 17 00:00:00 2001 From: Joao Da Silva Date: Tue, 5 Apr 2011 23:46:09 +0200 Subject: [PATCH 2/3] Added tests and documentation on README, h2o now accepts multiple paths on the searchpath option --- README.md | 6 ++-- h2o.php | 6 ++-- h2o/loaders.php | 81 +++++++++++++++++++++----------------------- spec/loader_spec.php | 57 +++++++++++++++++++++++-------- spec/spec_helper.php | 2 +- 5 files changed, 90 insertions(+), 62 deletions(-) diff --git a/README.md b/README.md index 2fb3146..c5920e5 100644 --- a/README.md +++ b/README.md @@ -312,9 +312,11 @@ default: this will be the base path of your template h2o use this path to load additional templates and extensions. -You can either explicity set the search path +You can either explicity set the search path which accepts an array of path(s) -`$template = new H2o('index.html', array('searchpath' => '/sites/common_templates'));` +`$paths = array( '/your/path/1', '/your/path/2');` + +`$template = new H2o('index.html', array('searchpath' => $paths));` or It will try to find the searchpath for you diff --git a/h2o.php b/h2o.php index 6ea94ed..eef1ae3 100644 --- a/h2o.php +++ b/h2o.php @@ -35,7 +35,7 @@ function getOptions($options = array()) { 'cache_ttl' => 3600, // file | apc | memcache 'searchpath' => false, 'autoescape' => true, - + // Enviroment setting 'BLOCK_START' => '{%', 'BLOCK_END' => '%}', @@ -64,7 +64,9 @@ function __construct($file = null, $options = array()) { throw new Exception('Invalid template loader'); if (isset($options['searchpath'])){ - $this->searchpath = $options['searchpath']; + $this->searchpath = is_array($options['searchpath']) ? + $options['searchpath'] : + realpath($options['searchpath']).DS ; } else if ($file) { $this->searchpath = dirname(realpath($file)).DS; } else { diff --git a/h2o/loaders.php b/h2o/loaders.php index 30dc5b8..7ee45f8 100644 --- a/h2o/loaders.php +++ b/h2o/loaders.php @@ -18,18 +18,17 @@ function cache_read($file, $object, $ttl = 3600) {} class H2o_File_Loader extends H2o_Loader { function __construct($searchpath, $options = array()) { - // if (is_file($searchpath)) { - // $searthpath = dirname($searchpath).DS; - // } - // if (!is_dir($searchpath)) - // throw new TemplateNotFound($filename); - // - - if (!is_array($searchpath)) - throw new Exception("searchpath must be an array"); - + + if (!is_array($searchpath) && is_string($search_path)){ + if (is_file($searchpath)) { + $searthpath = dirname($searchpath).DS; + } + + if (!is_dir($searchpath)) + throw new TemplateNotFound($filename); + } - $this->searchpath = (array) $searchpath; + $this->searchpath = $searchpath; $this->setOptions($options); } @@ -39,11 +38,10 @@ function setOptions($options = array()) { } } - function read($filename) { - + function read($filename) { if (!is_file($filename)) - $filename = $this->get_template_path($this->searchpath,$filename); - + $filename = $this->get_template_path($this->searchpath,$filename); + if (is_file($filename)) { $source = file_get_contents($filename); return $this->runtime->parse($source); @@ -52,37 +50,33 @@ function read($filename) { } } - function get_template_path($search_path, $filename){ - - - for ($i=0 ; $i < count($search_path) ; $i++) - { - - if(file_exists($search_path[$i] . $filename)) { - $filename = $search_path[$i] . $filename; - return $filename; - break; - } else { - continue; - } - - } - - throw new Exception('TemplateNotFound - Looked for template: ' . $filename); - - - + function get_template_path($search_path, $filename){ + if(is_array($search_path)){ + for ( $i = 0 ; $i < count($search_path); $i++ ) { + if(file_exists($search_path[$i] . $filename)) { + $filename = $search_path[$i] . $filename; + return $filename; + break; + } else { + continue; + } + } + throw new TemplateNotFound($filename); + } else { + return $search_path . $filename; + } } function read_cache($filename) { if (!$this->cache){ - $filename = $this->get_template_path($this->searchpath,$filename); + $filename = is_array($this->searchpath) ? + $this->get_template_path($this->searchpath,$filename) : + $this->read($filename) ; return $this->read($filename); } - if (!is_file($filename)){ + if (!is_file($filename)) $filename = $this->get_template_path($this->searchpath,$filename); - } $filename = realpath($filename); @@ -114,14 +108,15 @@ function flush_cache() { function expired($object) { if (!$object) return false; - + $files = array_merge(array($object->filename), $object->templates); + foreach ($files as $file) { - if (!is_file($file)) - $file = $this->get_template_path($this->searchpath,$filename); - + if (!is_file($file)) + $file = $this->get_template_path($this->searchpath,$file); + if ($object->created < filemtime($file)) - return true; + return true; } return false; } diff --git a/spec/loader_spec.php b/spec/loader_spec.php index 3b50bc3..07ec9bc 100644 --- a/spec/loader_spec.php +++ b/spec/loader_spec.php @@ -4,23 +4,44 @@ class Describe_file_loader extends SimpleSpec { function should_be_able_to_read_template() { - chdir(dirname(__FILE__)); - - $h2o = h2o('templates/a.html'); - expects($h2o->nodelist)->should_be_a('Nodelist'); - - $h2o = h2o('templates/b.html'); - expects($h2o->nodelist)->should_be_a('Nodelist'); - - $h2o = h2o('templates/emails/base.html'); - expects($h2o->nodelist)->should_be_a('Nodelist'); - + chdir(dirname(__FILE__)); + + $h2o = h2o('templates/a.html'); + expects($h2o->nodelist)->should_be_a('Nodelist'); + + $h2o = h2o('templates/b.html'); + expects($h2o->nodelist)->should_be_a('Nodelist'); + + $h2o = h2o('templates/emails/base.html'); + expects($h2o->nodelist)->should_be_a('Nodelist'); } + function should_be_able_to_read_template_from_search_path_array() { + + $paths = array(dirname(__FILE__).DS.'templates'.DS); + + $h2o = h2o('templates/a.html', array('searchpath' => $paths)); + expects($h2o->nodelist)->should_be_a('Nodelist'); + + $h2o = h2o('templates/b.html', array('searchpath' => $paths) ); + expects($h2o->nodelist)->should_be_a('Nodelist'); + + $h2o = h2o('templates/emails/base.html', array('searchpath' => $paths)); + expects($h2o->nodelist)->should_be_a('Nodelist'); + } + + function should_be_able_to_load_template_lazily() { + chdir(dirname(__FILE__)); + + $paths = array(dirname(__FILE__).DS.'templates'.DS); + + $h2o = h2o('a.html', array('searchpath' => $paths)); + expects($h2o->render())->should_not_be_empty(); + $h2o = new H2o('a.html', array('searchpath' => 'templates')); expects($h2o->render())->should_not_be_empty(); - + $h2o = new H2o(null, array('searchpath' => 'templates')); $h2o->loadTemplate('b.html'); expects($h2o->render())->should_not_be_empty(); @@ -34,11 +55,18 @@ function should_read_from_alternitive_working_path() { } function should_load_subtemplate_upon_extends_tag() { + $paths = array(dirname(__FILE__).DS.'templates'.DS); + $h2o = h2o('emails/campaign1.html', array( 'searchpath' => dirname(__FILE__).DS.'templates' + )); + expects($h2o->render())->should_match('/Dear Customer/'); + + $h2o = h2o('emails/campaign1.html', array( + 'searchpath' => $paths )); expects($h2o->render())->should_match('/Dear Customer/'); - + $h2o->loadTemplate('emails/campaign2.html'); expects($h2o->render())->should_match('/Hello Customer/'); } @@ -59,7 +87,8 @@ function shouble_cache_main_template() { } function should_invalidate_cache_if_any_subtemplates_has_updated() { - $opt = array('searchpath' => dirname(__FILE__).DS.'templates'); + + $opt = array('searchpath' => dirname(__FILE__).DS.'templates'.DS); # Load template twice to make sure its cached $h2o = h2o('emails/campaign1.html', $opt); diff --git a/spec/spec_helper.php b/spec/spec_helper.php index 3367745..d4da44f 100644 --- a/spec/spec_helper.php +++ b/spec/spec_helper.php @@ -1,6 +1,6 @@ Date: Wed, 6 Apr 2011 10:08:04 +0200 Subject: [PATCH 3/3] updated the readme --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c5920e5..0889e58 100644 --- a/README.md +++ b/README.md @@ -312,12 +312,20 @@ default: this will be the base path of your template h2o use this path to load additional templates and extensions. -You can either explicity set the search path which accepts an array of path(s) +You can either explicity set the search path which accepts a string with a single path or an array of path(s). -`$paths = array( '/your/path/1', '/your/path/2');` +All paths should terminate with a backslash `/`. + +Array of paths: + +`$paths = array( '/your/path/1/', '/your/path/2/');` `$template = new H2o('index.html', array('searchpath' => $paths));` +or a single string, + +`$template = new H2o('index.html', array('searchpath' => '/your/unique/path/'));` + or It will try to find the searchpath for you `$template = new H2o('/sites/common_templates/index.html');`