You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
2.7 KiB

<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage app
* @since 2010-02-24
* @version SVN: $Id$
* @filesource $URL$
*/
class FrontController
{
/**
* @var Router
*/
protected $router;
/**
* @var string
*/
protected $view = 'PHPView';
/**
* @var string
*/
protected $view_path;
protected $base_url = '';
/**
* @var FrontController
*/
protected static $instance;
private function __construct()
{
$this->router = new Router();
}
private function __clone(){}
/**
* @return FrontController
*/
static public function getInstance()
{
if (! isset(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @param string $path
* @return FrontController
*/
public function setViewPath($path)
{
$this->view_path = $path;
return $this;
}
/**
* @param string $view
* @return FrontController
*/
public function setView($view)
{
$this->view = $view;
return $this;
}
/**
*
* @return iView
*/
public function getView($view = null)
{
$view = ($view) ? $view : $this->view;
return new $view($this->view_path);
}
/**
* @param string $url
*/
public function setBaseUrl($url)
{
$this->base_url = rtrim($url, '/');
return $this;
}
public function getBaseUrl()
{
return $this->base_url;
}
/**
* @return Router
*/
public function getRouter()
{
return $this->router;
}
public function execute()
{
try {
try {
$request = Env::getRequestUri();
$route = $this->getRouter()->route($request);
if (!$route) {
throw new Exception('Route "' . $request . '" not found');
}
$action_class = $route->getAction();
$action = new $action_class();
$layout_class = $route->getLayout();
$layout = new $layout_class();
return $layout->fetch($action);
} catch (GeneralException $e) {
throw $e;
} catch (Exception $e) {
throw new GeneralException($e->getMessage(), $e->getCode());
}
} catch (Exception $e) {
if (DEBUG == true) {
return $e->toHtml();
}
}
}
}