From c39ee31e874580cc3655df2ac7139ce2e67f5acb Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Mon, 29 Jun 2015 09:46:49 -0400 Subject: [PATCH] Dependency injection! --- app/base/BaseController.php | 15 ++------ app/base/Config.php | 2 +- app/base/Router.php | 25 +++++++------ app/bootstrap.php | 57 +++++++++++++++++++++++++++++ app/controllers/AnimeController.php | 4 +- app/controllers/MangaController.php | 4 +- index.php | 37 +++---------------- 7 files changed, 85 insertions(+), 59 deletions(-) create mode 100644 app/bootstrap.php diff --git a/app/base/BaseController.php b/app/base/BaseController.php index 902ebcad..01c1e489 100644 --- a/app/base/BaseController.php +++ b/app/base/BaseController.php @@ -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() diff --git a/app/base/Config.php b/app/base/Config.php index d29c6425..93de5b35 100644 --- a/app/base/Config.php +++ b/app/base/Config.php @@ -12,7 +12,7 @@ class Config { * * @var array */ - protected $config; + protected $config = []; /** * Constructor diff --git a/app/base/Router.php b/app/base/Router.php index 66640147..93243ba4 100644 --- a/app/base/Router.php +++ b/app/base/Router.php @@ -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); diff --git a/app/bootstrap.php b/app/bootstrap.php new file mode 100644 index 00000000..49ae0fe3 --- /dev/null +++ b/app/bootstrap.php @@ -0,0 +1,57 @@ +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 \ No newline at end of file diff --git a/app/controllers/AnimeController.php b/app/controllers/AnimeController.php index 9298945b..6c40cc7c 100644 --- a/app/controllers/AnimeController.php +++ b/app/controllers/AnimeController.php @@ -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) { diff --git a/app/controllers/MangaController.php b/app/controllers/MangaController.php index 79b785f2..550b28c5 100644 --- a/app/controllers/MangaController.php +++ b/app/controllers/MangaController.php @@ -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', diff --git a/index.php b/index.php index 751f5411..4e9cdc85 100644 --- a/index.php +++ b/index.php @@ -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 \ No newline at end of file