mirror of
https://github.com/fenom-template/fenom.git
synced 2023-08-10 21:13:07 +03:00
Improve scope object
This commit is contained in:
parent
1ed2ed90aa
commit
7c83fe6460
@ -6,8 +6,10 @@ namespace Aspect;
|
|||||||
*/
|
*/
|
||||||
class Scope extends \ArrayObject {
|
class Scope extends \ArrayObject {
|
||||||
|
|
||||||
|
public $id = 0;
|
||||||
public $line = 0;
|
public $line = 0;
|
||||||
public $name;
|
public $name;
|
||||||
|
public $level = 0;
|
||||||
/**
|
/**
|
||||||
* @var Template
|
* @var Template
|
||||||
*/
|
*/
|
||||||
@ -16,30 +18,51 @@ class Scope extends \ArrayObject {
|
|||||||
public $is_next_close = false;
|
public $is_next_close = false;
|
||||||
public $is_compiler = true;
|
public $is_compiler = true;
|
||||||
private $_action;
|
private $_action;
|
||||||
|
private static $count = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param Template $tpl
|
* @param Template $tpl
|
||||||
* @param int $line
|
* @param int $line
|
||||||
* @param array $action
|
* @param array $action
|
||||||
|
* @param int $level
|
||||||
*/
|
*/
|
||||||
public function __construct($name, $tpl, $line, $action) {
|
public function __construct($name, $tpl, $line, $action, $level) {
|
||||||
|
$this->id = ++self::$count;
|
||||||
$this->line = $line;
|
$this->line = $line;
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
$this->tpl = $tpl;
|
$this->tpl = $tpl;
|
||||||
$this->_action = $action;
|
$this->_action = $action;
|
||||||
|
$this->level = $level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $function
|
||||||
|
*/
|
||||||
public function setFuncName($function) {
|
public function setFuncName($function) {
|
||||||
$this["function"] = $function;
|
$this["function"] = $function;
|
||||||
$this->is_compiler = false;
|
$this->is_compiler = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Open callback
|
||||||
|
*
|
||||||
|
* @param Tokenizer $tokenizer
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
public function open($tokenizer) {
|
public function open($tokenizer) {
|
||||||
return call_user_func($this->_action["open"], $tokenizer, $this);
|
return call_user_func($this->_action["open"], $tokenizer, $this)." /*#{$this->id}#*/";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function hasTag($tag, $level) {
|
/**
|
||||||
|
* Check, has the block this tag
|
||||||
|
*
|
||||||
|
* @param string $tag
|
||||||
|
* @param int $level
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function hasTag($tag, $level) {
|
||||||
if(isset($this->_action["tags"][$tag])) {
|
if(isset($this->_action["tags"][$tag])) {
|
||||||
if($level) {
|
if($level) {
|
||||||
return isset($this->_action["float_tags"][$tag]);
|
return isset($this->_action["float_tags"][$tag]);
|
||||||
@ -50,20 +73,39 @@ class Scope extends \ArrayObject {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tag($tag, $tokenizer) {
|
/**
|
||||||
|
* Call tag callback
|
||||||
|
*
|
||||||
|
* @param string $tag
|
||||||
|
* @param Tokenizer $tokenizer
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function tag($tag, $tokenizer) {
|
||||||
return call_user_func($this->_action["tags"][$tag], $tokenizer, $this);
|
return call_user_func($this->_action["tags"][$tag], $tokenizer, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close callback
|
||||||
|
*
|
||||||
|
* @param Tokenizer $tokenizer
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function close($tokenizer) {
|
public function close($tokenizer) {
|
||||||
return call_user_func($this->_action["close"], $tokenizer, $this);
|
return call_user_func($this->_action["close"], $tokenizer, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Count chars to close tag
|
* Return content of block
|
||||||
* @todo
|
*
|
||||||
* @return int
|
* @throws \LogicException
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getDistanceToClose() {
|
public function getContent() {
|
||||||
return 1;
|
if($pos = strpos($this->tpl->_body, "/*#{$this->id}#*/")) {
|
||||||
|
$begin = strpos($this->tpl->_body, "?>", $pos);
|
||||||
|
return substr($this->tpl->_body, $begin + 2);
|
||||||
|
} else {
|
||||||
|
throw new \LogicException("Trying get content of non-block scope");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
32
tests/cases/Aspect/ScopeTest.php
Normal file
32
tests/cases/Aspect/ScopeTest.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
namespace Aspect;
|
||||||
|
|
||||||
|
class ScopeTest extends TestCase {
|
||||||
|
public function openTag($tokenizer, $scope) {
|
||||||
|
$this->assertInstanceOf('Aspect\Tokenizer', $tokenizer);
|
||||||
|
$this->assertInstanceOf('Aspect\Scope', $scope);
|
||||||
|
$scope["value"] = true;
|
||||||
|
return "open-tag";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function closeTag($tokenizer, $scope) {
|
||||||
|
$this->assertInstanceOf('Aspect\Tokenizer', $tokenizer);
|
||||||
|
$this->assertInstanceOf('Aspect\Scope', $scope);
|
||||||
|
$this->assertTrue($scope["value"]);
|
||||||
|
return "close-tag";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testBlock() {
|
||||||
|
$scope = new Scope($this->aspect, new Template($this->aspect), 1, array(
|
||||||
|
"open" => array($this, "openTag"),
|
||||||
|
"close" => array($this, "closeTag")
|
||||||
|
), 0);
|
||||||
|
$tokenizer = new Tokenizer("1+1");
|
||||||
|
$this->assertSame("open-tag /*#{$scope->id}#*/", $scope->open($tokenizer));
|
||||||
|
$this->assertSame("close-tag", $scope->close($tokenizer));
|
||||||
|
|
||||||
|
$content = " some ?> content\n\nwith /*#9999999#*/ many\n\tlines";
|
||||||
|
$scope->tpl->_body = "start <?php ".$scope->open($tokenizer)." ?>".$content;
|
||||||
|
$this->assertSame($content, $scope->getContent());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user