<?php

class mfExceptionhandlerController {
	private $log;
	private $Layout;
	private $Exception;
	
	public function __construct(Exception $e) {
		$this->log=mfLoghandler::singleton();
		$this->Layout=Layout::singleton();
		
		$this->Exception=new mfExceptionhandler($e);
		$this->logException();
		$this->displayException();
	}
	
	private function displayException() {
		$template="mfExceptionhandler/mfExceptionhandler.cli";
		if(defined("mfUI") && mfUI!="cli") {
			$template="mfExceptionhandler/mfExceptionhandler.web";
			$code=$this->Exception->Code;
			if($code) {
				$template="mfExceptionhandler/error_$code";
				if(!file_exists(VIEWDIR."/".$template.".php")) {
					$template="mfExceptionhandler/mfExceptionhandler.web";
				}
			}
		}
		
		$this->Layout->setTemplate($template);
		$this->Layout->set("Exception",$this->Exception);
		$this->Layout->display();
		if($code) {
			exit($code);
		}
		exit(1);
	}
	
	private function logException() {
		$this->log->crit("Unhandled Exception: (".$this->Exception->Code.") ".$this->Exception->Message);
	}
}
