<?php
namespace elanpl\L3;
class Application{
public $config;
public $request;
public $response;
public $router;
public $serialization;
public $viewEngines;
public $baseDirectory;
public $controllersDirectory;
public $viewsDirectory;
public $applicationDirectory;
public $controller;
public $module;
public $action;
public function __construct($config){
$this->config = $config;
$this->baseDirectory = $config->getBaseDirectory();
$this->controllersDirectory = $config->getControllersDirectory();
$this->viewsDirectory = $config->getViewsDirectory();
$this->applicationDirectory = $config->getApplicationDirectory();
$this->request = $this->config->getRequest();
$this->router = new Router($this->config->getRouting());
$this->response = new Response();
$this->serialization = new Serialization();
$this->viewEngines = new ViewEngine();
$this->config->configure($this);
}
public function basePath(){
return strstr($_SERVER['REQUEST_URI'], $this->request->path, true);
}
public function getServerURL(){
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://{$_SERVER['HTTP_HOST']}";
}
public function getURL(){
return $this->getServerURL().$_SERVER['REQUEST_URI'];
}
public function relPath($file){
return ($this->router->depth>0 ? str_repeat("../", substr($this->request->path,-1)=='/'?$this->router->depth:$this->router->depth-1) : "" ).$file;
}
public function link($name, $parameters){
return $this->router->link($name, $parameters);
}
public function findControllerInCallStack($search_level=5){
$controller = "";
$module = "";
$action = "";
$match = array();
$call_stack = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $search_level);
for($i=0; $i<$search_level; $i++){
if(preg_match(
$pattern = '#('.$this->applicationDirectory.')(\\\\(?P<module>.+))?\\\\'.$this->controllersDirectory.'\\\\(?P<controller>.+)#',
$call_stack[$i]['class'],
$match
)
){
$action = $call_stack[$i]['function'];
$module = $match['module'];
$controller = $match['controller'];
break;
}
}
return array(
'module' => $module,
'controller' => $controller,
'action' => $action
);
}