diff --git a/composer.json b/composer.json index 50c4f00..583fccf 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "xamin/handlebars.php", + "name": "virtuecenter/handlebars.php", "description": "Handlebars processor for php", "homepage": "https://github.com/XaminProject/handlebars.php", "type": "library", @@ -10,10 +10,8 @@ "email": "fzerorubigd@gmail.com" } ], - "require": { - - }, + "require": {}, "autoload": { "psr-0": { "Handlebars": "src/" } } -} +} \ No newline at end of file diff --git a/src/Handlebars/Autoloader.php b/src/Handlebars/Autoloader.php index b70e77d..7d71136 100644 --- a/src/Handlebars/Autoloader.php +++ b/src/Handlebars/Autoloader.php @@ -28,7 +28,9 @@ * @version Release: @package_version@ * @link http://xamin.ir */ -class Handlebars_Autoloader +namespace Handlebars; + +class Autoloader { private $_baseDir; diff --git a/src/Handlebars/Cache.php b/src/Handlebars/Cache.php index ea4efa7..de2a3bc 100644 --- a/src/Handlebars/Cache.php +++ b/src/Handlebars/Cache.php @@ -29,7 +29,9 @@ * @link http://xamin.ir */ -interface Handlebars_Cache +namespace Handlebars; + +interface Cache { /** * Get cache for $name if exist. diff --git a/src/Handlebars/Cache/APC.php b/src/Handlebars/Cache/APC.php index e9af1d1..2446586 100644 --- a/src/Handlebars/Cache/APC.php +++ b/src/Handlebars/Cache/APC.php @@ -26,8 +26,9 @@ * @version Release: @package_version@ * @link http://xamin.ir */ - -class Handlebars_Cache_APC implements Handlebars_Cache +namespace Handlebars\Cache; + +class APC implements Cache { private $_cache = array(); diff --git a/src/Handlebars/Cache/Dummy.php b/src/Handlebars/Cache/Dummy.php index 6095b33..e1107be 100644 --- a/src/Handlebars/Cache/Dummy.php +++ b/src/Handlebars/Cache/Dummy.php @@ -27,7 +27,9 @@ * @link http://xamin.ir */ -class Handlebars_Cache_Dummy implements Handlebars_Cache +namespace Handlebars\Cache; +use Handlebars\Cache; +class Dummy implements Cache { private $_cache = array(); diff --git a/src/Handlebars/Context.php b/src/Handlebars/Context.php index cec9b78..7668b6a 100644 --- a/src/Handlebars/Context.php +++ b/src/Handlebars/Context.php @@ -28,7 +28,9 @@ * @version Release: @package_version@ * @link http://xamin.ir */ -class Handlebars_Context +namespace Handlebars; + +class Context { /** * @var array stack for context only top stack is available @@ -115,7 +117,7 @@ class Handlebars_Context } if (count($this->stack) < $level) { if ($strict) { - throw new InvalidArgumentException('can not find variable in context'); + throw new \InvalidArgumentException('can not find variable in context'); } return ''; } @@ -127,7 +129,7 @@ class Handlebars_Context $current = current($this->stack); if (!$variableName) { if ($strict) { - throw new InvalidArgumentException('can not find variable in context'); + throw new \InvalidArgumentException('can not find variable in context'); } return ''; } elseif ($variableName == '.' || $variableName == 'this') { @@ -172,7 +174,7 @@ class Handlebars_Context } elseif ($inside === '.') { $value = $variable; } elseif ($strict) { - throw new InvalidArgumentException('can not find variable in context'); + throw new \InvalidArgumentException('can not find variable in context'); } return $value; } diff --git a/src/Handlebars/Handlebars.php b/src/Handlebars/Handlebars.php new file mode 100644 index 0000000..7805ea8 --- /dev/null +++ b/src/Handlebars/Handlebars.php @@ -0,0 +1,492 @@ + + * @copyright 2012 (c) ParsPooyesh Co + * @license GPLv3 + * @version GIT: $Id$ + * @link http://xamin.ir + */ + + +/** + * Handlebars parser (infact its a mustache parser) + * This class is responsible for turning raw template source into a set of Mustache tokens. + * + * @category Xamin + * @package Handlebars + * @author fzerorubigd + * @copyright 2012 (c) ParsPooyesh Co + * @license GPLv3 + * @version Release: @package_version@ + * @link http://xamin.ir + */ +namespace Handlebars; +use Handlebars\Loader\StringLoader; +use Handlebars\Cache\Dummy; + +class Handlebars +{ + const VERSION = '1.0.0'; + + /** + * @var Tokenizer + */ + private $_tokenizer; + + /** + * @var Parser + */ + private $_parser; + /** + * @var Helpers + */ + private $_helpers; + + /** + * @var Loader + */ + private $_loader; + + /** + * @var Loader + */ + private $_partialsLoader; + + /** + * @var Cache + */ + private $_cache; + /** + * @var callable escape function to use + */ + private $_escape = 'htmlspecialchars'; + + /** + * @var array parametes to pass to escape function, script prepend string to this array + */ + private $_escapeArgs = array ( + ENT_COMPAT, + 'UTF-8' + ); + + private $_aliases = array(); + /** + * Handlebars engine constructor + * $options array can contain : + * helpers => Helpers object + * escape => a callable function to escape values + * escapeArgs => array to pass as extra parameter to escape function + * loader => Loader object + * partials_loader => Loader object + * cache => Cache object + * + * @param array $options array of options to set + */ + public function __construct(array $options = array()) + { + if (isset($options['helpers'])) { + $this->setHelpers($options['helpers']); + } + + if (isset($options['loader'])) { + $this->setLoader($options['loader']); + } + + if (isset($options['partials_loader'])) { + $this->setPartialsLoader($options['partials_loader']); + } + + if (isset($options['cache'])) { + $this->setCache($options['cache']); + } + + if (isset($options['escape'])) { + if (!is_callable($options['escape'])) { + throw new \InvalidArgumentException('Handlebars Constructor "escape" option must be callable'); + } + + $this->_escape = $options['escape']; + } + + if (isset($options['escapeArgs'])) { + if (!is_array($options['escapeArgs'])) { + $options['escapeArgs'] = array($options['escapeArgs']); + } + $this->_escapeArgs = $options['escapeArgs']; + } + + if (isset($options['partials_alias']) + && is_array($options['partials_alias']) + ) { + $this->_aliases = $options['partials_alias']; + } + } + + + /** + * Shortcut 'render' invocation. + * + * Equivalent to calling `$handlebars->loadTemplate($template)->render($data);` + * + * @param string $template template name + * @param mixed $data data to use as context + * + * @return string Rendered template + * @see Handlebars::loadTemplate + * @see Template::render + */ + public function render($template, $data) + { + return $this->loadTemplate($template)->render($data); + } + + /** + * Set helpers for current enfine + * + * @param Helpers $helpers handlebars helper + * + * @return void + */ + public function setHelpers(Helpers $helpers) + { + $this->_helpers = $helpers; + } + + /** + * Get helpers, or create new one if ther is no helper + * + * @return Helpers + */ + public function getHelpers() + { + if (!isset($this->_helpers)) { + $this->_helpers = new Helpers(); + } + return $this->_helpers; + } + + /** + * Add a new helper. + * + * @param string $name helper name + * @param mixed $helper helper callable + * + * @return void + */ + public function addHelper($name, $helper) + { + $this->getHelpers()->add($name, $helper); + } + + /** + * Get a helper by name. + * + * @param string $name helper name + * + * @return callable Helper + */ + public function getHelper($name) + { + return $this->getHelpers()->get($name); + } + + /** + * Check whether this instance has a helper. + * + * @param string $name helper name + * + * @return boolean True if the helper is present + */ + public function hasHelper($name) + { + return $this->getHelpers()->has($name); + } + + /** + * Remove a helper by name. + * + * @param string $name helper name + * + * @return void + */ + public function removeHelper($name) + { + $this->getHelpers()->remove($name); + } + + /** + * Set current loader + * + * @param Loader $loader handlebars loader + * + * @return void + */ + public function setLoader(Loader $loader) + { + $this->_loader = $loader; + } + + /** + * get current loader + * + * @return Loader + */ + public function getLoader() + { + if (!isset($this->_loader)) { + $this->_loader = new StringLoader(); + } + return $this->_loader; + } + + /** + * Set current partials loader + * + * @param Loader $loader handlebars loader + * + * @return void + */ + public function setPartialsLoader(Loader $loader) + { + $this->_partialsLoader = $loader; + } + + /** + * get current partials loader + * + * @return Loader + */ + public function getPartialsLoader() + { + if (!isset($this->_partialsLoader)) { + $this->_partialsLoader = new StringLoader(); + } + return $this->_partialsLoader; + } + + /** + * Set cache for current engine + * + * @param Cache $cache handlebars cache + * + * @return void + */ + public function setCache(Cache $cache) + { + $this->_cache = $cache; + } + + /** + * Get cache + * + * @return Cache + */ + public function getCache() + { + if (!isset($this->_cache)) { + $this->_cache = new Dummy(); + } + return $this->_cache; + } + /** + * Get current escape function + * + * @return callable + */ + public function getEscape() + { + return $this->_escape; + } + + /** + * Set current escpae function + * + * @param callable $escape function + * + * @return void + */ + public function setEscape($escape) + { + if (!is_callable($escape)) { + throw new \InvalidArgumentException('Escape function must be a callable'); + } + $this->_escape = $escape; + } + + /** + * Get current escape function + * + * @return callable + */ + public function getEscapeArgs() + { + return $this->_escapeArgs; + } + + /** + * Set current escpae function + * + * @param array $escapeArgs arguments to pass as extra arg to function + * + * @return void + */ + public function setEscapeArgs($escapeArgs) + { + if (!is_array($escapeArgs)) { + $escapeArgs = array($escapeArgs); + } + $this->_escapeArgs = $escapeArgs; + } + + + /** + * Set the Handlebars Tokenizer instance. + * + * @param Tokenizer $tokenizer tokenizer + * + * @return void + */ + public function setTokenizer(Tokenizer $tokenizer) + { + $this->_tokenizer = $tokenizer; + } + + /** + * Get the current Handlebars Tokenizer instance. + * + * If no Tokenizer instance has been explicitly specified, this method will instantiate and return a new one. + * + * @return Tokenizer + */ + public function getTokenizer() + { + if (!isset($this->_tokenizer)) { + $this->_tokenizer = new Tokenizer(); + } + + return $this->_tokenizer; + } + /** + * Set the Handlebars Parser instance. + * + * @param Parser $parser parser object + * + * @return void + */ + public function setParser(Parser $parser) + { + $this->_parser = $parser; + } + + /** + * Get the current Handlebars Parser instance. + * + * If no Parser instance has been explicitly specified, this method will instantiate and return a new one. + * + * @return Parser + */ + public function getParser() + { + if (!isset($this->_parser)) { + $this->_parser = new Parser(); + } + + return $this->_parser; + } + /** + * Load a template by name with current template loader + * + * @param string $name template name + * + * @return Template + */ + public function loadTemplate($name) + { + $source = $this->getLoader()->load($name); + $tree = $this->_tokenize($source); + return new Template($this, $tree, $source); + } + + /** + * Load a partial by name with current partial loader + * + * @param string $name partial name + * + * @return Template + */ + public function loadPartial($name) + { + if (isset($this->_aliases[$name])) { + $name = $this->_aliases[$name]; + } + $source = $this->getPartialsLoader()->load($name); + $tree = $this->_tokenize($source); + return new Template($this, $tree, $source); + } + + /** + * Register partial alias + * + * @param string $alias Partial alias + * @param string $content The real value + * + * @return void + */ + public function registerPartial($alias, $content) + { + $this->_aliases[$alias] = $content; + } + + /** + * Un-register partial alias + * + * @param string $alias Partial alias + * + * @return void + */ + public function unRegisterPartial($alias) + { + if (isset($this->_aliases[$alias])) { + unset($this->_aliases[$alias]); + } + } + + /** + * Load string into a template object + * + * @param string $source string to load + * + * @return Template + */ + public function loadString($source) + { + $tree = $this->_tokenize($source); + return new Template($this, $tree, $source); + } + + /** + * try to tokenize source, or get them from cache if available + * + * @param string $source handlebars source code + * + * @return array handlebars parsed data into array + */ + private function _tokenize($source) + { + $hash = md5(sprintf('version: %s, data : %s', self::VERSION, $source)); + $tree = $this->getCache()->get($hash); + if ($tree === false) { + $tokens = $this->getTokenizer()->scan($source); + $tree = $this->getParser()->parse($tokens); + $this->getCache()->set($hash, $tree); + } + return $tree; + } +} \ No newline at end of file diff --git a/src/Handlebars/Helpers.php b/src/Handlebars/Helpers.php index a0396e3..7678fc8 100644 --- a/src/Handlebars/Helpers.php +++ b/src/Handlebars/Helpers.php @@ -29,8 +29,9 @@ * @version Release: @package_version@ * @link http://xamin.ir */ +namespace Handlebars; -class Handlebars_Helpers +class Helpers { /** * @var array array of helpers @@ -52,7 +53,7 @@ class Handlebars_Helpers } if ($helpers != null) { if (!is_array($helpers) && !$helpers instanceof Traversable) { - throw new InvalidArgumentException('HelperCollection constructor expects an array of helpers'); + throw new \InvalidArgumentException('HelperCollection constructor expects an array of helpers'); } foreach ($helpers as $name => $helper) { $this->add($name, $helpers); @@ -176,28 +177,28 @@ class Handlebars_Helpers { $this->add( 'if', - array('Handlebars_Helpers', '_helper_if') + array('Helpers', '_helper_if') ); $this->add( 'each', - array('Handlebars_Helpers', '_helper_each') + array('Helpers', '_helper_each') ); $this->add( 'unless', - array('Handlebars_Helpers', '_helper_unless') + array('Helpers', '_helper_unless') ); $this->add( 'with', - array('Handlebars_Helpers', '_helper_with') + array('Helpers', '_helper_with') ); //Just for compatibility with ember $this->add( 'bindAttr', - array('Handlebars_Helpers', '_helper_bindAttr') + array('Helpers', '_helper_bindAttr') ); } @@ -213,7 +214,7 @@ class Handlebars_Helpers public function add($name ,$helper) { if (!is_callable($helper)) { - throw new InvalidArgumentException("$name Helper is not a callable."); + throw new \InvalidArgumentException("$name Helper is not a callable."); } $this->helpers[$name] = $helper; } @@ -241,7 +242,7 @@ class Handlebars_Helpers public function __get($name) { if (!$this->has($name)) { - throw new InvalidArgumentException('Unknow helper :' . $name); + throw new \InvalidArgumentException('Unknow helper :' . $name); } return $this->helpers[$name]; } @@ -297,7 +298,7 @@ class Handlebars_Helpers public function remove($name) { if (!$this->has($name)) { - throw new InvalidArgumentException('Unknown helper: ' . $name); + throw new \InvalidArgumentException('Unknown helper: ' . $name); } unset($this->helpers[$name]); diff --git a/src/Handlebars/Loader.php b/src/Handlebars/Loader.php index 91c7c0e..9c6ca1d 100644 --- a/src/Handlebars/Loader.php +++ b/src/Handlebars/Loader.php @@ -26,7 +26,9 @@ * @version Release: @package_version@ * @link http://xamin.ir */ -interface Handlebars_Loader +namespace Handlebars; + +interface Loader { /** @@ -34,7 +36,7 @@ interface Handlebars_Loader * * @param string $name template name to load * - * @return Handlebars_String + * @return String */ public function load($name); } diff --git a/src/Handlebars/Loader/FilesystemLoader.php b/src/Handlebars/Loader/FilesystemLoader.php index 93c9212..0be1004 100644 --- a/src/Handlebars/Loader/FilesystemLoader.php +++ b/src/Handlebars/Loader/FilesystemLoader.php @@ -27,7 +27,10 @@ * @link http://xamin.ir * * @implements Loader */ -class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader +namespace Handlebars\Loader; +use Handlesbars\String; + +class FilesystemLoader implements Loader { private $_baseDir; private $_extension = '.handlebars'; @@ -63,7 +66,7 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader foreach ($this->_baseDir as $dir) { if (!is_dir($dir)) { - throw new RuntimeException('FilesystemLoader baseDir must be a directory: ' . $dir); + throw new \RuntimeException('FilesystemLoader baseDir must be a directory: ' . $dir); } } @@ -84,14 +87,14 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader * * @param string $name template name * - * @return Handlebars_String Handlebars Template source + * @return String Handlebars Template source */ public function load($name) { if (!isset($this->_templates[$name])) { $this->_templates[$name] = $this->loadFile($name); } - return new Handlebars_String($this->_templates[$name]); + return new String($this->_templates[$name]); } /** @@ -107,7 +110,7 @@ class Handlebars_Loader_FilesystemLoader implements Handlebars_Loader $fileName = $this->getFileName($name); if ($fileName === false) { - throw new InvalidArgumentException('Template ' . $name . ' not found.'); + throw new \InvalidArgumentException('Template ' . $name . ' not found.'); } return file_get_contents($fileName); diff --git a/src/Handlebars/Loader/StringLoader.php b/src/Handlebars/Loader/StringLoader.php index def3f8a..19754be 100644 --- a/src/Handlebars/Loader/StringLoader.php +++ b/src/Handlebars/Loader/StringLoader.php @@ -26,7 +26,11 @@ * @link http://xamin.ir * * @implements Loader */ -class Handlebars_Loader_StringLoader implements Handlebars_Loader +namespace Handlebars\Loader; +use Handlebars\Loader; +use Handlebars\String; + +class StringLoader implements Loader { /** @@ -34,10 +38,10 @@ class Handlebars_Loader_StringLoader implements Handlebars_Loader * * @param string $name Handlebars Template source * - * @return Handlebars_string Handlebars Template source + * @return String Handlebars Template source */ public function load($name) { - return new Handlebars_String($name); + return new String($name); } } diff --git a/src/Handlebars/Parser.php b/src/Handlebars/Parser.php index df9e15b..dbeacf9 100644 --- a/src/Handlebars/Parser.php +++ b/src/Handlebars/Parser.php @@ -27,8 +27,9 @@ * @version Release: @package_version@ * @link http://xamin.ir */ +namespace Handlebars; -class Handlebars_Parser +class Parser { /** * Process array of tokens and convert them into parse tree @@ -39,7 +40,7 @@ class Handlebars_Parser */ public function parse(array $tokens = array()) { - return $this->_buildTree(new ArrayIterator($tokens)); + return $this->_buildTree(new \ArrayIterator($tokens)); } /** @@ -62,22 +63,22 @@ class Handlebars_Parser if ($token === null) { continue; } else { - switch ($token[Handlebars_Tokenizer::TYPE]) { - case Handlebars_Tokenizer::T_END_SECTION: + switch ($token[Tokenizer::TYPE]) { + case Tokenizer::T_END_SECTION: $newNodes = array (); $continue = true; do { $result = array_pop($stack); if ($result === null) { - throw new LogicException('Unexpected closing tag: /'. $token[Handlebars_Tokenizer::NAME]); + throw new \LogicException('Unexpected closing tag: /'. $token[Tokenizer::NAME]); } - if (!array_key_exists(Handlebars_Tokenizer::NODES, $result) - && isset($result[Handlebars_Tokenizer::NAME]) - && $result[Handlebars_Tokenizer::NAME] == $token[Handlebars_Tokenizer::NAME] + if (!array_key_exists(Tokenizer::NODES, $result) + && isset($result[Tokenizer::NAME]) + && $result[Tokenizer::NAME] == $token[Tokenizer::NAME] ) { - $result[Handlebars_Tokenizer::NODES] = $newNodes; - $result[Handlebars_Tokenizer::END] = $token[Handlebars_Tokenizer::INDEX]; + $result[Tokenizer::NODES] = $newNodes; + $result[Tokenizer::END] = $token[Tokenizer::INDEX]; array_push($stack, $result); break 2; } else { diff --git a/src/Handlebars/String.php b/src/Handlebars/String.php index 3cb321e..fab8696 100644 --- a/src/Handlebars/String.php +++ b/src/Handlebars/String.php @@ -25,8 +25,9 @@ * @version Release: @package_version@ * @link http://xamin.ir */ +namespace Handlebars; -class Handlebars_String +class String { private $_string; diff --git a/src/Handlebars/Template.php b/src/Handlebars/Template.php index 25a176d..615b4be 100644 --- a/src/Handlebars/Template.php +++ b/src/Handlebars/Template.php @@ -27,11 +27,12 @@ * @version Release: @package_version@ * @link http://xamin.ir */ +namespace Handlebars; -class Handlebars_Template +class Template { /** - * @var Handlebars_Engine + * @var Handlebars */ protected $handlebars; @@ -48,11 +49,11 @@ class Handlebars_Template /** * Handlebars template constructor * - * @param Handlebars_Engine $engine handlebar engine + * @param Handlebars $engine handlebar engine * @param array $tree Parsed tree * @param string $source Handlebars source */ - public function __construct(Handlebars_Engine $engine, $tree, $source) + public function __construct(Handlebars $engine, $tree, $source) { $this->handlebars = $engine; $this->tree = $tree; @@ -83,7 +84,7 @@ class Handlebars_Template /** * Get current engine associated with this object * - * @return Handlebars_Engine + * @return Handlebars */ public function getEngine() { @@ -125,8 +126,8 @@ class Handlebars_Template */ public function render($context) { - if (!$context instanceof Handlebars_Context) { - $context = new Handlebars_Context($context); + if (!$context instanceof Context) { + $context = new Context($context); } $topTree = end($this->_stack); //This method (render) never pop a value from stack list($index ,$tree, $stop) = $topTree; @@ -137,43 +138,43 @@ class Handlebars_Template $index++; //if the section is exactly like waitFor if (is_string($stop) - && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED - && $current[Handlebars_Tokenizer::NAME] === $stop + && $current[Tokenizer::TYPE] == Tokenizer::T_ESCAPED + && $current[Tokenizer::NAME] === $stop ) { break; } - switch ($current[Handlebars_Tokenizer::TYPE]) { - case Handlebars_Tokenizer::T_SECTION : - $newStack = isset($current[Handlebars_Tokenizer::NODES]) ? $current[Handlebars_Tokenizer::NODES] : array(); + switch ($current[Tokenizer::TYPE]) { + case Tokenizer::T_SECTION : + $newStack = isset($current[Tokenizer::NODES]) ? $current[Tokenizer::NODES] : array(); array_push($this->_stack, array(0, $newStack, false)); $buffer .= $this->_section($context, $current); array_pop($this->_stack); break; - case Handlebars_Tokenizer::T_INVERTED : - $newStack = isset($current[Handlebars_Tokenizer::NODES]) ? $current[Handlebars_Tokenizer::NODES] : array(); + case Tokenizer::T_INVERTED : + $newStack = isset($current[Tokenizer::NODES]) ? $current[Tokenizer::NODES] : array(); array_push($this->_stack, array(0, $newStack, false)); $buffer .= $this->_inverted($context, $current); array_pop($this->_stack); break; - case Handlebars_Tokenizer::T_COMMENT : + case Tokenizer::T_COMMENT : $buffer .= ''; break; - case Handlebars_Tokenizer::T_PARTIAL: - case Handlebars_Tokenizer::T_PARTIAL_2: + case Tokenizer::T_PARTIAL: + case Tokenizer::T_PARTIAL_2: $buffer .= $this->_partial($context, $current); break; - case Handlebars_Tokenizer::T_UNESCAPED: - case Handlebars_Tokenizer::T_UNESCAPED_2: + case Tokenizer::T_UNESCAPED: + case Tokenizer::T_UNESCAPED_2: $buffer .= $this->_variables($context, $current, false); break; - case Handlebars_Tokenizer::T_ESCAPED: + case Tokenizer::T_ESCAPED: $buffer .= $this->_variables($context, $current, true); break; - case Handlebars_Tokenizer::T_TEXT: - $buffer .= $current[Handlebars_Tokenizer::VALUE]; + case Tokenizer::T_TEXT: + $buffer .= $current[Tokenizer::VALUE]; break; default: - throw new RuntimeException('Invalid node type : ' . json_encode($current)); + throw new \RuntimeException('Invalid node type : ' . json_encode($current)); } } if ($stop) { @@ -195,8 +196,8 @@ class Handlebars_Template */ public function discard($context) { - if (!$context instanceof Handlebars_Context) { - $context = new Handlebars_Context($context); + if (!$context instanceof Context) { + $context = new Context($context); } $topTree = end($this->_stack); //This method never pop a value from stack list($index ,$tree, $stop) = $topTree; @@ -205,8 +206,8 @@ class Handlebars_Template $index++; //if the section is exactly like waitFor if (is_string($stop) - && $current[Handlebars_Tokenizer::TYPE] == Handlebars_Tokenizer::T_ESCAPED - && $current[Handlebars_Tokenizer::NAME] === $stop + && $current[Tokenizer::TYPE] == Tokenizer::T_ESCAPED + && $current[Tokenizer::NAME] === $stop ) { break; } @@ -224,21 +225,21 @@ class Handlebars_Template /** * Process section nodes * - * @param Handlebars_Context $context current context + * @param Context $context current context * @param array $current section node data * * @return string the result */ - private function _section(Handlebars_Context $context, $current) + private function _section(Context $context, $current) { $helpers = $this->handlebars->getHelpers(); - $sectionName = $current[Handlebars_Tokenizer::NAME]; + $sectionName = $current[Tokenizer::NAME]; if ($helpers->has($sectionName)) { - if (isset($current[Handlebars_Tokenizer::END])) { + if (isset($current[Tokenizer::END])) { $source = substr( $this->getSource(), - $current[Handlebars_Tokenizer::INDEX], - $current[Handlebars_Tokenizer::END] - $current[Handlebars_Tokenizer::INDEX] + $current[Tokenizer::INDEX], + $current[Tokenizer::END] - $current[Tokenizer::INDEX] ); } else { $source = ''; @@ -246,21 +247,21 @@ class Handlebars_Template $params = array( $this, //First argument is this template $context, //Secound is current context - $current[Handlebars_Tokenizer::ARGS], //Arguments + $current[Tokenizer::ARGS], //Arguments $source ); $return = call_user_func_array($helpers->$sectionName, $params); - if ($return instanceof Handlebars_String) { + if ($return instanceof String) { return $this->handlebars->loadString($return)->render($context); } else { return $return; } - } elseif (trim($current[Handlebars_Tokenizer::ARGS]) == '') { + } elseif (trim($current[Tokenizer::ARGS]) == '') { //Fallback for mustache style each/with/for just if there is no argument at all. try { $sectionVar = $context->get($sectionName, true); } catch (InvalidArgumentException $e) { - throw new RuntimeException($sectionName . ' is not registered as a helper'); + throw new \RuntimeException($sectionName . ' is not registered as a helper'); } $buffer = ''; if (is_array($sectionVar) || $sectionVar instanceof Traversable) { @@ -279,21 +280,21 @@ class Handlebars_Template } return $buffer; } else { - throw new RuntimeException($sectionName . ' is not registered as a helper'); + throw new \RuntimeException($sectionName . ' is not registered as a helper'); } } /** * Process inverted section * - * @param Handlebars_Context $context current context + * @param Context $context current context * @param array $current section node data * * @return string the result */ - private function _inverted(Handlebars_Context $context, $current) + private function _inverted(Context $context, $current) { - $sectionName = $current[Handlebars_Tokenizer::NAME]; + $sectionName = $current[Tokenizer::NAME]; $data = $context->get($sectionName); if (!$data) { return $this->render($context); @@ -306,17 +307,17 @@ class Handlebars_Template /** * Process partial section * - * @param Handlebars_Context $context current context + * @param Context $context current context * @param array $current section node data * * @return string the result */ private function _partial($context, $current) { - $partial = $this->handlebars->loadPartial($current[Handlebars_Tokenizer::NAME]); + $partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]); - if ( $current[Handlebars_Tokenizer::ARGS] ) { - $context = $context->get($current[Handlebars_Tokenizer::ARGS]); + if ( $current[Tokenizer::ARGS] ) { + $context = $context->get($current[Tokenizer::ARGS]); } return $partial->render($context); @@ -325,7 +326,7 @@ class Handlebars_Template /** * Process partial section * - * @param Handlebars_Context $context current context + * @param Context $context current context * @param array $current section node data * @param boolean $escaped escape result or not * @@ -333,7 +334,7 @@ class Handlebars_Template */ private function _variables($context, $current, $escaped) { - $value = $context->get($current[Handlebars_Tokenizer::NAME]); + $value = $context->get($current[Tokenizer::NAME]); if ($escaped) { $args = $this->handlebars->getEscapeArgs(); array_unshift($args, $value); diff --git a/src/Handlebars/Tokenizer.php b/src/Handlebars/Tokenizer.php index f24296e..e277cc8 100644 --- a/src/Handlebars/Tokenizer.php +++ b/src/Handlebars/Tokenizer.php @@ -32,7 +32,9 @@ * @version Release: @package_version@ * @link http://xamin.ir */ -class Handlebars_Tokenizer +namespace Handlebars; + +class Tokenizer { // Finite state machine states @@ -106,7 +108,7 @@ class Handlebars_Tokenizer */ public function scan($text, $delimiters = null) { - if ($text instanceof Handlebars_String) { + if ($text instanceof String) { $text = $text->getString(); } $this->reset();