use PHP CodeSniffer's coding standards

This commit is contained in:
behrooz shabani (everplays) 2013-11-05 14:12:44 +03:30
parent 4999f204f9
commit e513648544
15 changed files with 409 additions and 279 deletions

View File

@ -4,22 +4,25 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* Changes to match xamin-std and handlebars made by xamin team
*
*
* PHP version 5.3
*
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Autloader for handlebars.php
*
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
@ -28,7 +31,6 @@
* @version Release: @package_version@
* @link http://xamin.ir
*/
namespace Handlebars;
class Autoloader
{
@ -38,12 +40,13 @@ class Autoloader
/**
* Autoloader constructor.
*
* @param string $baseDir Handlebars library base directory (default: dirname(__FILE__).'/..')
* @param string $baseDir Handlebars library base directory default is
* __DIR__.'/..'
*/
public function __construct($baseDir = null)
{
if ($baseDir === null) {
$this->_baseDir = dirname(__FILE__).'/..';
$this->_baseDir = __DIR__.'/..';
} else {
$this->_baseDir = rtrim($baseDir, '/');
}
@ -52,7 +55,8 @@ class Autoloader
/**
* Register a new instance as an SPL autoloader.
*
* @param string $baseDir Handlebars library base directory (default: dirname(__FILE__).'/..')
* @param string $baseDir Handlebars library base directory, default is
* __DIR__.'/..'
*
* @return Handlebars_Autoloader Registered Autoloader instance
*/
@ -86,4 +90,5 @@ class Autoloader
include $file;
}
}
}

View File

@ -8,12 +8,15 @@
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Cache interface
@ -29,10 +32,9 @@
* @link http://xamin.ir
*/
namespace Handlebars;
interface Cache
{
/**
* Get cache for $name if exist.
*
@ -60,4 +62,5 @@ interface Cache
* @return void
*/
public function remove($name);
}

View File

@ -8,13 +8,17 @@
* @category Xamin
* @package Handlebars
* @author Joey Baker <joey@byjoeybaker.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2013 (c) Meraki, LLP
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars\Cache;
use Handlebars\Cache;
/**
* A dummy array cache
*
@ -26,12 +30,10 @@
* @version Release: @package_version@
* @link http://xamin.ir
*/
namespace Handlebars\Cache;
class APC implements Cache
{
private $_cache = array();
/**
* Get cache for $name if exist.
*
@ -41,12 +43,12 @@ class APC implements Cache
*/
public function get($name)
{
if (apc_exists($name)){
return apc_fetch($name);
}
return false;
if (apc_exists($name)) {
return apc_fetch($name);
}
return false;
}
/**
* Set a cache
*
@ -59,7 +61,7 @@ class APC implements Cache
{
apc_store($name, $value);
}
/**
* Remove cache
*
@ -71,4 +73,5 @@ class APC implements Cache
{
apc_delete($name);
}
}

View File

@ -8,12 +8,16 @@
* @category Xamin
* @package Handlebars
* @author Alex Soncodi <alex@brokerloop.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2013 (c) Brokerloop, Inc.
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars\Cache;
use Handlebars\Cache;
/**
* A flat-file filesystem cache.
@ -27,8 +31,9 @@
* @link http://xamin.ir
*/
class Handlebars_Cache_Disk implements Handlebars_Cache
class Disk implements Cache
{
private $_path = '';
private $_prefix = '';
private $_suffix = '';
@ -36,7 +41,7 @@ class Handlebars_Cache_Disk implements Handlebars_Cache
/**
* Construct the disk cache.
*
* @param string $path Filesystem path to the disk cache location
* @param string $path Filesystem path to the disk cache location
* @param string $prefix optional file prefix, defaults to empty string
* @param string $suffix optional file extension, defaults to empty string
*/
@ -44,8 +49,7 @@ class Handlebars_Cache_Disk implements Handlebars_Cache
{
if (empty($path)) {
throw new InvalidArgumentException('Must specify disk cache path');
}
else if (!is_dir($path)) {
} elseif (!is_dir($path)) {
@mkdir($path, 0777, true);
if (!is_dir($path)) {
@ -64,8 +68,10 @@ class Handlebars_Cache_Disk implements Handlebars_Cache
* and optional extension.
*
* @param string $name Name of the cache item
*
* @return string full disk path of cached item
*/
private function getPath($name)
private function _getPath($name)
{
return $this->_path . DIRECTORY_SEPARATOR .
$this->_prefix . $name . $this->_suffix;
@ -80,7 +86,7 @@ class Handlebars_Cache_Disk implements Handlebars_Cache
*/
public function get($name)
{
$path = $this->getPath($name);
$path = $this->_getPath($name);
return (file_exists($path)) ?
unserialize(file_get_contents($path)) : false;
@ -96,7 +102,7 @@ class Handlebars_Cache_Disk implements Handlebars_Cache
*/
public function set($name, $value)
{
$path = $this->getPath($name);
$path = $this->_getPath($name);
file_put_contents($path, serialize($value));
}
@ -110,8 +116,9 @@ class Handlebars_Cache_Disk implements Handlebars_Cache
*/
public function remove($name)
{
$path = $this->getPath($name);
$path = $this->_getPath($name);
unlink($path);
}
}

View File

@ -2,22 +2,26 @@
/**
* 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>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars\Cache;
use Handlebars\Cache;
/**
* A dummy array cache
*
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
@ -27,26 +31,24 @@
* @link http://xamin.ir
*/
namespace Handlebars\Cache;
use Handlebars\Cache;
class Dummy implements Cache
{
private $_cache = array();
/**
* Get cache for $name if exist.
* Get cache for $name if exist.
*
* @param string $name Cache id
*
* @return data on hit, boolean false on cache not found
* @return data on hit, boolean false on cache not found
*/
public function get($name)
{
if (array_key_exists($name, $this->_cache)) {
return $this->_cache[$name];
}
}
return false;
}
}
/**
* Set a cache
@ -59,10 +61,10 @@ class Dummy implements Cache
public function set($name, $value)
{
$this->_cache[$name] = $value;
}
}
/**
* Remove cache
* Remove cache
*
* @param string $name Cache id
*
@ -72,4 +74,5 @@ class Dummy implements Cache
{
unset($this->_cache[$name]);
}
}
}

View File

@ -2,23 +2,26 @@
/**
* 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>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Handlebars context
* Context for a template
*
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
@ -28,16 +31,15 @@
* @version Release: @package_version@
* @link http://xamin.ir
*/
namespace Handlebars;
class Context
{
/**
* @var array stack for context only top stack is available
*/
*/
protected $stack = array();
/**
* @var index stack for sections
*/
@ -75,7 +77,9 @@ class Context
/**
* Push an Index onto the index stack
*
* @param int $index Index of the current section item.
* @param integer $index Index of the current section item.
*
* @return void
*/
public function pushIndex($index)
{
@ -86,6 +90,8 @@ class Context
* Push a Key onto the key stack
*
* @param string $key Key of the current object property.
*
* @return void
*/
public function pushKey($key)
{
@ -102,7 +108,6 @@ class Context
return array_pop($this->stack);
}
/**
* Pop the last index from the stack.
*
@ -169,14 +174,14 @@ class Context
/**
* Get a avariable from current context
* Supported types :
* Supported types :
* variable , ../variable , variable.variable , .
*
*
* @param string $variableName variavle name to get from current context
* @param boolean $strict strict search? if not found then throw exception
*
* @return mixed
* @throw InvalidArgumentException in strict mode and variable not found
* @throws InvalidArgumentException in strict mode and variable not found
*/
public function get($variableName, $strict = false)
{
@ -189,8 +194,10 @@ class 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 '';
}
end($this->stack);
@ -201,8 +208,10 @@ class 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') {
return $current;
@ -226,8 +235,8 @@ class Context
* @param boolean $strict strict search? if not found then throw exception
*
* @return boolean true if exist
* @throw InvalidArgumentException in strict mode and variable not found
*/
* @throws InvalidArgumentException in strict mode and variable not found
*/
private function _findVariableInContext($variable, $inside, $strict = false)
{
$value = '';
@ -242,7 +251,7 @@ class Context
$value = $variable->$inside;
} elseif (is_callable(array($variable, $inside))) {
$value = call_user_func(array($variable, $inside));
}
}
} elseif ($inside === '.') {
$value = $variable;
} elseif ($strict) {
@ -250,4 +259,5 @@ class Context
}
return $value;
}
}
}

View File

@ -1,23 +1,27 @@
<?php
/**
* This file is part of Handlebars-php
* Base on mustache-php https://github.com/bobthecow/mustache.php
* Based on mustache-php https://github.com/bobthecow/mustache.php
*
* PHP version 5.3
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
use Handlebars\Loader\StringLoader;
use Handlebars\Cache\Dummy;
/**
* Handlebars parser (infact its a mustache parser)
* This class is responsible for turning raw template source into a set of Mustache tokens.
* Handlebars template engine, based on mustache.
*
* @category Xamin
* @package Handlebars
@ -27,20 +31,25 @@
* @version Release: @package_version@
* @link http://xamin.ir
*/
namespace Handlebars;
use Handlebars\Loader\StringLoader;
use Handlebars\Cache\Dummy;
class Handlebars
{
private static $instance = false;
private static $_instance = false;
const VERSION = '1.0.0';
public static function factory ($options=array()) {
if (self::$instance === false) {
self::$instance = new Handlebars($options);
/**
* factory method
*
* @param array $options see __construct's options parameter
*
* @return void
*/
public static function factory ($options=array())
{
if (self::$_instance === false) {
self::$_instance = new Handlebars($options);
}
return self::$instance;
return self::$_instance;
}
/**
@ -52,6 +61,7 @@ class Handlebars
* @var Parser
*/
private $_parser;
/**
* @var Helpers
*/
@ -71,20 +81,22 @@ class Handlebars
* @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
* @var array parametes to pass to escape function
*/
private $_escapeArgs = array (
ENT_COMPAT,
'UTF-8'
);
);
private $_aliases = array();
/**
* Handlebars engine constructor
* $options array can contain :
@ -117,7 +129,9 @@ class Handlebars
if (isset($options['escape'])) {
if (!is_callable($options['escape'])) {
throw new \InvalidArgumentException('Handlebars Constructor "escape" option must be callable');
throw new \InvalidArgumentException(
'Handlebars Constructor "escape" option must be callable'
);
}
$this->_escape = $options['escape'];
@ -323,7 +337,9 @@ class Handlebars
public function setEscape($escape)
{
if (!is_callable($escape)) {
throw new \InvalidArgumentException('Escape function must be a callable');
throw new \InvalidArgumentException(
'Escape function must be a callable'
);
}
$this->_escape = $escape;
}
@ -369,7 +385,8 @@ class Handlebars
/**
* Get the current Handlebars Tokenizer instance.
*
* If no Tokenizer instance has been explicitly specified, this method will instantiate and return a new one.
* If no Tokenizer instance has been explicitly specified, this method will
* instantiate and return a new one.
*
* @return Tokenizer
*/
@ -396,7 +413,8 @@ class Handlebars
/**
* Get the current Handlebars Parser instance.
*
* If no Parser instance has been explicitly specified, this method will instantiate and return a new one.
* If no Parser instance has been explicitly specified, this method will
* instantiate and return a new one.
*
* @return Parser
*/
@ -497,4 +515,5 @@ class Handlebars
}
return $tree;
}
}

View File

@ -2,25 +2,31 @@
/**
* 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>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Handlebars helpers
* Handlebars helpers
*
* a collection of helper function. normally a function like
* function ($sender, $name, $arguments) $arguments is unscaped arguments and
* is a string, not array
*
* a collection of helper function. normally a function like
* function ($sender, $name, $arguments) $arguments is unscaped arguments and is a string, not array
* TODO: Add support for an interface with an execute method
*
*
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
@ -29,7 +35,6 @@
* @version Release: @package_version@
* @link http://xamin.ir
*/
namespace Handlebars;
class Helpers
{
@ -44,196 +49,216 @@ class Helpers
* @param array $helpers array of name=>$value helpers
* @param array $defaults add defaults helper (if, unless, each,with)
*
* @throw InvalidArgumentException when $helpers is not an array (or traversable) or helper is not a caallable
* @throws InvalidArgumentException when $helpers is not an array
* (or traversable) or helper is not a callable
*/
public function __construct($helpers = null, $defaults = true)
{
if ($defaults) {
$this->addDefaultHelpers();
}
}
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);
}
}
}
}
/**
* Create handler for the 'if' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous functions.
*
* @param $template
* @param $context
* @param $args
* @param $source
*
* @return mixed
*/
public static function _helper_if($template, $context, $args, $source) {
$tmp = $context->get($args);
$buffer = '';
/**
* Create handler for the 'if' helper.
*
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions.
*
* @param Handlebars\Template $template template that is being rendered
* @param Handlebars\Context $context context object
* @param array $args passed arguments to helper
* @param string $source part of template that is wrapped
* within helper
*
* @return mixed
*/
public static function helperIf($template, $context, $args, $source)
{
$tmp = $context->get($args);
$buffer = '';
if ($tmp) {
$template->setStopToken('else');
$buffer = $template->render($context);
$template->setStopToken(false);
$template->discard($context);
} else {
$template->setStopToken('else');
$template->discard($context);
$template->setStopToken(false);
$buffer = $template->render($context);
}
return $buffer;
}
if ($tmp) {
$template->setStopToken('else');
$buffer = $template->render($context);
$template->setStopToken(false);
$template->discard($context);
} else {
$template->setStopToken('else');
$template->discard($context);
$template->setStopToken(false);
$buffer = $template->render($context);
}
return $buffer;
}
/**
* Create handler for the 'each' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous functions.
*
* @param $template
* @param $context
* @param $args
* @param $source
*
* @return mixed
*/
public static function _helper_each($template, $context, $args, $source) {
$tmp = $context->get($args);
$buffer = '';
if (is_array($tmp) || $tmp instanceof Traversable) {
$islist = ( array_keys($tmp) == range(0, count($tmp) - 1) );
/**
* Create handler for the 'each' helper.
*
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions.
*
* @param Handlebars\Template $template template that is being rendered
* @param Handlebars\Context $context context object
* @param array $args passed arguments to helper
* @param string $source part of template that is wrapped
* within helper
*
* @return mixed
*/
public static function helperEach($template, $context, $args, $source)
{
$tmp = $context->get($args);
$buffer = '';
if (is_array($tmp) || $tmp instanceof Traversable) {
$islist = ( array_keys($tmp) == range(0, count($tmp) - 1) );
foreach ($tmp as $key => $var) {
if( $islist ) {
$context->pushIndex($key);
} else {
$context->pushKey($key);
}
$context->push($var);
$buffer .= $template->render($context);
$context->pop();
if( $islist ) {
$context->popIndex();
} else {
$context->popKey();
}
}
}
return $buffer;
}
foreach ($tmp as $key => $var) {
if ( $islist ) {
$context->pushIndex($key);
} else {
$context->pushKey($key);
}
$context->push($var);
$buffer .= $template->render($context);
$context->pop();
if ( $islist ) {
$context->popIndex();
} else {
$context->popKey();
}
}
}
return $buffer;
}
/**
* Create handler for the 'unless' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous functions.
*
* @param $template
* @param $context
* @param $args
* @param $source
*
* @return mixed
*/
public static function _helper_unless($template, $context, $args, $source) {
$tmp = $context->get($args);
$buffer = '';
if (!$tmp) {
$buffer = $template->render($context);
}
return $buffer;
}
/**
* Create handler for the 'unless' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions.
*
* @param Handlebars\Template $template template that is being rendered
* @param Handlebars\Context $context context object
* @param array $args passed arguments to helper
* @param string $source part of template that is wrapped
* within helper
*
* @return mixed
*/
public static function helperUnless($template, $context, $args, $source)
{
$tmp = $context->get($args);
$buffer = '';
if (!$tmp) {
$buffer = $template->render($context);
}
return $buffer;
}
/**
* Create handler for the 'with' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous functions.
*
* @param $template
* @param $context
* @param $args
* @param $source
*
* @return mixed
*/
public static function _helper_with($template, $context, $args, $source) {
$tmp = $context->get($args);
$context->push($tmp);
$buffer = $template->render($context);
$context->pop();
return $buffer;
}
/**
* Create handler for the 'with' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions.
*
* @param Handlebars\Template $template template that is being rendered
* @param Handlebars\Context $context context object
* @param array $args passed arguments to helper
* @param string $source part of template that is wrapped
* within helper
*
* @return mixed
*/
public static function helperWith($template, $context, $args, $source)
{
$tmp = $context->get($args);
$context->push($tmp);
$buffer = $template->render($context);
$context->pop();
return $buffer;
}
/**
* Create handler for the 'bindAttr' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous functions.
*
* @param $template
* @param $context
* @param $args
* @param $source
*
* @return mixed
*/
public static function _helper_bindAttr($template, $context, $args, $source) {
return $args;
}
/**
* Create handler for the 'bindAttr' helper.
* Needed for compatibility with PHP 5.2 since it doesn't support anonymous
* functions.
*
* @param Handlebars\Template $template template that is being rendered
* @param Handlebars\Context $context context object
* @param array $args passed arguments to helper
* @param string $source part of template that is wrapped
* within helper
*
* @return mixed
*/
public static function helperBindAttr($template, $context, $args, $source)
{
return $args;
}
/**
* Add default helpers (if unless each with bindAttr)
*
*
* @return void
*/
protected function addDefaultHelpers()
{
$this->add(
'if',
array('Handlebars\Helpers', '_helper_if')
array('Handlebars\Helpers', 'helperIf')
);
$this->add(
'each',
array('Handlebars\Helpers', '_helper_each')
array('Handlebars\Helpers', 'helperEach')
);
$this->add(
'unless',
array('Handlebars\Helpers', '_helper_unless')
array('Handlebars\Helpers', 'helperUnless')
);
$this->add(
'with',
array('Handlebars\Helpers', '_helper_with')
array('Handlebars\Helpers', 'helperWith')
);
//Just for compatibility with ember
$this->add(
'bindAttr',
array('Handlebars\Helpers', '_helper_bindAttr')
array('Handlebars\Helpers', 'helperBindAttr')
);
}
/**
* Add a new helper to helpers
*
* Add a new helper to helpers
*
* @param string $name helper name
* @param callable $helper a function as a helper
*
* @return void
* @throw InvalidArgumentException if $helper is not a callable
* @throws InvalidArgumentException if $helper is not a callable
*/
public function add($name ,$helper)
public function add($name ,$helper)
{
if (!is_callable($helper)) {
throw new \InvalidArgumentException("$name Helper is not a callable.");
}
}
$this->helpers[$name] = $helper;
}
/**
* Check if $name helper is available
*
*
* @param string $name helper name
*
* @return boolean
@ -244,24 +269,24 @@ class Helpers
}
/**
* Get a helper. __magic__ method :)
*
* Get a helper. __magic__ method :)
*
* @param string $name helper name
*
* @return callable helper function
* @throw InvalidArgumentException if $name is not available
* @throws InvalidArgumentException if $name is not available
*/
public function __get($name)
{
if (!$this->has($name)) {
throw new \InvalidArgumentException('Unknow helper :' . $name);
}
}
return $this->helpers[$name];
}
/**
* Check if $name helper is available __magic__ method :)
*
* Check if $name helper is available __magic__ method :)
*
* @param string $name helper name
*
* @return boolean
@ -273,26 +298,26 @@ class Helpers
}
/**
* Add a new helper to helpers __magic__ method :)
*
* Add a new helper to helpers __magic__ method :)
*
* @param string $name helper name
* @param callable $helper a function as a helper
*
* @return void
* @throw InvalidArgumentException if $helper is not a callable
* @throws InvalidArgumentException if $helper is not a callable
*/
public function __set($name ,$helper)
{
$this->add($name, $helpers);
}
}
/**
* Unset a helper
*
*
* @param string $name helpername to remove
*
* @return void
* @return void
*/
public function __unset($name)
{
@ -337,5 +362,5 @@ class Helpers
{
return empty($this->helpers);
}
}
}

View File

@ -8,12 +8,15 @@
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Handlebars loader interface
@ -26,7 +29,6 @@
* @version Release: @package_version@
* @link http://xamin.ir
*/
namespace Handlebars;
interface Loader
{
@ -39,4 +41,5 @@ interface Loader
* @return String
*/
public function load($name);
}

View File

@ -9,12 +9,18 @@
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars\Loader;
use Handlesbars\Loader;
use Handlesbars\String;
/**
* Handlebars Template filesystem Loader implementation.
*
@ -27,8 +33,6 @@
* @link http://xamin.ir *
* @implements Loader
*/
namespace Handlebars\Loader;
use Handlesbars\String;
class FilesystemLoader implements Loader
{
@ -40,10 +44,10 @@ class FilesystemLoader implements Loader
/**
* Handlebars filesystem Loader constructor.
*
* Passing an $options array allows overriding certain Loader options during instantiation:
* $options array allows overriding certain Loader options during instantiation:
*
* $options = array(
* // The filename extension used for Handlebars templates. Defaults to '.handlebars'
* // extension used for Handlebars templates. Defaults to '.handlebars'
* 'extension' => '.other',
* );
*
@ -66,7 +70,9 @@ class FilesystemLoader implements 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
);
}
}
@ -83,7 +89,8 @@ class FilesystemLoader implements Loader
* Load a Template by name.
*
* $loader = new FilesystemLoader(dirname(__FILE__).'/views');
* $loader->load('admin/dashboard'); // loads "./views/admin/dashboard.handlebars";
* // loads "./views/admin/dashboard.handlebars";
* $loader->load('admin/dashboard');
*
* @param string $name template name
*
@ -136,8 +143,9 @@ class FilesystemLoader implements Loader
$fileParts[] = $file;
$fileName .= implode('/', $fileParts);
$lastCharacters = substr($fileName, 0 - strlen($this->_extension));
if (substr($fileName, 0 - strlen($this->_extension)) !== $this->_extension) {
if ($lastCharacters !== $this->_extension) {
$fileName .= $this->_extension;
}
if (file_exists($fileName)) {
@ -147,4 +155,5 @@ class FilesystemLoader implements Loader
}
return $fileName;
}
}

View File

@ -8,12 +8,18 @@
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars\Loader;
use Handlebars\Loader;
use Handlebars\String;
/**
* Handlebars Template string Loader implementation.
*
@ -26,9 +32,6 @@
* @link http://xamin.ir *
* @implements Loader
*/
namespace Handlebars\Loader;
use Handlebars\Loader;
use Handlebars\String;
class StringLoader implements Loader
{
@ -44,4 +47,5 @@ class StringLoader implements Loader
{
return new String($name);
}
}

View File

@ -9,15 +9,21 @@
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Handlebars parser (infact its a mustache parser)
* This class is responsible for turning raw template source into a set of Mustache tokens.
* Handlebars parser (based on mustache)
*
* This class is responsible for turning raw template source into a set of
* Handlebars tokens.
*
* @category Xamin
* @package Handlebars
@ -27,7 +33,6 @@
* @version Release: @package_version@
* @link http://xamin.ir
*/
namespace Handlebars;
class Parser
{
@ -50,7 +55,8 @@ class Parser
*
* @return array Token parse tree
*
* @throws LogicException when nesting errors or mismatched section tags are encountered.
* @throws LogicException when nesting errors or mismatched section tags
* are encountered.
*/
private function _buildTree(\ArrayIterator $tokens)
{
@ -70,7 +76,9 @@ class Parser
do {
$result = array_pop($stack);
if ($result === null) {
throw new \LogicException('Unexpected closing tag: /'. $token[Tokenizer::NAME]);
throw new \LogicException(
'Unexpected closing tag: /'. $token[Tokenizer::NAME]
);
}
if (!array_key_exists(Tokenizer::NODES, $result)
@ -96,4 +104,5 @@ class Parser
return $stack;
}
}

View File

@ -7,12 +7,14 @@
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2013 Authors
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Handlebars string
@ -25,7 +27,6 @@
* @version Release: @package_version@
* @link http://xamin.ir
*/
namespace Handlebars;
class String
{
@ -72,4 +73,5 @@ class String
{
$this->_string = $string;
}
}

View File

@ -8,12 +8,15 @@
* @category Xamin
* @package Handlebars
* @author fzerorubigd <fzerorubigd@gmail.com>
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/MIT>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Handlebars base template
@ -27,7 +30,6 @@
* @version Release: @package_version@
* @link http://xamin.ir
*/
namespace Handlebars;
class Template
{
@ -50,8 +52,8 @@ class Template
* Handlebars template constructor
*
* @param Handlebars $engine handlebar engine
* @param array $tree Parsed tree
* @param string $source Handlebars source
* @param array $tree Parsed tree
* @param string $source Handlebars source
*/
public function __construct(Handlebars $engine, $tree, $source)
{
@ -129,7 +131,7 @@ class Template
if (!$context instanceof Context) {
$context = new Context($context);
}
$topTree = end($this->_stack); //This method (render) never pop a value from stack
$topTree = end($this->_stack); // never pop a value from stack
list($index ,$tree, $stop) = $topTree;
$buffer = '';
@ -145,13 +147,15 @@ class Template
}
switch ($current[Tokenizer::TYPE]) {
case Tokenizer::T_SECTION :
$newStack = isset($current[Tokenizer::NODES]) ? $current[Tokenizer::NODES] : array();
$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 Tokenizer::T_INVERTED :
$newStack = isset($current[Tokenizer::NODES]) ? $current[Tokenizer::NODES] : array();
$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);
@ -174,7 +178,9 @@ class Template
$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) {
@ -226,7 +232,7 @@ class Template
* Process section nodes
*
* @param Context $context current context
* @param array $current section node data
* @param array $current section node data
*
* @return string the result
*/
@ -258,11 +264,14 @@ class Template
return $return;
}
} elseif (trim($current[Tokenizer::ARGS]) == '') {
//Fallback for mustache style each/with/for just if there is no argument at all.
// fallback to 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) {
@ -283,7 +292,9 @@ class Template
}
return $buffer;
} else {
throw new \RuntimeException($sectionName . ' is not registered as a helper');
throw new \RuntimeException(
$sectionName . ' is not registered as a helper'
);
}
}
@ -291,7 +302,7 @@ class Template
* Process inverted section
*
* @param Context $context current context
* @param array $current section node data
* @param array $current section node data
*
* @return string the result
*/
@ -311,7 +322,7 @@ class Template
* Process partial section
*
* @param Context $context current context
* @param array $current section node data
* @param array $current section node data
*
* @return string the result
*/
@ -330,8 +341,8 @@ class Template
* Process partial section
*
* @param Context $context current context
* @param array $current section node data
* @param boolean $escaped escape result or not
* @param array $current section node data
* @param boolean $escaped escape result or not
*
* @return string the result
*/
@ -339,19 +350,21 @@ class Template
{
$name = $current[Handlebars_Tokenizer::NAME];
$value = $context->get($name);
if( $name == '@index' ) {
if ( $name == '@index' ) {
return $context->lastIndex();
}
if( $name == '@key' ) {
if ( $name == '@key' ) {
return $context->lastKey();
}
if ($escaped) {
$args = $this->handlebars->getEscapeArgs();
array_unshift($args, $value);
$value = call_user_func_array($this->handlebars->getEscape(), array_values($args));
$value = call_user_func_array(
$this->handlebars->getEscape(),
array_values($args)
);
}
return $value;
}
}

View File

@ -11,17 +11,18 @@
* @package Handlebars
* @author Justin Hileman <dontknow@example.org>
* @author fzerorubigd <fzerorubigd@gmail.com>
* @copyright 2012 Justin Hileman
* @author Behrooz Shabani <everplays@gmail.com>
* @copyright 2012 (c) ParsPooyesh Co
* @copyright 2013 (c) Behrooz Shabani
* @license MIT <http://opensource.org/licenses/mit-license.php>
* @version GIT: $Id$
* @link http://xamin.ir
*/
namespace Handlebars;
/**
* Handlebars parser (infact its a mustache parser)
* This class is responsible for turning raw template source into a set of Mustache tokens.
* Some minor changes to handle Handlebars instead of Mustache
* Handlebars tokenizer (based on mustache)
*
* @category Xamin
* @package Handlebars
@ -32,7 +33,6 @@
* @version Release: @package_version@
* @link http://xamin.ir
*/
namespace Handlebars;
class Tokenizer
{
@ -47,7 +47,8 @@ class Tokenizer
const T_INVERTED = '^';
const T_END_SECTION = '/';
const T_COMMENT = '!';
const T_PARTIAL = '>'; //Maybe remove this partials and replace them with helpers
// XXX: remove partials support from tokenizer and make it a helper?
const T_PARTIAL = '>';
const T_PARTIAL_2 = '<';
const T_DELIM_CHANGE = '=';
const T_ESCAPED = '_v';
@ -102,7 +103,7 @@ class Tokenizer
* Scan and tokenize template source.
*
* @param string $text Mustache template source to tokenize
* @param string $delimiters Optionally, pass initial opening and closing delimiters (default: null)
* @param string $delimiters Optional, pass opening and closing delimiters
*
* @return array Set of Mustache tokens
*/
@ -163,8 +164,7 @@ class Tokenizer
if ($this->tagChange($this->ctag, $text, $i)) {
// Sections (Helpers) can accept parameters
// Same thing for Partials (little known fact)
if (
($this->tagType == self::T_SECTION)
if ( ($this->tagType == self::T_SECTION)
|| ($this->tagType == self::T_PARTIAL)
|| ($this->tagType == self::T_PARTIAL_2)
) {
@ -180,7 +180,9 @@ class Tokenizer
self::NAME => trim($this->buffer),
self::OTAG => $this->otag,
self::CTAG => $this->ctag,
self::INDEX => ($this->tagType == self::T_END_SECTION) ? $this->seenTag - strlen($this->otag) : $i + strlen($this->ctag),
self::INDEX => ($this->tagType == self::T_END_SECTION) ?
$this->seenTag - strlen($this->otag) :
$i + strlen($this->ctag),
);
if (isset($args)) {
$t[self::ARGS] = $args;
@ -196,9 +198,12 @@ class Tokenizer
$i++;
} else {
// Clean up `{{{ tripleStache }}}` style tokens.
$lastName = $this->tokens[count($this->tokens) - 1][self::NAME];
$lastIndex = count($this->tokens) - 1;
$lastName = $this->tokens[$lastIndex][self::NAME];
if (substr($lastName, -1) === '}') {
$this->tokens[count($this->tokens) - 1][self::NAME] = trim(substr($lastName, 0, -1));
$this->tokens[$lastIndex][self::NAME] = trim(
substr($lastName, 0, -1)
);
}
}
}
@ -240,7 +245,10 @@ class Tokenizer
protected function flushBuffer()
{
if (!empty($this->buffer)) {
$this->tokens[] = array(self::TYPE => self::T_TEXT, self::VALUE => $this->buffer);
$this->tokens[] = array(
self::TYPE => self::T_TEXT,
self::VALUE => $this->buffer
);
$this->buffer = '';
}
}
@ -283,8 +291,11 @@ class Tokenizer
$tokensCount = count($this->tokens);
for ($j = $this->lineStart; $j < $tokensCount; $j++) {
if ($this->tokens[$j][self::TYPE] == self::T_TEXT) {
if (isset($this->tokens[$j + 1]) && $this->tokens[$j + 1][self::TYPE] == self::T_PARTIAL) {
$this->tokens[$j + 1][self::INDENT] = $this->tokens[$j][self::VALUE];
if (isset($this->tokens[$j + 1])
&& $this->tokens[$j + 1][self::TYPE] == self::T_PARTIAL
) {
$this->tokens[$j + 1][self::INDENT]
= $this->tokens[$j][self::VALUE];
}
$this->tokens[$j] = null;
@ -312,7 +323,10 @@ class Tokenizer
$close = '='.$this->ctag;
$closeIndex = strpos($text, $close, $index);
list($otag, $ctag) = explode(' ', trim(substr($text, $startIndex, $closeIndex - $startIndex)));
list($otag, $ctag) = explode(
' ',
trim(substr($text, $startIndex, $closeIndex - $startIndex))
);
$this->otag = $otag;
$this->ctag = $ctag;
@ -332,4 +346,5 @@ class Tokenizer
{
return substr($text, $index, strlen($tag)) === $tag;
}
}