mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Add checking nested level for {use} and {extends}
Add more docs
This commit is contained in:
@ -23,7 +23,7 @@ class Compiler {
|
||||
* @static
|
||||
* @param Tokenizer $tokens
|
||||
* @param Template $tpl
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
* @return string
|
||||
*/
|
||||
public static function tagInclude(Tokenizer $tokens, Template $tpl) {
|
||||
@ -68,12 +68,12 @@ class Compiler {
|
||||
* @static
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
* @return string
|
||||
*/
|
||||
public static function tagElseIf(Tokenizer $tokens, Scope $scope) {
|
||||
if($scope["else"]) {
|
||||
throw new ImproperUseException('Incorrect use of the tag {elseif}');
|
||||
throw new InvalidUsageException('Incorrect use of the tag {elseif}');
|
||||
}
|
||||
return '} elseif('.$scope->tpl->parseExp($tokens, true).') {';
|
||||
}
|
||||
@ -99,7 +99,7 @@ class Compiler {
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @throws UnexpectedTokenException
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
* @return string
|
||||
*/
|
||||
public static function foreachOpen(Tokenizer $tokens, Scope $scope) {
|
||||
@ -132,7 +132,7 @@ class Compiler {
|
||||
while($token = $tokens->key()) {
|
||||
$param = $tokens->get(T_STRING);
|
||||
if(!isset($p[ $param ])) {
|
||||
throw new ImproperUseException("Unknown parameter '$param' in {foreach}");
|
||||
throw new InvalidUsageException("Unknown parameter '$param' in {foreach}");
|
||||
}
|
||||
$tokens->getNext("=");
|
||||
$tokens->next();
|
||||
@ -197,7 +197,7 @@ class Compiler {
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @return string
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
*/
|
||||
public static function forOpen(Tokenizer $tokens, Scope $scope) {
|
||||
$p = array("index" => false, "first" => false, "last" => false, "step" => 1, "to" => false, "max" => false, "min" => false);
|
||||
@ -218,7 +218,7 @@ class Compiler {
|
||||
$condition = "$var >= {$p['to']}";
|
||||
if($p["last"]) $c = "($var + {$p['step']}) < {$p['to']}";
|
||||
} else {
|
||||
throw new ImproperUseException("Invalid step value if {for}");
|
||||
throw new InvalidUsageException("Invalid step value if {for}");
|
||||
}
|
||||
} else {
|
||||
$condition = "({$p['step']} > 0 && $var <= {$p['to']} || {$p['step']} < 0 && $var >= {$p['to']})";
|
||||
@ -323,14 +323,14 @@ class Compiler {
|
||||
* @static
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
* @return string
|
||||
*/
|
||||
public static function tagContinue($tokens, Scope $scope) {
|
||||
if(empty($scope["no-continue"])) {
|
||||
return 'continue;';
|
||||
} else {
|
||||
throw new ImproperUseException("Improper usage of the tag {continue}");
|
||||
throw new InvalidUsageException("Improper usage of the tag {continue}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -358,14 +358,14 @@ class Compiler {
|
||||
* @static
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
* @return string
|
||||
*/
|
||||
public static function tagBreak($tokens, Scope $scope) {
|
||||
if(empty($scope["no-break"])) {
|
||||
return 'break;';
|
||||
} else {
|
||||
throw new ImproperUseException("Improper usage of the tag {break}");
|
||||
throw new InvalidUsageException("Improper usage of the tag {break}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -373,12 +373,14 @@ class Compiler {
|
||||
* Dispatch {extends} tag
|
||||
* @param Tokenizer $tokens
|
||||
* @param Template $tpl
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
* @return string
|
||||
*/
|
||||
public static function tagExtends(Tokenizer $tokens, Template $tpl) {
|
||||
if(!empty($tpl->_extends)) {
|
||||
throw new ImproperUseException("Only one {extends} allowed");
|
||||
throw new InvalidUsageException("Only one {extends} allowed");
|
||||
} elseif($tpl->getStackSize()) {
|
||||
throw new InvalidUsageException("Tags {extends} can not be nested");
|
||||
}
|
||||
$tpl_name = $tpl->parsePlainArg($tokens, $name);
|
||||
if(empty($tpl->_extended)) {
|
||||
@ -446,10 +448,13 @@ class Compiler {
|
||||
* Tag {use ...}
|
||||
* @param Tokenizer $tokens
|
||||
* @param Template $tpl
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
* @return string
|
||||
*/
|
||||
public static function tagUse(Tokenizer $tokens, Template $tpl) {
|
||||
if($tpl->getStackSize()) {
|
||||
throw new InvalidUsageException("Tags {use} can not be nested");
|
||||
}
|
||||
$cname = $tpl->parsePlainArg($tokens, $name);
|
||||
if($name) {
|
||||
$donor = $tpl->getStorage()->getRawTemplate()->load($name, false);
|
||||
@ -469,11 +474,12 @@ class Compiler {
|
||||
$tpl->addDepend($donor);
|
||||
return '?>'.$donor->getBody().'<?php ';
|
||||
} else {
|
||||
$tpl->_compatible = true;
|
||||
return '$donor = $tpl->getStorage()->getTemplate('.$cname.', \Fenom\Template::EXTENDED);'.PHP_EOL.
|
||||
// throw new InvalidUsageException('template name must be given explicitly yet');
|
||||
// under construction
|
||||
$tpl->_compatible = true;
|
||||
return '$donor = $tpl->getStorage()->getTemplate('.$cname.', \Fenom\Template::EXTENDED);'.PHP_EOL.
|
||||
'$donor->fetch((array)$tpl);'.PHP_EOL.
|
||||
'$tpl->b += (array)$donor->b';
|
||||
// throw new ImproperUseException('template name must be given explicitly');
|
||||
}
|
||||
}
|
||||
|
||||
@ -482,9 +488,13 @@ class Compiler {
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @return string
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
*/
|
||||
public static function tagBlockOpen(Tokenizer $tokens, Scope $scope) {
|
||||
if($scope->level > 0) {
|
||||
var_dump("".$scope->tpl);
|
||||
$scope->tpl->_compatible = true;
|
||||
}
|
||||
$scope["cname"] = $scope->tpl->parsePlainArg($tokens, $name);
|
||||
$scope["name"] = $name;
|
||||
}
|
||||
@ -553,7 +563,7 @@ class Compiler {
|
||||
|
||||
public static function tagParent($tokens, Scope $scope) {
|
||||
if(empty($scope->tpl->_extends)) {
|
||||
throw new ImproperUseException("Tag {parent} may be declared in childs");
|
||||
throw new InvalidUsageException("Tag {parent} may be declared in childs");
|
||||
}
|
||||
}
|
||||
|
||||
@ -710,7 +720,7 @@ class Compiler {
|
||||
* @param Tokenizer $tokens
|
||||
* @param Template $tpl
|
||||
* @return string
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
*/
|
||||
public static function tagCycle(Tokenizer $tokens, Template $tpl) {
|
||||
if($tokens->is("[")) {
|
||||
@ -721,7 +731,7 @@ class Compiler {
|
||||
if($tokens->valid()) {
|
||||
$p = $tpl->parseParams($tokens);
|
||||
if(empty($p["index"])) {
|
||||
throw new ImproperUseException("Cycle may contain only index attribute");
|
||||
throw new InvalidUsageException("Cycle may contain only index attribute");
|
||||
} else {
|
||||
return 'echo '.__CLASS__.'::cycle('.$exp.', '.$p["index"].')';
|
||||
}
|
||||
@ -747,7 +757,7 @@ class Compiler {
|
||||
* @param Tokenizer $tokens
|
||||
* @param Template $tpl
|
||||
* @throws UnexpectedTokenException
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
* @return string
|
||||
*/
|
||||
public static function tagImport(Tokenizer $tokens, Template $tpl) {
|
||||
@ -775,7 +785,7 @@ class Compiler {
|
||||
|
||||
$tpl->parsePlainArg($tokens, $name);
|
||||
if(!$name) {
|
||||
throw new ImproperUseException("Invalid usage tag {import}");
|
||||
throw new InvalidUsageException("Invalid usage tag {import}");
|
||||
}
|
||||
if($tokens->is(T_AS)) {
|
||||
$alias = $tokens->next()->get(Tokenizer::MACRO_STRING);
|
||||
@ -812,7 +822,7 @@ class Compiler {
|
||||
*
|
||||
* @param Tokenizer $tokens
|
||||
* @param Scope $scope
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
*/
|
||||
public static function macroOpen(Tokenizer $tokens, Scope $scope) {
|
||||
$scope["name"] = $tokens->get(Tokenizer::MACRO_STRING);
|
||||
@ -832,7 +842,7 @@ class Compiler {
|
||||
if($tokens->is(T_CONSTANT_ENCAPSED_STRING, T_LNUMBER, T_DNUMBER) || $tokens->isSpecialVal()) {
|
||||
$scope["defaults"][ $param ] = $tokens->getAndNext();
|
||||
} else {
|
||||
throw new ImproperUseException("Macro parameters may have only scalar defaults");
|
||||
throw new InvalidUsageException("Macro parameters may have only scalar defaults");
|
||||
}
|
||||
}
|
||||
$tokens->skipIf(',');
|
||||
|
@ -106,6 +106,14 @@ class Template extends Render {
|
||||
$this->_options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get tag stack size
|
||||
* @return int
|
||||
*/
|
||||
public function getStackSize() {
|
||||
return count($this->_stack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load source from provider
|
||||
* @param string $name
|
||||
@ -408,7 +416,7 @@ class Template extends Render {
|
||||
} else {
|
||||
return $code = "echo ".$this->parseExp($tokens).";";
|
||||
}
|
||||
} catch (ImproperUseException $e) {
|
||||
} catch (InvalidUsageException $e) {
|
||||
throw new CompileException($e->getMessage()." in {$this} line {$this->_line}", 0, E_ERROR, $this->_name, $this->_line, $e);
|
||||
} catch (\LogicException $e) {
|
||||
throw new SecurityException($e->getMessage()." in {$this} line {$this->_line}, near '{".$tokens->getSnippetAsString(0,0)."' <- there", 0, E_ERROR, $this->_name, $this->_line, $e);
|
||||
@ -937,7 +945,7 @@ class Template extends Render {
|
||||
*
|
||||
* @param Tokenizer $tokens
|
||||
* @return string
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
*/
|
||||
public function parseConst(Tokenizer $tokens) {
|
||||
$tokens->get('#');
|
||||
@ -956,7 +964,7 @@ class Template extends Render {
|
||||
if(defined($name)) {
|
||||
return $name;
|
||||
} else {
|
||||
throw new ImproperUseException("Use undefined constant $name");
|
||||
throw new InvalidUsageException("Use undefined constant $name");
|
||||
}
|
||||
}
|
||||
|
||||
@ -964,7 +972,7 @@ class Template extends Render {
|
||||
* @param Tokenizer $tokens
|
||||
* @param $name
|
||||
* @return string
|
||||
* @throws ImproperUseException
|
||||
* @throws InvalidUsageException
|
||||
*/
|
||||
public function parseMacro(Tokenizer $tokens, $name) {
|
||||
if(isset($this->macros[ $name ])) {
|
||||
@ -977,13 +985,13 @@ class Template extends Render {
|
||||
} elseif(isset($macro["defaults"][ $arg ])) {
|
||||
$args[ $arg ] = $macro["defaults"][ $arg ];
|
||||
} else {
|
||||
throw new ImproperUseException("Macro '$name' require '$arg' argument");
|
||||
throw new InvalidUsageException("Macro '$name' require '$arg' argument");
|
||||
}
|
||||
}
|
||||
$args = $args ? '$tpl = '.Compiler::toArray($args).';' : '';
|
||||
return '$_tpl = $tpl; '.$args.' ?>'.$macro["body"].'<?php $tpl = $_tpl; unset($_tpl);';
|
||||
} else {
|
||||
throw new ImproperUseException("Undefined macro '$name'");
|
||||
throw new InvalidUsageException("Undefined macro '$name'");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1088,5 +1096,5 @@ class Template extends Render {
|
||||
|
||||
class CompileException extends \ErrorException {}
|
||||
class SecurityException extends CompileException {}
|
||||
class ImproperUseException extends \LogicException {}
|
||||
class InvalidUsageException extends \LogicException {}
|
||||
class ReparseTagException extends \Exception {}
|
Reference in New Issue
Block a user