Dependency injection!

This commit is contained in:
Timothy Warren 2015-06-29 09:46:49 -04:00
parent 4785b82208
commit c39ee31e87
7 changed files with 85 additions and 59 deletions

View File

@ -44,20 +44,13 @@ class BaseController {
/**
* Constructor
*/
public function __construct()
public function __construct(Config $config, Array $web)
{
global $config;
$this->config = $config;
$web_factory = new WebFactory([
'_GET' => $_GET,
'_POST' => $_POST,
'_COOKIE' => $_COOKIE,
'_SERVER' => $_SERVER,
'_FILES' => $_FILES
]);
$this->request = $web_factory->newRequest();
$this->response = $web_factory->newResponse();
list($request, $response) = $web;
$this->request = $request;
$this->response = $response;
}
public function __destruct()

View File

@ -12,7 +12,7 @@ class Config {
*
* @var array
*/
protected $config;
protected $config = [];
/**
* Constructor

View File

@ -5,8 +5,6 @@
namespace AnimeClient;
use Aura\Router\RouterFactory;
/**
* Basic routing/ dispatch
*/
@ -25,16 +23,21 @@ class Router {
protected $config;
/**
* Constructor
* Array containing request and response objects
* @var array $web
*/
public function __construct()
{
global $config;
$this->config = $config;
protected $web;
$router_factory = new RouterFactory();
$router = $router_factory->newInstance();
$this->router = $router_factory->newInstance();
/**
* Constructor
*
* @param
*/
public function __construct(Config $config, \Aura\Router\Router $router, \Aura\Web\Request $request, \Aura\Web\Response $response)
{
$this->config = $config;
$this->router = $router;
$this->web = [$request, $response];
$this->_setup_routes();
}
@ -107,7 +110,7 @@ class Router {
}
}
$controller = new $controller_name();
$controller = new $controller_name($this->config, $this->web);
// Run the appropriate controller method
$defaultHandler->addDataTable('controller_args', $params);

57
app/bootstrap.php Normal file
View File

@ -0,0 +1,57 @@
<?php
namespace AnimeClient;
use \Whoops\Handler\PrettyPageHandler;
use \Whoops\Handler\JsonResponseHandler;
use \Aura\Web\WebFactory;
use \Aura\Router\RouterFactory;
use \GuzzleHttp\Client;
use \GuzzleHttp\Cookie\CookieJar;
// -----------------------------------------------------------------------------
// Setup error handling
// -----------------------------------------------------------------------------
$whoops = new \Whoops\Run();
// Set up default handler for general errors
$defaultHandler = new PrettyPageHandler();
$whoops->pushHandler($defaultHandler);
// Set up json handler for ajax errors
$jsonHandler = new JsonResponseHandler();
$jsonHandler->onlyForAjaxRequests(true);
$whoops->pushHandler($jsonHandler);
$whoops->register();
// -----------------------------------------------------------------------------
// Injected Objects
// -----------------------------------------------------------------------------
// Create Config Object
$config = new Config();
require _dir(BASE_DIR, '/functions.php');
// Create Aura Router Object
$router_factory = new RouterFactory();
$aura_router = $router_factory->newInstance();
// Create Request/Response Objects
$web_factory = new WebFactory([
'_GET' => $_GET,
'_POST' => $_POST,
'_COOKIE' => $_COOKIE,
'_SERVER' => $_SERVER,
'_FILES' => $_FILES
]);
$request = $web_factory->newRequest();
$response = $web_factory->newResponse();
// -----------------------------------------------------------------------------
// Router
// -----------------------------------------------------------------------------
$router = new Router($config, $aura_router, $request, $response);
$router->dispatch();
// End of bootstrap.php

View File

@ -45,9 +45,9 @@ class AnimeController extends BaseController {
/**
* Constructor
*/
public function __construct()
public function __construct(Config $config, Array $web)
{
parent::__construct();
parent::__construct($config, $web);
if ($this->config->show_anime_collection === FALSE)
{

View File

@ -38,9 +38,9 @@ class MangaController extends BaseController {
/**
* Constructor
*/
public function __construct()
public function __construct(Config $config, Array $web)
{
parent::__construct();
parent::__construct($config, $web);
$this->model = new MangaModel();
$this->base_data = [
'url_type' => 'manga',

View File

@ -18,6 +18,8 @@ define('WHOSE', "Tim's");
// ! End config
// -----------------------------------------------------------------------------
\session_start();
// Work around the silly timezone error
$timezone = ini_get('date.timezone');
if ($timezone === '' || $timezone === FALSE)
@ -25,6 +27,7 @@ if ($timezone === '' || $timezone === FALSE)
ini_set('date.timezone', 'GMT');
}
// Define base directories
define('ROOT_DIR', __DIR__);
define('APP_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'app');
define('CONF_DIR', APP_DIR . DIRECTORY_SEPARATOR . 'config');
@ -34,37 +37,7 @@ require BASE_DIR . DIRECTORY_SEPARATOR . 'pre_conf_functions.php';
// Setup autoloaders
_setup_autoloaders();
// Load config and global functions
$config = new Config();
require _dir(BASE_DIR, '/functions.php');
\session_start();
use \Whoops\Handler\PrettyPageHandler;
use \Whoops\Handler\JsonResponseHandler;
// -----------------------------------------------------------------------------
// Setup error handling
// -----------------------------------------------------------------------------
$whoops = new \Whoops\Run();
// Set up default handler for general errors
$defaultHandler = new PrettyPageHandler();
$whoops->pushHandler($defaultHandler);
// Set up json handler for ajax errors
$jsonHandler = new JsonResponseHandler();
$jsonHandler->onlyForAjaxRequests(true);
$whoops->pushHandler($jsonHandler);
$whoops->register();
// -----------------------------------------------------------------------------
// Router
// -----------------------------------------------------------------------------
$router = new Router();
$defaultHandler->addDataTable('route', (array)$router->get_route());
$router->dispatch();
// Do dependency injection, and go!
require _dir(APP_DIR, 'bootstrap.php');
// End of index.php