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:
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