Location: PHPKode > projects > Simple Way to Usenet > swun/libs/core/parser/block_tag.class.php
<?php
/**
 *  
 * @author  Benjamin Gillissen <hide@address.com>
 * 
 *	**************************************************************

	Copyright (C) 2009  Benjamin Gillissen
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details at:
	http://www.gnu.org/copyleft/gpl.html

 *	**************************************************************
 */
class block_tag {
	
	
	private static $open = '{--';
	private static $close= '--}';
	private static $match= "#(\{--)([\w\W]+?)(--})#";
	private static $typek= 2;
	private static $argsk= 4;
	private static $close_match= "#(\{--)([\w\W]+?)(-END--})#";
	
	private $HDL;
	private $pos;		//tag start offset
	private $size;		//tag size
	private $tag;
	
	public function __construct($hdl, $pos){
		if ( !is_resource($hdl) ){
			errors::raise('Expect a valid file ressource as first parameters', CORE_LOG_FATAL, 'TPL'); 
			return; 
		}
		$this->HDL = $hdl;
		$this->pos = $pos;
	}
	
	public static function openlen(){ return 3; }
	public static function closelen(){ return 3; }
	
	public static function open(){ return '{--'; }
	public static function close(){ return '--}'; }
	
	public function start(){ return $this->pos; }
	public function end(){ 
		if ( !isset($this->tag) ){ $this->detect(); }
		if ( FALSE === $this->tag ){ return FALSE; }
		return ($this->pos + strlen($this->tag)); 
	} 
	
	public function setsize($s){ $this->size=$s;}
	
	public function size(){
		if ( !isset($this->size) ){ $this->detect(); }
		return $this->size;
	}
	
	public function type(){ 
		if (!isset($this->tag) ){ $this->read(); }
		if ( FALSE === $this->tag ){ return FALSE; }
		$c = preg_match_all(self::$close_match, $this->tag, $result);
		if ( $c !== 0 ){ return 'close'; }
		$c = preg_match_all(self::$match, $this->tag, $result);
		if ( $c !== 0 ){ return 'open'; }
		return FALSE;
	}
	
	public function plugin(){
		if (!isset($this->tag) ){ $this->read(); }
		if ( FALSE === $this->tag ){ return FALSE; }
		$c = preg_match_all(self::$match, $this->tag, $result);
		if ( $c !== 1 ){ return FALSE; }
		$result = split('=', $result[2][0]);
		return str_replace('-', '_', strtolower(trim($result[0])));
	}
	
	public function detect(){
		//we do want to parse our open tag again ;)
		fseek($this->HDL, $this->pos + self::openlen() );
		//echo "Searching Tag End From Tag Start @ ".ftell($this->HDL)."<br/>\n";
		//errors::raise("Searching Tag End From Tag Start @ ".ftell($this->HDL), CORE_LOG_NOTICE, 'TPL');
		$ob='';		//to check for --}
		$cb='';		//tocheck for {--
		while( TRUE !== feof($this->HDL) ){
			$char = fread($this->HDL, 1);
			$ob .= $char;
			$cb .= $char;
			if ( strlen($ob) > self::openlen() ){ $ob = substr($ob, 1); }
			if ( strlen($cb) > self::closelen() ){ $cb = substr($cb, 1); }
			if ( $ob === self::open() ){ $this->size=FALSE;return FALSE; }	//tag open before tagclose
			if ( $cb === self::close() ){
				//echo "Tag End Found @ ".ftell($this->HDL)."<br/>\n";
				$this->size = ( ftell($this->HDL) - $this->pos );
				return TRUE; 
			}
		}
		errors::raise('Unexpected end of file while detecting block end @ '.$this->pos, CORE_LOG_ERROR, 'TPL');
		$this->size=FALSE;
		return FALSE; 		
	}
	
	public function read(){
		if ( isset($this->tag) ){ return $this->tag; }
		if ( !isset($this->size) ){ $this->detect(); }
		if ( FALSE === $this->size ){ $this->tag = FALSE;return FALSE; }
		//let's move cursor back to tag_open to read it once 
		fseek($this->HDL, $this->pos);
		//echo "Reading Tag From $this->pos to ".($this->pos + $this->size)." ($this->size)<br/>\n";
		$this->tag = fread($this->HDL, $this->size);
		return $this->tag;
	}
	
}
Return current item: Simple Way to Usenet