Code refactoring, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@114 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
127
app/FrontController.php
Normal file
127
app/FrontController.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user