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 * Constructor
*/ */
public function __construct() public function __construct(Config $config, Array $web)
{ {
global $config;
$this->config = $config; $this->config = $config;
$web_factory = new WebFactory([ list($request, $response) = $web;
'_GET' => $_GET, $this->request = $request;
'_POST' => $_POST, $this->response = $response;
'_COOKIE' => $_COOKIE,
'_SERVER' => $_SERVER,
'_FILES' => $_FILES
]);
$this->request = $web_factory->newRequest();
$this->response = $web_factory->newResponse();
} }
public function __destruct() public function __destruct()

View File

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

View File

@ -5,8 +5,6 @@
namespace AnimeClient; namespace AnimeClient;
use Aura\Router\RouterFactory;
/** /**
* Basic routing/ dispatch * Basic routing/ dispatch
*/ */
@ -25,16 +23,21 @@ class Router {
protected $config; protected $config;
/** /**
* Constructor * Array containing request and response objects
* @var array $web
*/ */
public function __construct() protected $web;
{
global $config;
$this->config = $config;
$router_factory = new RouterFactory(); /**
$router = $router_factory->newInstance(); * Constructor
$this->router = $router_factory->newInstance(); *
* @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(); $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 // Run the appropriate controller method
$defaultHandler->addDataTable('controller_args', $params); $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 * Constructor
*/ */
public function __construct() public function __construct(Config $config, Array $web)
{ {
parent::__construct(); parent::__construct($config, $web);
if ($this->config->show_anime_collection === FALSE) if ($this->config->show_anime_collection === FALSE)
{ {

View File

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

View File

@ -18,6 +18,8 @@ define('WHOSE', "Tim's");
// ! End config // ! End config
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
\session_start();
// Work around the silly timezone error // Work around the silly timezone error
$timezone = ini_get('date.timezone'); $timezone = ini_get('date.timezone');
if ($timezone === '' || $timezone === FALSE) if ($timezone === '' || $timezone === FALSE)
@ -25,6 +27,7 @@ if ($timezone === '' || $timezone === FALSE)
ini_set('date.timezone', 'GMT'); ini_set('date.timezone', 'GMT');
} }
// Define base directories
define('ROOT_DIR', __DIR__); define('ROOT_DIR', __DIR__);
define('APP_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'app'); define('APP_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'app');
define('CONF_DIR', APP_DIR . DIRECTORY_SEPARATOR . 'config'); 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
_setup_autoloaders(); _setup_autoloaders();
// Load config and global functions // Do dependency injection, and go!
$config = new Config(); require _dir(APP_DIR, 'bootstrap.php');
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();
// End of index.php // End of index.php