fenom/src/Aspect/Scope.php

111 lines
2.5 KiB
PHP
Raw Normal View History

2013-01-25 18:36:16 +04:00
<?php
namespace Aspect;
/**
* Scope for blocks tags
*/
class Scope extends \ArrayObject {
2013-02-20 18:12:44 +04:00
public $id = 0;
public $line = 0;
public $name;
2013-02-20 18:12:44 +04:00
public $level = 0;
/**
* @var Template
*/
public $tpl;
2013-01-25 18:36:16 +04:00
public $closed = false;
public $is_next_close = false;
public $is_compiler = true;
private $_action;
2013-02-20 18:12:44 +04:00
private static $count = 0;
2013-01-25 18:36:16 +04:00
/**
* @param string $name
* @param Template $tpl
* @param int $line
* @param array $action
2013-02-20 18:12:44 +04:00
* @param int $level
2013-01-25 18:36:16 +04:00
*/
2013-02-20 18:12:44 +04:00
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;
}
2013-01-25 18:36:16 +04:00
2013-02-20 18:12:44 +04:00
/**
*
* @param string $function
*/
2013-01-25 18:36:16 +04:00
public function setFuncName($function) {
$this["function"] = $function;
$this->is_compiler = false;
}
2013-02-20 18:12:44 +04:00
/**
* Open callback
*
* @param Tokenizer $tokenizer
* @return mixed
*/
2013-01-25 18:36:16 +04:00
public function open($tokenizer) {
2013-02-20 18:12:44 +04:00
return call_user_func($this->_action["open"], $tokenizer, $this)." /*#{$this->id}#*/";
2013-01-25 18:36:16 +04:00
}
2013-02-20 18:12:44 +04:00
/**
* 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])) {
2013-01-25 18:36:16 +04:00
if($level) {
return isset($this->_action["float_tags"][$tag]);
} else {
return true;
}
}
return false;
}
2013-01-25 18:36:16 +04:00
2013-02-20 18:12:44 +04:00
/**
* 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);
}
2013-01-25 18:36:16 +04:00
2013-02-20 18:12:44 +04:00
/**
* Close callback
*
* @param Tokenizer $tokenizer
* @return string
*/
2013-01-25 18:36:16 +04:00
public function close($tokenizer) {
return call_user_func($this->_action["close"], $tokenizer, $this);
}
/**
2013-02-20 18:12:44 +04:00
* Return content of block
*
* @throws \LogicException
* @return string
2013-01-25 18:36:16 +04:00
*/
2013-02-20 18:12:44 +04:00
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");
}
2013-01-25 18:36:16 +04:00
}
}