Improve scope object

This commit is contained in:
bzick 2013-02-20 18:12:44 +04:00
parent 1ed2ed90aa
commit 7c83fe6460
2 changed files with 83 additions and 9 deletions

View File

@ -6,8 +6,10 @@ namespace Aspect;
*/
class Scope extends \ArrayObject {
public $id = 0;
public $line = 0;
public $name;
public $level = 0;
/**
* @var Template
*/
@ -16,30 +18,51 @@ class Scope extends \ArrayObject {
public $is_next_close = false;
public $is_compiler = true;
private $_action;
private static $count = 0;
/**
* @param string $name
* @param Template $tpl
* @param int $line
* @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->name = $name;
$this->tpl = $tpl;
$this->_action = $action;
$this->level = $level;
}
/**
*
* @param string $function
*/
public function setFuncName($function) {
$this["function"] = $function;
$this->is_compiler = false;
}
/**
* Open callback
*
* @param Tokenizer $tokenizer
* @return mixed
*/
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($level) {
return isset($this->_action["float_tags"][$tag]);
@ -50,20 +73,39 @@ class Scope extends \ArrayObject {
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);
}
/**
* Close callback
*
* @param Tokenizer $tokenizer
* @return string
*/
public function close($tokenizer) {
return call_user_func($this->_action["close"], $tokenizer, $this);
}
/**
* Count chars to close tag
* @todo
* @return int
* Return content of block
*
* @throws \LogicException
* @return string
*/
public function getDistanceToClose() {
return 1;
public function getContent() {
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");
}
}
}

View 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());
}
}