mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-04-17 03:37:23 +03:00
add namespace, remove underscores from class names
This commit is contained in:
parent
dd7df5bf65
commit
82a9faf3eb
@ -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/" }
|
||||
}
|
||||
}
|
||||
}
|
@ -28,7 +28,9 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
class Handlebars_Autoloader
|
||||
namespace Handlebars;
|
||||
|
||||
class Autoloader
|
||||
{
|
||||
|
||||
private $_baseDir;
|
||||
|
@ -29,7 +29,9 @@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
|
||||
interface Handlebars_Cache
|
||||
namespace Handlebars;
|
||||
|
||||
interface Cache
|
||||
{
|
||||
/**
|
||||
* Get cache for $name if exist.
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
492
src/Handlebars/Handlebars.php
Normal file
492
src/Handlebars/Handlebars.php
Normal file
@ -0,0 +1,492 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of Handlebars-php
|
||||
* Base on mustache-php https://github.com/bobthecow/mustache.php
|
||||
*
|
||||
* PHP version 5.3
|
||||
*
|
||||
* @category Xamin
|
||||
* @package Handlebars
|
||||
* @author fzerorubigd <fzerorubigd@gmail.com>
|
||||
* @copyright 2012 (c) ParsPooyesh Co
|
||||
* @license GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
|
||||
* @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 <fzerorubigd@gmail.com>
|
||||
* @copyright 2012 (c) ParsPooyesh Co
|
||||
* @license GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
|
||||
* @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;
|
||||
}
|
||||
}
|
@ -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]);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -25,8 +25,9 @@
|
||||
* @version Release: @package_version@
|
||||
* @link http://xamin.ir
|
||||
*/
|
||||
namespace Handlebars;
|
||||
|
||||
class Handlebars_String
|
||||
class String
|
||||
{
|
||||
private $_string;
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user