Add namespace.
This commit is contained in:
76
App/Router/Route.php
Normal file
76
App/Router/Route.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php namespace Majestic\App\Router;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
*/
|
||||
|
||||
class Route
|
||||
{
|
||||
|
||||
protected $route;
|
||||
protected $action;
|
||||
protected $params;
|
||||
protected $layout;
|
||||
|
||||
public function __construct($route, $action, $params = array(), $layout = null)
|
||||
{
|
||||
$this->route = $route;
|
||||
$this->action = $action;
|
||||
$this->params = $params;
|
||||
$this->layout = $layout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $request
|
||||
* @return bool
|
||||
*/
|
||||
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)] = urldecode($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] = urldecode($match[$key]);
|
||||
} elseif ($parts[$i] != $request[$i]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getUri()
|
||||
{
|
||||
return '/' . $this->route;
|
||||
}
|
||||
|
||||
public function getAction()
|
||||
{
|
||||
return $this->action . 'Action';
|
||||
}
|
||||
|
||||
public function getLayout()
|
||||
{
|
||||
return $this->layout . 'Layout';
|
||||
}
|
||||
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
}
|
129
App/Router/Router.php
Normal file
129
App/Router/Router.php
Normal file
@ -0,0 +1,129 @@
|
||||
<?php namespace Majestic\App\Router;
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage app
|
||||
* @since 2010-02-25
|
||||
*/
|
||||
|
||||
class Router
|
||||
{
|
||||
/**
|
||||
* @var Route[]
|
||||
*/
|
||||
protected $routes = array();
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $route_name;
|
||||
|
||||
protected $default_layout = 'Default';
|
||||
|
||||
protected $error_layout = 'Error';
|
||||
|
||||
/**
|
||||
* @var Route
|
||||
*/
|
||||
protected $route;
|
||||
|
||||
public function add($name, $route, $action, $params = array(), $layout = null)
|
||||
{
|
||||
if (!$layout) {
|
||||
$layout = $this->default_layout;
|
||||
}
|
||||
$this->routes[$name] = new Route($route, $action, $params, $layout);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @return bool|Route
|
||||
*/
|
||||
public function route($request)
|
||||
{
|
||||
$req = explode('/', trim($request, '/'));
|
||||
|
||||
foreach ($this->routes as $name => $route) {
|
||||
if ($route->match($req)) {
|
||||
$this->route_name = $name;
|
||||
$this->route = $route;
|
||||
\Majestic\Env::setParams($route->getParams());
|
||||
return $this->route;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set default layout name
|
||||
* @param string $layout
|
||||
*/
|
||||
public function setDefaultLayout($layout = 'Default')
|
||||
{
|
||||
$this->default_layout = $layout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of error page layout
|
||||
* @param string $layout
|
||||
*/
|
||||
public function setErrorLayout($layout = 'Error')
|
||||
{
|
||||
$this->error_layout = $layout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns error layout name
|
||||
* @return string Error layout name
|
||||
*/
|
||||
public function getErrorLayout()
|
||||
{
|
||||
return $this->error_layout . 'Layout';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return current router name
|
||||
* @return string
|
||||
*/
|
||||
public function getRouteName()
|
||||
{
|
||||
return $this->route_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null|string $name
|
||||
* @return Route
|
||||
* @throws \ErrorException
|
||||
*/
|
||||
public function getRoute($name = null)
|
||||
{
|
||||
if (is_null($name)) {
|
||||
return $this->route;
|
||||
} else {
|
||||
if ($this->routeIsExists($name)) {
|
||||
return $this->getRouteByName($name);
|
||||
} else {
|
||||
throw new \ErrorException('Unknown route name: "' . $name . '".');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function routeIsExists($name)
|
||||
{
|
||||
return array_key_exists($name, $this->routes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Route
|
||||
*/
|
||||
protected function getRouteByName($name)
|
||||
{
|
||||
return $this->routes[$name];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user