Commit 89df11c3 by pupi1985

Add a Route class and remove array accesses and checks

parent 4a64df7a
...@@ -19,6 +19,13 @@ ...@@ -19,6 +19,13 @@
More about this license: http://www.question2answer.org/license.php 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 if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
header('Location: ../../'); header('Location: ../../');
exit; exit;
...@@ -178,14 +185,15 @@ function qa_get_request_content() ...@@ -178,14 +185,15 @@ function qa_get_request_content()
$firstlower = strtolower($requestparts[0]); $firstlower = strtolower($requestparts[0]);
$routing = qa_page_routing(); $routing = qa_page_routing();
$router = new \Q2A\Http\Router(qa_routing_config()); $router = new Router(qa_routing_config());
$routeInfo = $router->match($requestlower); $route = $router->match($requestlower);
if ($routeInfo !== false) { if (isset($route)) {
// use new Controller system // use new Controller system
qa_set_template($routeInfo['id']); qa_set_template($route->getId());
$ctrl = new $routeInfo['controller'](); $controllerClass = $route->getController();
$qa_content = call_user_func_array(array($ctrl, $routeInfo['function']), $routeInfo['params']); $ctrl = new $controllerClass();
$qa_content = call_user_func_array(array($ctrl, $route->getFunction()), $route->getParameters());
} elseif (isset($routing[$requestlower])) { } elseif (isset($routing[$requestlower])) {
qa_set_template($firstlower); qa_set_template($firstlower);
...@@ -451,22 +459,22 @@ function qa_page_routing() ...@@ -451,22 +459,22 @@ function qa_page_routing()
/** /**
* [qa_routing_config description] * [qa_routing_config description]
* @return [type] [description] * @return Route[]
*/ */
function qa_routing_config() function qa_routing_config()
{ {
return array( return array(
'user' => array('get', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile'), new Route('user', 'get', 'user/{str}', UserProfile::class, 'profile'),
'user-self' => array('get', 'user', '\Q2A\Controllers\User\UserProfile', 'index'), new Route('user-self', 'get', 'user', UserProfile::class, 'index'),
'user-wall' => array('get', 'user/{str}/wall', '\Q2A\Controllers\User\UserMessages', 'wall'), new Route('user-wall', 'get', 'user/{str}/wall', UserMessages::class, 'wall'),
'user-activity' => array('get', 'user/{str}/activity', '\Q2A\Controllers\User\UserPosts', 'activity'), new Route('user-activity', 'get', 'user/{str}/activity', UserPosts::class, 'activity'),
'user-questions' => array('get', 'user/{str}/questions', '\Q2A\Controllers\User\UserPosts', 'questions'), new Route('user-questions', 'get', 'user/{str}/questions', UserPosts::class, 'questions'),
'user-answers' => array('get', 'user/{str}/answers', '\Q2A\Controllers\User\UserPosts', 'answers'), new Route('user-answers', 'get', 'user/{str}/answers', UserPosts::class, 'answers'),
'users-top' => array('get', 'users', '\Q2A\Controllers\User\UsersList', 'top'), new Route('user-top', 'get', 'users', UsersList::class, 'top'),
'users-blocked' => array('get', 'users/blocked', '\Q2A\Controllers\User\UsersList', 'blocked'), new Route('user-blocked', 'get', 'users/blocked', UsersList::class, 'blocked'),
'users-new' => array('get', 'users/new', '\Q2A\Controllers\User\UsersList', 'newest'), new Route('user-new', 'get', 'users/new', UsersList::class, 'newest'),
'users-special' => array('get', 'users/special', '\Q2A\Controllers\User\UsersList', 'special'), 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; ...@@ -20,7 +20,7 @@ namespace Q2A\Http;
class Router class Router
{ {
/** @var array */ /** @var Route[] */
protected $routes; protected $routes;
/** @var array */ /** @var array */
...@@ -48,34 +48,25 @@ class Router ...@@ -48,34 +48,25 @@ class Router
public function addRoute($id, $httpMethod, $routePath, $class, $func) 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) public function match($request)
{ {
foreach ($this->routes as $id => $route) { foreach ($this->routes as $route) {
if (count($route) !== 4) { if ($route->getHttpMethod() !== $this->httpMethod) {
continue;
}
list($httpMethod, $routePath, $callClass, $callFunc) = $route;
if (strtoupper($httpMethod) !== $this->httpMethod) {
continue; continue;
} }
$pathRegex = $this->buildPathRegex($routePath); $pathRegex = $this->buildPathRegex($route->getRoutePath());
if (preg_match($pathRegex, $request, $matches)) { if (preg_match($pathRegex, $request, $matches)) {
return array( $route->bindParameters(array_slice($matches, 1));
'id' => $id,
'controller' => $callClass, return $route;
'function' => $callFunc,
'params' => array_slice($matches, 1),
);
} }
} }
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