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:
51
app/Action.php
Normal file
51
app/Action.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Action
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->extractParams();
|
||||
$this->execute();
|
||||
}
|
||||
|
||||
protected function extractParams()
|
||||
{
|
||||
$params = FrontController::getInstance()->getRouter()->getRoute()->getParams();
|
||||
if ($params) {
|
||||
foreach ($params as $name => $value) {
|
||||
if (is_string($name)) {
|
||||
$this->$name = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract protected function execute();
|
||||
|
||||
/**
|
||||
* Redirect
|
||||
*
|
||||
* @param mixed $url
|
||||
* @param mixed $relative Default to true
|
||||
*/
|
||||
protected function redirect($url = null, $relative = true)
|
||||
{
|
||||
$url = ($url) ? $url : Env::getRequestUri();
|
||||
|
||||
if ($relative) {
|
||||
$url = FrontController::getInstance()->getBaseUrl() . $url;
|
||||
}
|
||||
header('Location: ' . $url);
|
||||
exit();
|
||||
}
|
||||
}
|
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;
|
||||
}
|
||||
}
|
||||
}
|
25
app/StaticViewAction.php
Normal file
25
app/StaticViewAction.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class StaticViewAction extends ViewAction
|
||||
{
|
||||
|
||||
protected function getTemplate()
|
||||
{
|
||||
$template = ($this->template) ? $this->template : substr(get_class($this), 0, -6/*strlen('Action')*/);
|
||||
return '/static/' . $template;
|
||||
}
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
return $this->view->fetch($this->getTemplate());
|
||||
}
|
||||
}
|
41
app/ViewAction.php
Normal file
41
app/ViewAction.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class ViewAction extends Action
|
||||
{
|
||||
|
||||
protected $template;
|
||||
|
||||
/**
|
||||
* @var PHPView
|
||||
*/
|
||||
protected $view;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->view = FrontController::getInstance()->getView();
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function getTemplate()
|
||||
{
|
||||
$class = get_class($this);
|
||||
$template = ($this->template) ? $this->template : substr($class, 0, -6/*strlen('Action')*/);
|
||||
$dir = array_slice(explode('/', Load::getFilePath($class)), -2, 1);
|
||||
return '/actions/' . array_pop($dir) . '/' . $template;
|
||||
}
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
$this->view->assignObject($this);
|
||||
return $this->view->fetch($this->getTemplate());
|
||||
}
|
||||
}
|
72
app/router/Route.php
Normal file
72
app/router/Route.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Route
|
||||
{
|
||||
|
||||
protected $route;
|
||||
protected $action;
|
||||
protected $params;
|
||||
protected $layout;
|
||||
|
||||
public function __construct($route, $action, $params = null, $layout = null)
|
||||
{
|
||||
$this->route = $route;
|
||||
$this->action = $action;
|
||||
$this->params = $params;
|
||||
$this->layout = $layout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $request
|
||||
*/
|
||||
public function match($request)
|
||||
{
|
||||
$parts = explode('/', $this->route);
|
||||
$cnt = count($parts);
|
||||
|
||||
if(count($request) != $cnt) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $cnt; $i++) {
|
||||
if (substr($parts[$i], 0, 1) == ':') {
|
||||
$this->params[substr($parts[$i], 1)] = $request[$i];
|
||||
} elseif (substr($parts[$i], 0, 2) == '(?') {
|
||||
$match = array();
|
||||
if (!preg_match('#^' . $parts[$i] . '$#iu', $request[$i], $match)) {
|
||||
return false;
|
||||
}
|
||||
$start = strpos($parts[$i], '<') + 1;
|
||||
$key = substr($parts[$i], $start, strpos($parts[$i], '>', $start) - $start);
|
||||
$this->params[$key] = $match[$key];
|
||||
} elseif ($parts[$i] != $request[$i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getAction()
|
||||
{
|
||||
return $this->action . 'Action';
|
||||
}
|
||||
|
||||
public function getLayout()
|
||||
{
|
||||
return $this->layout . 'Layout';
|
||||
}
|
||||
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
}
|
65
app/router/Router.php
Normal file
65
app/router/Router.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class Router
|
||||
{
|
||||
|
||||
protected $routes = array();
|
||||
|
||||
protected $route_name;
|
||||
|
||||
protected $default_layout = 'Default';
|
||||
|
||||
/**
|
||||
* @var Route
|
||||
*/
|
||||
protected $route;
|
||||
|
||||
public function add($name, $route, $action, $params = null, $layout = null)
|
||||
{
|
||||
if (! $layout) {
|
||||
$layout = $this->default_layout;
|
||||
}
|
||||
$this->routes[$name] = new Route($route, $action, $params, $layout);
|
||||
}
|
||||
|
||||
public function route($request)
|
||||
{
|
||||
$req = explode('/', trim($request, '/'));
|
||||
|
||||
foreach ($this->routes as $name => $route) {
|
||||
if ($route->match($req)) {
|
||||
$this->route_name = $route;
|
||||
$this->route = $route;
|
||||
return $this->route;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setDefaultLayout($layout = 'Default')
|
||||
{
|
||||
$this->default_layout = $layout;
|
||||
}
|
||||
|
||||
public function getRouteName()
|
||||
{
|
||||
return $this->route_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Route
|
||||
*/
|
||||
public function getRoute()
|
||||
{
|
||||
return $this->route;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user