Commit 4fd88051 by Scott

Add dependency injection to Controllers

parent be4f6d21
...@@ -187,7 +187,7 @@ function qa_get_request_content() ...@@ -187,7 +187,7 @@ function qa_get_request_content()
if ($route !== null) { if ($route !== null) {
qa_set_template($route->getOption('template')); qa_set_template($route->getOption('template'));
$controllerClass = $route->getController(); $controllerClass = $route->getController();
$ctrl = new $controllerClass(); $ctrl = new $controllerClass(qa_service('database'));
$qa_content = $ctrl->executeAction($route->getAction(), $route->getParameters()); $qa_content = $ctrl->executeAction($route->getAction(), $route->getParameters());
} }
......
...@@ -18,15 +18,16 @@ ...@@ -18,15 +18,16 @@
namespace Q2A\Controllers\Admin; namespace Q2A\Controllers\Admin;
use Q2A\Auth\NoPermissionException; use Q2A\Database\DbConnection;
use Q2A\Controllers\BaseController;
use Q2A\Middleware\Auth\MinimumUserLevel; use Q2A\Middleware\Auth\MinimumUserLevel;
/** /**
* Controller for admin page for settings about user points * Controller for admin page for settings about user points
*/ */
class Points extends \Q2A\Controllers\BaseController class Points extends BaseController
{ {
public function __construct() public function __construct(DbConnection $db)
{ {
require_once QA_INCLUDE_DIR . 'db/recalc.php'; require_once QA_INCLUDE_DIR . 'db/recalc.php';
require_once QA_INCLUDE_DIR . 'db/points.php'; require_once QA_INCLUDE_DIR . 'db/points.php';
...@@ -34,7 +35,7 @@ class Points extends \Q2A\Controllers\BaseController ...@@ -34,7 +35,7 @@ class Points extends \Q2A\Controllers\BaseController
require_once QA_INCLUDE_DIR . 'app/admin.php'; require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'util/sort.php'; require_once QA_INCLUDE_DIR . 'util/sort.php';
parent::__construct(); parent::__construct($db);
$this->addMiddleware(new MinimumUserLevel(QA_USER_LEVEL_ADMIN)); $this->addMiddleware(new MinimumUserLevel(QA_USER_LEVEL_ADMIN));
} }
......
...@@ -18,22 +18,23 @@ ...@@ -18,22 +18,23 @@
namespace Q2A\Controllers\Admin; namespace Q2A\Controllers\Admin;
use Q2A\Auth\NoPermissionException; use Q2A\Controllers\BaseController;
use Q2A\Database\DbConnection;
use Q2A\Middleware\Auth\MinimumUserLevel; use Q2A\Middleware\Auth\MinimumUserLevel;
/** /**
* Controller for admin page showing usage statistics and clean-up buttons * Controller for admin page showing usage statistics and clean-up buttons
*/ */
class Stats extends \Q2A\Controllers\BaseController class Stats extends BaseController
{ {
public function __construct() public function __construct(DbConnection $db)
{ {
require_once QA_INCLUDE_DIR . 'db/recalc.php'; require_once QA_INCLUDE_DIR . 'db/recalc.php';
require_once QA_INCLUDE_DIR . 'app/admin.php'; require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'db/admin.php'; require_once QA_INCLUDE_DIR . 'db/admin.php';
require_once QA_INCLUDE_DIR . 'app/format.php'; require_once QA_INCLUDE_DIR . 'app/format.php';
parent::__construct(); parent::__construct($db);
$this->addMiddleware(new MinimumUserLevel(QA_USER_LEVEL_ADMIN)); $this->addMiddleware(new MinimumUserLevel(QA_USER_LEVEL_ADMIN));
} }
......
...@@ -18,17 +18,18 @@ ...@@ -18,17 +18,18 @@
namespace Q2A\Controllers; namespace Q2A\Controllers;
use Q2A\Database\DbConnection;
use Q2A\Middleware\BaseMiddleware; use Q2A\Middleware\BaseMiddleware;
abstract class BaseController abstract class BaseController
{ {
/** @var BaseMiddleware[string] */ /** @var BaseMiddleware[string] */
private $middleware; protected $middleware = [];
protected $db;
public function __construct() public function __construct(DbConnection $db)
{ {
// TODO: constructor taking Database class parameter $this->db = $db;
$this->middleware = array();
} }
/** /**
......
...@@ -19,18 +19,20 @@ ...@@ -19,18 +19,20 @@
namespace Q2A\Controllers\User; namespace Q2A\Controllers\User;
use Q2A\Auth\NoPermissionException; use Q2A\Auth\NoPermissionException;
use Q2A\Controllers\BaseController;
use Q2A\Database\DbConnection;
/** /**
* Controller for page showing recent activity for an IP address * Controller for page showing recent activity for an IP address
*/ */
class Ip extends \Q2A\Controllers\BaseController class Ip extends BaseController
{ {
public function __construct() public function __construct(DbConnection $db)
{ {
require_once QA_INCLUDE_DIR . 'db/selects.php'; require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/format.php'; require_once QA_INCLUDE_DIR . 'app/format.php';
parent::__construct(); parent::__construct($db);
} }
public function address($ip) public function address($ip)
......
...@@ -18,17 +18,19 @@ ...@@ -18,17 +18,19 @@
namespace Q2A\Controllers\User; namespace Q2A\Controllers\User;
use Q2A\Controllers\BaseController;
use Q2A\Database\DbConnection;
use Q2A\Http\Exceptions\PageNotFoundException; use Q2A\Http\Exceptions\PageNotFoundException;
use Q2A\Middleware\Auth\InternalUsersOnly; use Q2A\Middleware\Auth\InternalUsersOnly;
class UserMessages extends \Q2A\Controllers\BaseController class UserMessages extends BaseController
{ {
public function __construct() public function __construct(DbConnection $db)
{ {
require_once QA_INCLUDE_DIR . 'db/selects.php'; require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/messages.php'; require_once QA_INCLUDE_DIR . 'app/messages.php';
parent::__construct(); parent::__construct($db);
$this->addMiddleware(new InternalUsersOnly()); $this->addMiddleware(new InternalUsersOnly());
} }
......
...@@ -18,19 +18,23 @@ ...@@ -18,19 +18,23 @@
namespace Q2A\Controllers\User; namespace Q2A\Controllers\User;
use Q2A\Controllers\BaseController;
use Q2A\Database\DbConnection;
use Q2A\Http\Exceptions\PageNotFoundException; use Q2A\Http\Exceptions\PageNotFoundException;
class UserPosts extends \Q2A\Controllers\BaseController class UserPosts extends BaseController
{ {
protected $userid; protected $userid;
protected $userhtml; protected $userhtml;
public function __construct() public function __construct(DbConnection $db)
{ {
require_once QA_INCLUDE_DIR . 'db/users.php'; require_once QA_INCLUDE_DIR . 'db/users.php';
require_once QA_INCLUDE_DIR . 'db/selects.php'; require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/users.php'; require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/format.php'; require_once QA_INCLUDE_DIR . 'app/format.php';
parent::__construct($db);
} }
/** /**
......
...@@ -18,19 +18,23 @@ ...@@ -18,19 +18,23 @@
namespace Q2A\Controllers\User; namespace Q2A\Controllers\User;
use Q2A\Controllers\BaseController;
use Q2A\Database\DbConnection;
use Q2A\Http\Exceptions\PageNotFoundException; use Q2A\Http\Exceptions\PageNotFoundException;
class UserProfile extends \Q2A\Controllers\BaseController class UserProfile extends BaseController
{ {
private $userid; private $userid;
private $userhtml; private $userhtml;
public function __construct() public function __construct(DbConnection $db)
{ {
require_once QA_INCLUDE_DIR . 'db/users.php'; require_once QA_INCLUDE_DIR . 'db/users.php';
require_once QA_INCLUDE_DIR . 'db/selects.php'; require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/users.php'; require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/format.php'; require_once QA_INCLUDE_DIR . 'app/format.php';
parent::__construct($db);
} }
public function index() public function index()
......
...@@ -19,19 +19,21 @@ ...@@ -19,19 +19,21 @@
namespace Q2A\Controllers\User; namespace Q2A\Controllers\User;
use Q2A\Auth\NoPermissionException; use Q2A\Auth\NoPermissionException;
use Q2A\Controllers\BaseController;
use Q2A\Database\DbConnection;
use Q2A\Middleware\Auth\InternalUsersOnly; use Q2A\Middleware\Auth\InternalUsersOnly;
use Q2A\Middleware\Auth\MinimumUserLevel; use Q2A\Middleware\Auth\MinimumUserLevel;
class UsersList extends \Q2A\Controllers\BaseController class UsersList extends BaseController
{ {
public function __construct() public function __construct(DbConnection $db)
{ {
require_once QA_INCLUDE_DIR . 'db/users.php'; require_once QA_INCLUDE_DIR . 'db/users.php';
require_once QA_INCLUDE_DIR . 'db/selects.php'; require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'app/users.php'; require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/format.php'; require_once QA_INCLUDE_DIR . 'app/format.php';
parent::__construct(); parent::__construct($db);
$this->addMiddleware(new InternalUsersOnly(), array('newest', 'special', 'blocked')); $this->addMiddleware(new InternalUsersOnly(), array('newest', 'special', 'blocked'));
$this->addMiddleware(new MinimumUserLevel(QA_USER_LEVEL_MODERATOR), array('blocked')); $this->addMiddleware(new MinimumUserLevel(QA_USER_LEVEL_MODERATOR), array('blocked'));
......
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