diff --git a/src/Aspect/Scope.php b/src/Aspect/Scope.php index 505fec0..f18ae79 100644 --- a/src/Aspect/Scope.php +++ b/src/Aspect/Scope.php @@ -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"); + } } } \ No newline at end of file diff --git a/tests/cases/Aspect/ScopeTest.php b/tests/cases/Aspect/ScopeTest.php new file mode 100644 index 0000000..636cee3 --- /dev/null +++ b/tests/cases/Aspect/ScopeTest.php @@ -0,0 +1,32 @@ +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 open($tokenizer)." ?>".$content; + $this->assertSame($content, $scope->getContent()); + } +}