Commit 89df11c3 by pupi1985

Add a Route class and remove array accesses and checks

parent 4a64df7a
......@@ -19,6 +19,13 @@
More about this license: http://www.question2answer.org/license.php
*/
use Q2A\Controllers\User\UserMessages;
use Q2A\Controllers\User\UserPosts;
use Q2A\Controllers\User\UserProfile;
use Q2A\Controllers\User\UsersList;
use Q2A\Http\Route;
use Q2A\Http\Router;
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../../');
exit;
......@@ -178,14 +185,15 @@ function qa_get_request_content()
$firstlower = strtolower($requestparts[0]);
$routing = qa_page_routing();
$router = new \Q2A\Http\Router(qa_routing_config());
$routeInfo = $router->match($requestlower);
$router = new Router(qa_routing_config());
$route = $router->match($requestlower);
if ($routeInfo !== false) {
if (isset($route)) {
// use new Controller system
qa_set_template($routeInfo['id']);
$ctrl = new $routeInfo['controller']();
$qa_content = call_user_func_array(array($ctrl, $routeInfo['function']), $routeInfo['params']);
qa_set_template($route->getId());
$controllerClass = $route->getController();
$ctrl = new $controllerClass();
$qa_content = call_user_func_array(array($ctrl, $route->getFunction()), $route->getParameters());
} elseif (isset($routing[$requestlower])) {
qa_set_template($firstlower);
......@@ -451,22 +459,22 @@ function qa_page_routing()
/**
* [qa_routing_config description]
* @return [type] [description]
* @return Route[]
*/
function qa_routing_config()
{
return array(
'user' => array('get', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile'),
'user-self' => array('get', 'user', '\Q2A\Controllers\User\UserProfile', 'index'),
'user-wall' => array('get', 'user/{str}/wall', '\Q2A\Controllers\User\UserMessages', 'wall'),
'user-activity' => array('get', 'user/{str}/activity', '\Q2A\Controllers\User\UserPosts', 'activity'),
'user-questions' => array('get', 'user/{str}/questions', '\Q2A\Controllers\User\UserPosts', 'questions'),
'user-answers' => array('get', 'user/{str}/answers', '\Q2A\Controllers\User\UserPosts', 'answers'),
'users-top' => array('get', 'users', '\Q2A\Controllers\User\UsersList', 'top'),
'users-blocked' => array('get', 'users/blocked', '\Q2A\Controllers\User\UsersList', 'blocked'),
'users-new' => array('get', 'users/new', '\Q2A\Controllers\User\UsersList', 'newest'),
'users-special' => array('get', 'users/special', '\Q2A\Controllers\User\UsersList', 'special'),
new Route('user', 'get', 'user/{str}', UserProfile::class, 'profile'),
new Route('user-self', 'get', 'user', UserProfile::class, 'index'),
new Route('user-wall', 'get', 'user/{str}/wall', UserMessages::class, 'wall'),
new Route('user-activity', 'get', 'user/{str}/activity', UserPosts::class, 'activity'),
new Route('user-questions', 'get', 'user/{str}/questions', UserPosts::class, 'questions'),
new Route('user-answers', 'get', 'user/{str}/answers', UserPosts::class, 'answers'),
new Route('user-top', 'get', 'users', UsersList::class, 'top'),
new Route('user-blocked', 'get', 'users/blocked', UsersList::class, 'blocked'),
new Route('user-new', 'get', 'users/new', UsersList::class, 'newest'),
new Route('user-special', 'get', 'users/special', UsersList::class, 'special'),
);
}
......
<?php
/*
Question2Answer by Gideon Greenspan and contributors
http://www.question2answer.org/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
More about this license: http://www.question2answer.org/license.php
*/
namespace Q2A\Http;
class Route
{
/** @var string */
private $id;
/** @var string */
private $httpMethod;
/** @var string */
private $routePath;
/** @var string */
private $controller;
/** @var string */
private $function;
/** @var array */
private $parameters;
public function __construct($id = null, $httpMethod = null, $routePath = null, $controller = null, $function = null)
{
$this->id = $id;
$this->setHttpMethod($httpMethod);
$this->routePath = $routePath;
$this->controller = $controller;
$this->function = $function;
$this->parameters = array();
}
/**
* Bind actual request parameters to the route, replacing any existing ones.
*
* @param array $parameters
*/
public function bindParameters($parameters)
{
$this->parameters = $parameters;
}
/**
* @return string
*/
public function getId()
{
return $this->id;
}
/**
* @param string $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return string
*/
public function getHttpMethod()
{
return $this->httpMethod;
}
/**
* @param string $httpMethod
*/
public function setHttpMethod($httpMethod)
{
$this->httpMethod = strtoupper($httpMethod);
}
/**
* @return string
*/
public function getRoutePath()
{
return $this->routePath;
}
/**
* @param string $routePath
*/
public function setRoutePath($routePath)
{
$this->routePath = $routePath;
}
/**
* @return string
*/
public function getController()
{
return $this->controller;
}
/**
* @param string $controller
*/
public function setController($controller)
{
$this->controller = $controller;
}
/**
* @return string
*/
public function getFunction()
{
return $this->function;
}
/**
* @param string $function
*/
public function setFunction($function)
{
$this->function = $function;
}
/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
}
......@@ -20,7 +20,7 @@ namespace Q2A\Http;
class Router
{
/** @var array */
/** @var Route[] */
protected $routes;
/** @var array */
......@@ -48,34 +48,25 @@ class Router
public function addRoute($id, $httpMethod, $routePath, $class, $func)
{
$this->routes[$id] = array($httpMethod, $routePath, $class, $func);
$this->routes[] = new Route($id, $httpMethod, $routePath, $class, $func);
}
public function match($request)
{
foreach ($this->routes as $id => $route) {
if (count($route) !== 4) {
foreach ($this->routes as $route) {
if ($route->getHttpMethod() !== $this->httpMethod) {
continue;
}
list($httpMethod, $routePath, $callClass, $callFunc) = $route;
if (strtoupper($httpMethod) !== $this->httpMethod) {
continue;
}
$pathRegex = $this->buildPathRegex($routePath);
$pathRegex = $this->buildPathRegex($route->getRoutePath());
if (preg_match($pathRegex, $request, $matches)) {
return array(
'id' => $id,
'controller' => $callClass,
'function' => $callFunc,
'params' => array_slice($matches, 1),
);
$route->bindParameters(array_slice($matches, 1));
return $route;
}
}
return false;
return null;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment