<commit_before><?php
namespace Web;

use Web\Templates\Scope;

use Core\Resource;

class Template extends PageHandler\PageBase {
	protected $vars = array();
	protected $file;
	protected $name = 'error';
	protected $handler = false;
	
	function __construct($name, $vars = array(), $container = 'HTML') {
		$this->vars = $vars;
		$this->name = $name;
		$this->file = new \File\Instance(static::getPath($name,$container));
		if(!$this->file->Exists()){
			throw new \Exception('Template '.$name.' in '.$container.' doesnt exist');
		}
		
		foreach(array_slice(debug_backtrace(true),1) as $r){
			if(isset($r['object']) && $r['object'] instanceof PageHandler\IPage && !($r['object'] instanceof Template)){
				$this->handler = $r['object'];
				break;
			}
		}
	}
	
	function __call($method,$args){
		if(!method_exists($this->handler,$method)) return '';
		return call_user_func_array(array($this->handler,$method),$args);
	}
	
	function addVarMember($k,$v){
		$this->vars[$k] = $v;
	}
	
	static function adapters(){
		return \ClassLoader::getNSExpression('Web\\Templates\\Adapter\\*');
	}
	static function isSupported($path){
		if(!($path instanceof \File\Instance)){
			$path = new \File\Instance($path);
		}
		
		$handlers = static::adapters();
		foreach($handlers as $class){
			if(class_exists($class)){
				if($class::is($path)){
					return true;
				}
			}
		}
		return false;
	}
	static function getPath($name,$output = 'HTML'){
		global $BASEPATH;
		//Normally we would use the resource handler for this
		//howeaver as well as detirming which part of the system
		//to fetch from we also have a variable extension
		//@todo overriding order
		$expr = $BASEPATH . '*' . DS . 'template' . DS . $output . DS . $name.'.*';
		foreach(glob($expr) as $path){
			if(static::isSupported($path)){
				return $path;
			}
		}
		
	}
	
	static function Exists($name,$output='HTML'){
		return file_exists(static::getPath($name,$output));
	}
	
	function adapter(){	
		$handlers = static::adapters();
		foreach($handlers as $class){
			if(class_exists($class)){
				if($class::is($this->file)){
					return new $class($this->file);
				}
			}
		}
	}
	function GET() {		<commit_msg>Template Fix<commit_after><?php
namespace Web;

use Web\Templates\Scope;

use Core\Resource;

class Template extends PageHandler\PageBase {
	protected $vars = array();
	protected $file;
	protected $name = 'error';
	protected $handler = false;
	
	function __construct($name, $vars = array(), $container = 'HTML') {
		$this->vars = $vars;
		$this->name = $name;
		$this->file = new \File\Instance(static::getPath($name,$container));
		if(!$this->file->Exists()){
			throw new \Exception('Template '.$name.' in '.$container.' doesnt exist');
		}
		
		foreach(array_slice(debug_backtrace(true),1) as $r){
			if(isset($r['object']) && $r['object'] instanceof PageHandler\IPage && !($r['object'] instanceof Template)){
				$this->handler = $r['object'];
				break;
			}
		}
	}
	
	function __call($method,$args){
		if(!method_exists($this->handler,$method)) return '';
		return call_user_func_array(array($this->handler,$method),$args);
	}
	
	function addVarMember($k,$v){
		$this->vars[$k] = $v;
	}
	
	static function adapters(){
		return \ClassLoader::getNSExpression('Web\\Templates\\Adapter\\*');
	}
	static function isSupported($path){
		if(!($path instanceof \File\Instance)){
			$path = new \File\Instance($path);
		}
		
		$handlers = static::adapters();
		foreach($handlers as $class){
			if(class_exists($class)){
				if($class::is($path)){
					return true;
				}
			}
		}
		return false;
	}
	static function getPath($name,$output = 'HTML'){
		global $BASEPATH;
		//Normally we would use the resource handler for this
		//howeaver as well as detirming which part of the system
		//to fetch from we also have a variable extension
		//@todo overriding order
		$expr = $BASEPATH . '*' . DS . 'template' . DS . $output . DS . $name.'.*';
		foreach(glob($expr) as $path){
			if(static::isSupported($path)){
				return $path;
			}
		}
	}
	
	static function Exists($name,$output='HTML'){
		return file_exists(static::getPath($name,$output));
	}
	
	function adapter(){	
		$handlers = static::adapters();
		foreach($handlers as $class){
			if(class_exists($class)){
				if($class::is($this->file)){
					return new $class($this->file);
				}
			}
		}
	}
	function GET() {		
{'fraction_non_alphanumeric': 0.18441678192715538, 'fraction_numerical': 0.0004610419548178884, 'mean_word_length': 3.9930955120828537, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1991618', 'n_tokens_mistral': 1627, 'n_tokens_neox': 1417, 'n_words': 325}