127 lines
2.4 KiB
PHP
127 lines
2.4 KiB
PHP
![]() |
<?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 {
|
||
|
$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 (Exception $e) {
|
||
|
throw $e;
|
||
|
}
|
||
|
}
|
||
|
}
|