Commit 8383d5c8 by pupi1985

Add a simple application and container and update them to use the router

parent a80cc533
...@@ -19,13 +19,7 @@ ...@@ -19,13 +19,7 @@
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\Exceptions\ExceptionHandler; use Q2A\Exceptions\ExceptionHandler;
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: ../../');
...@@ -186,8 +180,8 @@ function qa_get_request_content() ...@@ -186,8 +180,8 @@ function qa_get_request_content()
$firstlower = strtolower($requestparts[0]); $firstlower = strtolower($requestparts[0]);
$routing = qa_page_routing(); $routing = qa_page_routing();
$router = new Router(qa_routing_config()); qa_routing_config();
$route = $router->match($requestlower); $route = app('router')->match($requestlower);
if (isset($route)) { if (isset($route)) {
// use new Controller system // use new Controller system
...@@ -463,23 +457,22 @@ function qa_page_routing() ...@@ -463,23 +457,22 @@ function qa_page_routing()
/** /**
* [qa_routing_config description] * [qa_routing_config description]
* @return Route[]
*/ */
function qa_routing_config() function qa_routing_config()
{ {
return array( $router = app('router');
new Route('user', 'get', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile'), $router->addRoute('user', 'get', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile');
new Route('user-self', 'get', 'user', '\Q2A\Controllers\User\UserProfile', 'index'), $router->addRoute('user', 'post', 'user/{str}', '\Q2A\Controllers\User\UserProfile', 'profile');
new Route('user-wall', 'get', 'user/{str}/wall', '\Q2A\Controllers\User\UserMessages', 'wall'), $router->addRoute('user-self', 'get', 'user', '\Q2A\Controllers\User\UserProfile', 'index');
new Route('user-activity', 'get', 'user/{str}/activity', '\Q2A\Controllers\User\UserPosts', 'activity'), $router->addRoute('user-wall', 'get', 'user/{str}/wall', '\Q2A\Controllers\User\UserMessages', 'wall');
new Route('user-questions', 'get', 'user/{str}/questions', '\Q2A\Controllers\User\UserPosts', 'questions'), $router->addRoute('user-activity', 'get', 'user/{str}/activity', '\Q2A\Controllers\User\UserPosts', 'activity');
new Route('user-answers', 'get', 'user/{str}/answers', '\Q2A\Controllers\User\UserPosts', 'answers'), $router->addRoute('user-questions', 'get', 'user/{str}/questions', '\Q2A\Controllers\User\UserPosts', 'questions');
$router->addRoute('user-answers', 'get', 'user/{str}/answers', '\Q2A\Controllers\User\UserPosts', 'answers');
new Route('user-top', 'get', 'users', '\Q2A\Controllers\User\UsersList', 'top'),
new Route('user-blocked', 'get', 'users/blocked', '\Q2A\Controllers\User\UsersList', 'blocked'), $router->addRoute('user-top', 'get', 'users', '\Q2A\Controllers\User\UsersList', 'top');
new Route('user-new', 'get', 'users/new', '\Q2A\Controllers\User\UsersList', 'newest'), $router->addRoute('user-blocked', 'get', 'users/blocked', '\Q2A\Controllers\User\UsersList', 'blocked');
new Route('user-special', 'get', 'users/special', '\Q2A\Controllers\User\UsersList', 'special'), $router->addRoute('user-new', 'get', 'users/new', '\Q2A\Controllers\User\UsersList', 'newest');
); $router->addRoute('user-special', 'get', 'users/special', '\Q2A\Controllers\User\UsersList', 'special');
} }
......
...@@ -19,11 +19,9 @@ ...@@ -19,11 +19,9 @@
More about this license: http://www.question2answer.org/license.php More about this license: http://www.question2answer.org/license.php
*/ */
define('QA_VERSION', '1.8.0-beta1'); // also used as suffix for .js and .css requests define('QA_VERSION', '1.8.0-beta1'); // also used as suffix for .js and .css requests
define('QA_BUILD_DATE', '2017-09-07'); define('QA_BUILD_DATE', '2017-09-07');
/** /**
* Autoloads some Q2A classes so it's possible to use them without adding a require_once first. From version 1.7 onwards. * Autoloads some Q2A classes so it's possible to use them without adding a require_once first. From version 1.7 onwards.
* These loosely follow PHP-FIG's PSR-0 standard where faux namespaces are separated by underscores. This is being done * These loosely follow PHP-FIG's PSR-0 standard where faux namespaces are separated by underscores. This is being done
...@@ -63,6 +61,10 @@ if (defined('QA_WORDPRESS_LOAD_FILE')) { ...@@ -63,6 +61,10 @@ if (defined('QA_WORDPRESS_LOAD_FILE')) {
} }
require QA_BASE_DIR . 'qa-src/helpers.php';
app();
qa_initialize_constants_2(); qa_initialize_constants_2();
qa_initialize_modularity(); qa_initialize_modularity();
qa_register_core_modules(); qa_register_core_modules();
......
<?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\App;
use Q2A\Http\Router;
class Application
{
/** @var Container */
private $container;
/** @var static */
protected static $instance;
public function __construct()
{
$this->container = new Container();
$this->registerCoreBindings();
}
/**
* Instantiate and fetch the application as a singleton.
* @return static
*/
public static function getInstance()
{
if (is_null(static::$instance)) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Register the bindings used by the core.
*/
private function registerCoreBindings()
{
$this->container->bind('router', new Router());
}
/**
* Return the container instance.
* @return Container
*/
public function getContainer()
{
return $this->container;
}
}
<?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\App;
use Q2A\Exceptions\FatalErrorException;
class Container
{
/** @var array */
protected $bindings = array();
/**
* Bind an object to a key.
* @param mixed $key The key to bind the object to
* @param mixed $object The object to bind to the key
*/
public function bind($key, $object)
{
$this->bindings[$key] = $object;
}
/**
* Return an object bound to the given key. If the key is not found an exception is thrown.
* @param mixed $key The key to look for
* @return mixed
*/
public function resolve($key)
{
if (isset($this->bindings[$key])) {
return $this->bindings[$key];
}
throw new FatalErrorException(sprintf('Key "%s" not bound in container', $key));
}
}
...@@ -21,7 +21,7 @@ namespace Q2A\Http; ...@@ -21,7 +21,7 @@ namespace Q2A\Http;
class Router class Router
{ {
/** @var Route[] */ /** @var Route[] */
protected $routes; protected $routes = array();
/** @var array */ /** @var array */
private $paramsConverter; private $paramsConverter;
...@@ -29,10 +29,8 @@ class Router ...@@ -29,10 +29,8 @@ class Router
/** @var string */ /** @var string */
private $httpMethod; private $httpMethod;
public function __construct($routeData = array()) public function __construct()
{ {
$this->routes = $routeData;
$this->paramsConverter = array( $this->paramsConverter = array(
'{str}' => '([^/]+)', '{str}' => '([^/]+)',
'{int}' => '([0-9]+)', '{int}' => '([0-9]+)',
......
<?php
use Q2A\App\Application;
if (!function_exists('app')) {
/**
* Helper function to access the application object.
* If the $key parameter is null the application instance is returned.
* If the $key parameter is set and the $object parameter is null the container is called to resolve the $key.
* If the $key and the $object parameters are null the container is called to bind the $object to the $key.
* @param mixed $key
* @param mixed $object
* @return mixed
*/
function app($key = null, $object = null)
{
if (is_null($key)) {
return Application::getInstance();
}
if (is_null($object)) {
return Application::getInstance()->getContainer()->resolve($key);
}
Application::getInstance()->getContainer()->bind($key, $object);
}
}
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