<?php
namespace elanpl\L3;
class View{
protected $viewFileExtension;
protected $viewFile;
protected $context;
public $_L3;
public function __construct(){
$numargs = func_num_args();
if ($numargs == 0) {
$view = "";
$context = array();
}
else if ($numargs == 1) {
$arg = func_get_arg(0);
if(is_object($arg) && $arg instanceof ViewModel){
if(method_exists($arg, 'getContext')){
$context = $arg->getContext();
}
else{
$context = array();
$context['vm'] = $arg;
}
if(method_exists($arg, 'getView')){
$view = $arg->getView();
}
else{
$view = $arg;
}
if(isset($arg->_L3)){
$this->_L3 = $arg->_L3;
}
}
else{
$view = "";
$context = $arg;
}
}
else {
$arg = func_get_arg(0);
if($arg instanceof Application){
$this->_L3 = $arg;
$view = func_get_arg(1);
$context = func_get_arg(2);
}
else{
$view = func_get_arg(0);
$context = func_get_arg(1);
}
}
if(!isset($this->_L3)){
global $_L3;
$this->_L3 = $_L3;
}
if($this->viewFile = $this->find($view)){
$this->viewFileExtension = ".".pathinfo($this->viewFile, PATHINFO_EXTENSION);
}
else{
throw new \Exception("View file:\"$view\" not found!");
}
$this->context = $context;
}
public function render(){
if ($viewEngineClass = $this->_L3->viewEngines::get($this->viewFileExtension)){
$view = new $viewEngineClass();
return $view->render($this->viewFile, $this->context);
}
else{
throw new \Exception("View Engine not found for the view file extension:\"".$this->viewFileExtension."\"!");
}
}
public function find($view){
if(is_string($view) && is_file($view)){
return $view;
}
else{
if(is_object($view) && $view instanceof ViewModel){
$controller = $view->getController();
$module = $view->getModule();
$action = $view->getAction();
$view = "";
}
else{
$match = $this->_L3->findControllerInCallStack();