Another cleanup

This commit is contained in:
fzerorubigd 2013-11-08 18:48:19 +03:30
parent 287a8da70c
commit 8938fdc5c3
No known key found for this signature in database
GPG Key ID: D6EE858AF9D2999A
10 changed files with 106 additions and 82 deletions

View File

@ -46,7 +46,7 @@ class Autoloader
protected function __construct($baseDir = null) protected function __construct($baseDir = null)
{ {
if ($baseDir === null) { if ($baseDir === null) {
$this->_baseDir = realpath(__DIR__.'/..'); $this->_baseDir = realpath(__DIR__ . '/..');
} else { } else {
$this->_baseDir = rtrim($baseDir, '/'); $this->_baseDir = rtrim($baseDir, '/');
} }

View File

@ -39,7 +39,7 @@ class APC implements Cache
* *
* @param string $name Cache id * @param string $name Cache id
* *
* @return data on hit, boolean false on cache not found * @return mixed data on hit, boolean false on cache not found
*/ */
public function get($name) public function get($name)
{ {

View File

@ -44,16 +44,19 @@ class Disk implements 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 $prefix optional file prefix, defaults to empty string
* @param string $suffix optional file extension, defaults to empty string * @param string $suffix optional file extension, defaults to empty string
*
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/ */
public function __construct($path, $prefix = '', $suffix = '') public function __construct($path, $prefix = '', $suffix = '')
{ {
if (empty($path)) { if (empty($path)) {
throw new InvalidArgumentException('Must specify disk cache path'); throw new \InvalidArgumentException('Must specify disk cache path');
} elseif (!is_dir($path)) { } elseif (!is_dir($path)) {
@mkdir($path, 0777, true); @mkdir($path, 0777, true);
if (!is_dir($path)) { if (!is_dir($path)) {
throw new RuntimeException('Could not create cache file path'); throw new \RuntimeException('Could not create cache file path');
} }
} }
@ -82,7 +85,7 @@ class Disk implements Cache
* *
* @param string $name Cache id * @param string $name Cache id
* *
* @return data on hit, boolean false on cache not found * @return mixed data on hit, boolean false on cache not found
*/ */
public function get($name) public function get($name)
{ {

View File

@ -40,7 +40,7 @@ class Dummy implements Cache
* *
* @param string $name Cache id * @param string $name Cache id
* *
* @return data on hit, boolean false on cache not found * @return mixed data on hit, boolean false on cache not found
*/ */
public function get($name) public function get($name)
{ {

View File

@ -169,6 +169,7 @@ class Context
{ {
$value = $this->get($variableName); $value = $this->get($variableName);
$this->push($value); $this->push($value);
return $value; return $value;
} }
@ -198,6 +199,7 @@ class Context
'can not find variable in context' 'can not find variable in context'
); );
} }
return ''; return '';
} }
end($this->stack); end($this->stack);
@ -212,6 +214,7 @@ class Context
'can not find variable in context' 'can not find variable in context'
); );
} }
return ''; return '';
} elseif ($variableName == '.' || $variableName == 'this') { } elseif ($variableName == '.' || $variableName == 'this') {
return $current; return $current;
@ -224,6 +227,7 @@ class Context
$current = $this->_findVariableInContext($current, $chunk, $strict); $current = $this->_findVariableInContext($current, $chunk, $strict);
} }
} }
return $current; return $current;
} }
@ -240,7 +244,7 @@ class Context
private function _findVariableInContext($variable, $inside, $strict = false) private function _findVariableInContext($variable, $inside, $strict = false)
{ {
$value = ''; $value = '';
if ( empty( $inside ) || ( $inside == 'this' ) ) { if (empty($inside) || ($inside == 'this')) {
return $variable; return $variable;
} elseif (is_array($variable)) { } elseif (is_array($variable)) {
if (isset($variable[$inside])) { if (isset($variable[$inside])) {
@ -257,6 +261,7 @@ class Context
} elseif ($strict) { } elseif ($strict) {
throw new \InvalidArgumentException('can not find variable in context'); throw new \InvalidArgumentException('can not find variable in context');
} }
return $value; return $value;
} }

View File

@ -42,13 +42,14 @@ class Handlebars
* *
* @param array $options see __construct's options parameter * @param array $options see __construct's options parameter
* *
* @return void * @return Handlebars
*/ */
public static function factory ($options=array()) public static function factory($options = array())
{ {
if (self::$_instance === false) { if (self::$_instance === false) {
self::$_instance = new Handlebars($options); self::$_instance = new Handlebars($options);
} }
return self::$_instance; return self::$_instance;
} }
@ -90,7 +91,7 @@ class Handlebars
/** /**
* @var array parametes to pass to escape function * @var array parametes to pass to escape function
*/ */
private $_escapeArgs = array ( private $_escapeArgs = array(
ENT_COMPAT, ENT_COMPAT,
'UTF-8' 'UTF-8'
); );
@ -193,6 +194,7 @@ class Handlebars
if (!isset($this->_helpers)) { if (!isset($this->_helpers)) {
$this->_helpers = new Helpers(); $this->_helpers = new Helpers();
} }
return $this->_helpers; return $this->_helpers;
} }
@ -218,7 +220,7 @@ class Handlebars
*/ */
public function getHelper($name) public function getHelper($name)
{ {
return $this->getHelpers()->get($name); return $this->getHelpers()->__get($name);
} }
/** /**
@ -267,6 +269,7 @@ class Handlebars
if (!isset($this->_loader)) { if (!isset($this->_loader)) {
$this->_loader = new StringLoader(); $this->_loader = new StringLoader();
} }
return $this->_loader; return $this->_loader;
} }
@ -292,6 +295,7 @@ class Handlebars
if (!isset($this->_partialsLoader)) { if (!isset($this->_partialsLoader)) {
$this->_partialsLoader = new StringLoader(); $this->_partialsLoader = new StringLoader();
} }
return $this->_partialsLoader; return $this->_partialsLoader;
} }
@ -317,8 +321,10 @@ class Handlebars
if (!isset($this->_cache)) { if (!isset($this->_cache)) {
$this->_cache = new Dummy(); $this->_cache = new Dummy();
} }
return $this->_cache; return $this->_cache;
} }
/** /**
* Get current escape function * Get current escape function
* *
@ -330,7 +336,7 @@ class Handlebars
} }
/** /**
* Set current escpae function * Set current escape function
* *
* @param callable $escape function * @param callable $escape function
* *
@ -358,7 +364,7 @@ class Handlebars
} }
/** /**
* Set current escpae function * Set current escape function
* *
* @param array $escapeArgs arguments to pass as extra arg to function * @param array $escapeArgs arguments to pass as extra arg to function
* *
@ -401,6 +407,7 @@ class Handlebars
return $this->_tokenizer; return $this->_tokenizer;
} }
/** /**
* Set the Handlebars Parser instance. * Set the Handlebars Parser instance.
* *
@ -429,6 +436,7 @@ class Handlebars
return $this->_parser; return $this->_parser;
} }
/** /**
* Load a template by name with current template loader * Load a template by name with current template loader
* *
@ -440,6 +448,7 @@ class Handlebars
{ {
$source = $this->getLoader()->load($name); $source = $this->getLoader()->load($name);
$tree = $this->_tokenize($source); $tree = $this->_tokenize($source);
return new Template($this, $tree, $source); return new Template($this, $tree, $source);
} }
@ -457,6 +466,7 @@ class Handlebars
} }
$source = $this->getPartialsLoader()->load($name); $source = $this->getPartialsLoader()->load($name);
$tree = $this->_tokenize($source); $tree = $this->_tokenize($source);
return new Template($this, $tree, $source); return new Template($this, $tree, $source);
} }
@ -497,6 +507,7 @@ class Handlebars
public function loadString($source) public function loadString($source)
{ {
$tree = $this->_tokenize($source); $tree = $this->_tokenize($source);
return new Template($this, $tree, $source); return new Template($this, $tree, $source);
} }
@ -516,6 +527,7 @@ class Handlebars
$tree = $this->getParser()->parse($tokens); $tree = $this->getParser()->parse($tokens);
$this->getCache()->set($hash, $tree); $this->getCache()->set($hash, $tree);
} }
return $tree; return $tree;
} }

View File

@ -31,7 +31,6 @@ use Handlebars\String;
* @license MIT <http://opensource.org/licenses/MIT> * @license MIT <http://opensource.org/licenses/MIT>
* @version Release: @package_version@ * @version Release: @package_version@
* @link http://xamin.ir * * @link http://xamin.ir *
* @implements Loader
*/ */
class FilesystemLoader implements Loader class FilesystemLoader implements Loader
@ -54,7 +53,7 @@ class FilesystemLoader implements Loader
* @param string|array $baseDirs A path contain template files or array of paths * @param string|array $baseDirs A path contain template files or array of paths
* @param array $options Array of Loader options (default: array()) * @param array $options Array of Loader options (default: array())
* *
* @throws RuntimeException if $baseDir does not exist. * @throws \RuntimeException if $baseDir does not exist.
*/ */
public function __construct($baseDirs, array $options = array()) public function __construct($baseDirs, array $options = array())
{ {
@ -101,6 +100,7 @@ class FilesystemLoader implements Loader
if (!isset($this->_templates[$name])) { if (!isset($this->_templates[$name])) {
$this->_templates[$name] = $this->loadFile($name); $this->_templates[$name] = $this->loadFile($name);
} }
return new String($this->_templates[$name]); return new String($this->_templates[$name]);
} }
@ -109,8 +109,8 @@ class FilesystemLoader implements Loader
* *
* @param string $name template name * @param string $name template name
* *
* @throws \InvalidArgumentException if a template file is not found.
* @return string Handlebars Template source * @return string Handlebars Template source
* @throws InvalidArgumentException if a template file is not found.
*/ */
protected function loadFile($name) protected function loadFile($name)
{ {
@ -149,11 +149,11 @@ class FilesystemLoader implements Loader
$fileName .= $this->_extension; $fileName .= $this->_extension;
} }
if (file_exists($fileName)) { if (file_exists($fileName)) {
break;
}
$fileName = false;
}
return $fileName; return $fileName;
} }
}
return false;
}
} }

View File

@ -30,7 +30,6 @@ use Handlebars\String;
* @license MIT <http://opensource.org/licenses/MIT> * @license MIT <http://opensource.org/licenses/MIT>
* @version Release: @package_version@ * @version Release: @package_version@
* @link http://xamin.ir * * @link http://xamin.ir *
* @implements Loader
*/ */
class StringLoader implements Loader class StringLoader implements Loader

View File

@ -60,7 +60,7 @@ class Template
$this->handlebars = $engine; $this->handlebars = $engine;
$this->tree = $tree; $this->tree = $tree;
$this->source = $source; $this->source = $source;
array_push($this->_stack, array (0, $this->getTree(), false)); array_push($this->_stack, array(0, $this->getTree(), false));
} }
/** /**
@ -117,6 +117,7 @@ class Template
public function getStopToken() public function getStopToken()
{ {
$topStack = end($this->_stack); $topStack = end($this->_stack);
return $topStack[2]; return $topStack[2];
} }
@ -134,7 +135,7 @@ class Template
$context = new Context($context); $context = new Context($context);
} }
$topTree = end($this->_stack); // never pop a value from stack $topTree = end($this->_stack); // never pop a value from stack
list($index ,$tree, $stop) = $topTree; list($index, $tree, $stop) = $topTree;
$buffer = ''; $buffer = '';
while (array_key_exists($index, $tree)) { while (array_key_exists($index, $tree)) {
@ -192,6 +193,7 @@ class Template
$newStack[2] = false; //No stop token from now on $newStack[2] = false; //No stop token from now on
array_push($this->_stack, $newStack); array_push($this->_stack, $newStack);
} }
return $buffer; return $buffer;
} }
@ -208,7 +210,7 @@ class Template
$context = new Context($context); $context = new Context($context);
} }
$topTree = end($this->_stack); //This method never pop a value from stack $topTree = end($this->_stack); //This method never pop a value from stack
list($index ,$tree, $stop) = $topTree; list($index, $tree, $stop) = $topTree;
while (array_key_exists($index, $tree)) { while (array_key_exists($index, $tree)) {
$current = $tree[$index]; $current = $tree[$index];
$index++; $index++;
@ -227,6 +229,7 @@ class Template
$newStack[2] = false; $newStack[2] = false;
array_push($this->_stack, $newStack); array_push($this->_stack, $newStack);
} }
return ''; return '';
} }
@ -293,6 +296,7 @@ class Template
} elseif ($sectionVar) { } elseif ($sectionVar) {
$buffer = $this->render($context); $buffer = $this->render($context);
} }
return $buffer; return $buffer;
} else { } else {
throw new \RuntimeException( throw new \RuntimeException(
@ -333,7 +337,7 @@ class Template
{ {
$partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]); $partial = $this->handlebars->loadPartial($current[Tokenizer::NAME]);
if ( $current[Tokenizer::ARGS] ) { if ($current[Tokenizer::ARGS]) {
$context = $context->get($current[Tokenizer::ARGS]); $context = $context->get($current[Tokenizer::ARGS]);
} }
@ -353,10 +357,10 @@ class Template
{ {
$name = $current[Tokenizer::NAME]; $name = $current[Tokenizer::NAME];
$value = $context->get($name); $value = $context->get($name);
if ( $name == '@index' ) { if ($name == '@index') {
return $context->lastIndex(); return $context->lastIndex();
} }
if ( $name == '@key' ) { if ($name == '@key') {
return $context->lastKey(); return $context->lastKey();
} }
if ($escaped) { if ($escaped) {
@ -367,6 +371,7 @@ class Template
array_values($args) array_values($args)
); );
} }
return $value; return $value;
} }
} }

View File

@ -164,7 +164,7 @@ class Tokenizer
if ($this->tagChange($this->ctag, $text, $i)) { if ($this->tagChange($this->ctag, $text, $i)) {
// Sections (Helpers) can accept parameters // Sections (Helpers) can accept parameters
// Same thing for Partials (little known fact) // 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)
|| ($this->tagType == self::T_PARTIAL_2) || ($this->tagType == self::T_PARTIAL_2)
) { ) {
@ -320,7 +320,7 @@ class Tokenizer
protected function changeDelimiters($text, $index) protected function changeDelimiters($text, $index)
{ {
$startIndex = strpos($text, '=', $index) + 1; $startIndex = strpos($text, '=', $index) + 1;
$close = '='.$this->ctag; $close = '=' . $this->ctag;
$closeIndex = strpos($text, $close, $index); $closeIndex = strpos($text, $close, $index);
list($otag, $ctag) = explode( list($otag, $ctag) = explode(