From 4785b82208cc13a0026982d77066a06c547b16df Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 26 Jun 2015 16:39:10 -0400 Subject: [PATCH] Add php namespaces --- app/base/BaseApiModel.php | 3 ++- app/base/BaseController.php | 32 +++++++++++++++++++++++++---- app/base/BaseDBModel.php | 1 + app/base/BaseModel.php | 1 + app/base/Config.php | 16 ++++++--------- app/base/Router.php | 6 ++++-- app/base/pre_conf_functions.php | 3 +++ app/controllers/AnimeController.php | 2 ++ app/controllers/MangaController.php | 1 + app/models/AnimeCollectionModel.php | 6 ++++-- app/models/AnimeModel.php | 2 ++ app/models/MangaModel.php | 15 +++++--------- app/views/manga/list.php | 9 ++++---- index.php | 4 +++- public/css/base.css | 4 ++++ public/css/base.myth.css | 1 + tests/base/BaseApiModelTest.php | 6 ++++-- tests/base/BaseDBModelTest.php | 2 ++ tests/base/BaseModelTest.php | 2 ++ tests/base/ConfigTest.php | 2 ++ tests/base/FunctionsTest.php | 2 ++ tests/bootstrap.php | 2 ++ 22 files changed, 85 insertions(+), 37 deletions(-) diff --git a/app/base/BaseApiModel.php b/app/base/BaseApiModel.php index b07584a1..1c6679ad 100644 --- a/app/base/BaseApiModel.php +++ b/app/base/BaseApiModel.php @@ -2,6 +2,7 @@ /** * Base API Model */ +namespace AnimeClient; use \GuzzleHttp\Client; use \GuzzleHttp\Cookie\CookieJar; @@ -63,7 +64,7 @@ class BaseApiModel extends BaseModel { { $result = $this->client->post('https://hummingbird.me/api/v1/users/authenticate', [ 'body' => [ - 'username' => $this->config->hummingbird_username, + 'username' => $username, 'password' => $password ] ]); diff --git a/app/base/BaseController.php b/app/base/BaseController.php index e86b990a..902ebcad 100644 --- a/app/base/BaseController.php +++ b/app/base/BaseController.php @@ -2,6 +2,7 @@ /** * Base Controller */ +namespace AnimeClient; use Aura\Web\WebFactory; @@ -28,6 +29,18 @@ class BaseController { */ protected $response; + /** + * The api model for the current controller + * @var object + */ + protected $model; + + /** + * Common data to be sent to views + * @var array + */ + protected $base_data = []; + /** * Constructor */ @@ -77,7 +90,6 @@ class BaseController { if ( ! is_file($template_path)) { throw new Exception("Invalid template : {$path}"); - die(); } ob_start(); @@ -198,10 +210,14 @@ class BaseController { */ public function login_action() { - if ($this->model->authenticate($this->config->hummingbird_username, $this->request->post->get('password'))) + if ( + $this->model->authenticate( + $this->config->hummingbird_username, + $this->request->post->get('password') + ) + ) { $this->response->redirect->afterPost(full_url('', $this->base_data['url_type'])); - $this->output(); return; } @@ -227,7 +243,15 @@ class BaseController { // cookies foreach($this->response->cookies->get() as $name => $cookie) { - @setcookie($name, $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httponly']); + @setcookie( + $name, + $cookie['value'], + $cookie['expire'], + $cookie['path'], + $cookie['domain'], + $cookie['secure'], + $cookie['httponly'] + ); } // send the actual response diff --git a/app/base/BaseDBModel.php b/app/base/BaseDBModel.php index 174eae35..d2a7e52f 100644 --- a/app/base/BaseDBModel.php +++ b/app/base/BaseDBModel.php @@ -2,6 +2,7 @@ /** * Base DB model */ +namespace AnimeClient; /** * Base model for database interaction diff --git a/app/base/BaseModel.php b/app/base/BaseModel.php index fc37c173..e3710e28 100644 --- a/app/base/BaseModel.php +++ b/app/base/BaseModel.php @@ -2,6 +2,7 @@ /** * Base for base models */ +namespace AnimeClient; use abeautifulsite\SimpleImage; diff --git a/app/base/Config.php b/app/base/Config.php index 7d39cc28..d29c6425 100644 --- a/app/base/Config.php +++ b/app/base/Config.php @@ -1,5 +1,7 @@ config = $config; - - foreach($base_config as $key => $val) - { - $this->config[$key] = $val; - } + $this->config = array_merge($config, $base_config); } diff --git a/app/base/Router.php b/app/base/Router.php index f6bbdb10..66640147 100644 --- a/app/base/Router.php +++ b/app/base/Router.php @@ -3,6 +3,8 @@ * Routing logic */ +namespace AnimeClient; + use Aura\Router\RouterFactory; /** @@ -120,8 +122,8 @@ class Router { private function _setup_routes() { $route_map = [ - 'anime' => 'AnimeController', - 'manga' => 'MangaController', + 'anime' => '\\AnimeClient\\AnimeController', + 'manga' => '\\AnimeClient\\MangaController', ]; $route_type = "anime"; diff --git a/app/base/pre_conf_functions.php b/app/base/pre_conf_functions.php index 4faadd9e..b5b25dab 100644 --- a/app/base/pre_conf_functions.php +++ b/app/base/pre_conf_functions.php @@ -25,6 +25,9 @@ function _setup_autoloaders() { require _dir(ROOT_DIR, '/vendor/autoload.php'); spl_autoload_register(function ($class) { + $class_parts = explode('\\', $class); + $class = end($class_parts); + $dirs = ["base", "controllers", "models"]; foreach($dirs as $dir) diff --git a/app/controllers/AnimeController.php b/app/controllers/AnimeController.php index a80aee69..9298945b 100644 --- a/app/controllers/AnimeController.php +++ b/app/controllers/AnimeController.php @@ -3,6 +3,8 @@ * Anime Controller */ +namespace AnimeClient; + /** * Controller for Anime-related pages */ diff --git a/app/controllers/MangaController.php b/app/controllers/MangaController.php index cd7f6c8f..79b785f2 100644 --- a/app/controllers/MangaController.php +++ b/app/controllers/MangaController.php @@ -2,6 +2,7 @@ /** * Manga Controller */ +namespace AnimeClient; /** * Controller for manga list diff --git a/app/models/AnimeCollectionModel.php b/app/models/AnimeCollectionModel.php index 9d7a5cbc..184fed84 100644 --- a/app/models/AnimeCollectionModel.php +++ b/app/models/AnimeCollectionModel.php @@ -3,6 +3,8 @@ * Anime Collection DB Model */ +namespace AnimeClient; + /** * Model for getting anime collection data */ @@ -27,7 +29,7 @@ class AnimeCollectionModel extends BaseDBModel { { parent::__construct(); - $this->db = Query($this->db_config['collection']); + $this->db = \Query($this->db_config['collection']); $this->anime_model = new AnimeModel(); // Is database valid? If not, set a flag so the @@ -82,7 +84,7 @@ class AnimeCollectionModel extends BaseDBModel { ->order_by('title') ->get(); - return $query->fetchAll(PDO::FETCH_ASSOC); + return $query->fetchAll(\PDO::FETCH_ASSOC); } /** diff --git a/app/models/AnimeModel.php b/app/models/AnimeModel.php index 0d841933..b7523892 100644 --- a/app/models/AnimeModel.php +++ b/app/models/AnimeModel.php @@ -3,6 +3,8 @@ * Anime API Model */ +namespace AnimeClient; + /** * Model for handling requests dealing with the anime list */ diff --git a/app/models/MangaModel.php b/app/models/MangaModel.php index 4bbf0078..64a51d67 100644 --- a/app/models/MangaModel.php +++ b/app/models/MangaModel.php @@ -2,6 +2,7 @@ /** * Manga API Model */ +namespace AnimeClient; /** * Model for handling requests dealing with the manga list @@ -9,17 +10,11 @@ class MangaModel extends BaseApiModel { /** - * @var string $base_url - The base url for api requests + * The base url for api requests + * @var string */ protected $base_url = "https://hummingbird.me/"; - /** - * Constructor - */ - public function __construct() - { - parent::__construct(); - } /** * Update the selected manga @@ -112,9 +107,9 @@ class MangaModel extends BaseApiModel { $raw_data = $response->json(); // Attempt to create the cache dir if it doesn't exist - if ( ! is_dir($config->data_cache_path)) + if ( ! is_dir($this->config->data_cache_path)) { - mkdir($config->data_cache_path); + mkdir($this->config->data_cache_path); } // Cache data in case of downtime diff --git a/app/views/manga/list.php b/app/views/manga/list.php index 594a35df..dda12c62 100644 --- a/app/views/manga/list.php +++ b/app/views/manga/list.php @@ -5,10 +5,9 @@ Title - Alternate Title Rating Chapters - + Volumes Type @@ -17,13 +16,13 @@ - + + - 0) ? (int)($item['rating'] * 2) : '-' ?> / 10 / 0) ? $item['manga']['chapter_count'] : "-" ?> - + / 0) ? $item['manga']['volume_count'] : "-" ?> diff --git a/index.php b/index.php index f99c9210..751f5411 100644 --- a/index.php +++ b/index.php @@ -3,6 +3,8 @@ * Here begins everything! */ +namespace AnimeClient; + // ----------------------------------------------------------------------------- // ! Start config // ----------------------------------------------------------------------------- @@ -36,7 +38,7 @@ _setup_autoloaders(); $config = new Config(); require _dir(BASE_DIR, '/functions.php'); -session_start(); +\session_start(); use \Whoops\Handler\PrettyPageHandler; use \Whoops\Handler\JsonResponseHandler; diff --git a/public/css/base.css b/public/css/base.css index cd145e36..24b2e0f6 100644 --- a/public/css/base.css +++ b/public/css/base.css @@ -35,6 +35,10 @@ tbody > tr:nth-child(odd) { align-items: flex-end; } +.flex-justify-space-around { + jusify-content: space-around; +} + .flex { display: -webkit-flex; display: -ms-flexbox; diff --git a/public/css/base.myth.css b/public/css/base.myth.css index 8c45e996..227db035 100644 --- a/public/css/base.myth.css +++ b/public/css/base.myth.css @@ -23,6 +23,7 @@ tbody > tr:nth-child(odd) { .flex-wrap {flex-wrap: wrap} .flex-no-wrap {flex-wrap: nowrap} .flex-align-end {align-items: flex-end} +.flex-justify-space-around {jusify-content: space-around} .flex {display: flex} .small-font { diff --git a/tests/base/BaseApiModelTest.php b/tests/base/BaseApiModelTest.php index 26e38400..3d49f5f3 100644 --- a/tests/base/BaseApiModelTest.php +++ b/tests/base/BaseApiModelTest.php @@ -1,5 +1,7 @@ assertInstanceOf('BaseModel', $baseApiModel); - $this->assertInstanceOf('BaseApiModel', $baseApiModel); + $this->assertInstanceOf('\AnimeClient\BaseModel', $baseApiModel); + $this->assertInstanceOf('\AnimeClient\BaseApiModel', $baseApiModel); $this->assertInstanceOf('\GuzzleHttp\Client', $baseApiModel->client); $this->assertInstanceOf('\GuzzleHttp\Cookie\CookieJar', $baseApiModel->cookieJar); diff --git a/tests/base/BaseDBModelTest.php b/tests/base/BaseDBModelTest.php index 66e8cb90..bb020722 100644 --- a/tests/base/BaseDBModelTest.php +++ b/tests/base/BaseDBModelTest.php @@ -1,5 +1,7 @@