From b6307eb88be0d3738ae8c480d9c1c9edb3507700 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 10 Feb 2016 17:30:45 -0500 Subject: [PATCH] Start of migration from php to toml config, see #11 --- app/config/base_config.php | 4 +- app/config/config.php | 37 ------------------- app/config/config.toml.example | 18 +++++++++ app/config/database.php | 25 ------------- app/config/database.toml.example | 13 +++++++ app/config/minify_config.php | 10 ++--- index.php | 15 +++++--- src/Aviat/AnimeClient/AnimeClient.php | 36 +++++++++++++++++- src/Aviat/AnimeClient/Model/API.php | 2 +- .../AnimeClient/Model/AnimeCollection.php | 1 + 10 files changed, 84 insertions(+), 77 deletions(-) delete mode 100644 app/config/config.php create mode 100644 app/config/config.toml.example delete mode 100644 app/config/database.php create mode 100644 app/config/database.toml.example diff --git a/app/config/base_config.php b/app/config/base_config.php index a2e4fb5a..86bc6385 100644 --- a/app/config/base_config.php +++ b/app/config/base_config.php @@ -20,6 +20,8 @@ $APP_DIR = realpath(__DIR__ . '/../'); $ROOT_DIR = realpath("{$APP_DIR}/../"); $base_config = [ + 'asset_dir' => "{$ROOT_DIR}/public", + // Template file path 'view_path' => "{$APP_DIR}/views", @@ -28,7 +30,7 @@ $base_config = [ 'img_cache_path' => "{$ROOT_DIR}/public/images", // Included config files - 'database' => require 'database.php', + //'database' => require 'database.php', 'menus' => require 'menus.php', 'routes' => require 'routes.php', ]; \ No newline at end of file diff --git a/app/config/config.php b/app/config/config.php deleted file mode 100644 index b72d11ce..00000000 --- a/app/config/config.php +++ /dev/null @@ -1,37 +0,0 @@ - 'timw4mail', - - // ---------------------------------------------------------------------------- - // Whose list is it? - // ---------------------------------------------------------------------------- - 'whose_list' => 'Tim', - - // ---------------------------------------------------------------------------- - // General config - // ---------------------------------------------------------------------------- - - // do you wish to show the anime collection? - 'show_anime_collection' => TRUE, - - // do you wish to show the manga collection? - 'show_manga_collection' => FALSE, - - // path to public directory on the server - 'asset_dir' => realpath(__DIR__ . '/../../public'), -]; \ No newline at end of file diff --git a/app/config/config.toml.example b/app/config/config.toml.example new file mode 100644 index 00000000..a5afd3a6 --- /dev/null +++ b/app/config/config.toml.example @@ -0,0 +1,18 @@ +################################################################################ +# Main User Configuration # +################################################################################ + +# Username for anime and manga lists +hummingbird_username = "timw4mail" + +# Whose list is it? +whose_list = "Tim" + +# do you wish to show the anime collection? +show_anime_collection = true + +# do you wish to show the manga collection? +show_manga_collection = false + +# path to public directory on the server +asset_dir = "/../../public" \ No newline at end of file diff --git a/app/config/database.php b/app/config/database.php deleted file mode 100644 index 4d9b2cb1..00000000 --- a/app/config/database.php +++ /dev/null @@ -1,25 +0,0 @@ - [ - 'type' => 'sqlite', - 'host' => '', - 'user' => '', - 'pass' => '', - 'port' => '', - 'name' => 'default', - 'database' => '', - 'file' => __DIR__ . '/../../anime_collection.sqlite', - ] -]; \ No newline at end of file diff --git a/app/config/database.toml.example b/app/config/database.toml.example new file mode 100644 index 00000000..d6d8656d --- /dev/null +++ b/app/config/database.toml.example @@ -0,0 +1,13 @@ +################################################################################ +# Database Configuration # +################################################################################ + +[collection] +type = "sqlite" +host = "" +user = "" +pass = "" +port = "" +name = "default" +database = "" +file = "/../../anime_collection.sqlite" \ No newline at end of file diff --git a/app/config/minify_config.php b/app/config/minify_config.php index 6e3d9cd0..28198664 100644 --- a/app/config/minify_config.php +++ b/app/config/minify_config.php @@ -13,8 +13,6 @@ // -------------------------------------------------------------------------- -/* $config = */require 'config.php'; - return [ /* @@ -25,7 +23,7 @@ return [ | The folder where css files exist, in relation to the document root | */ - 'css_root' => $config['asset_dir'] . '/css/', + 'css_root' => 'css/', /* |-------------------------------------------------------------------------- @@ -55,7 +53,7 @@ return [ | The file where the css groups are configured | */ - 'css_groups_file' => realpath(__DIR__ . '/minify_css_groups.php'), + 'css_groups_file' => __DIR__ . '/minify_css_groups.php', /* |-------------------------------------------------------------------------- @@ -65,7 +63,7 @@ return [ | The folder where javascript files exist, in relation to the document root | */ - 'js_root' => $config['asset_dir'] . '/js/', + 'js_root' => 'js/', /* |-------------------------------------------------------------------------- @@ -75,7 +73,7 @@ return [ | The file where the javascript groups are configured | */ - 'js_groups_file' => realpath(__DIR__ . '/minify_js_groups.php'), + 'js_groups_file' => __DIR__ . '/minify_js_groups.php', ]; // End of minify_config.php \ No newline at end of file diff --git a/index.php b/index.php index 88cc02d5..571f1987 100644 --- a/index.php +++ b/index.php @@ -10,6 +10,7 @@ * @link https://github.com/timw4mail/HummingBirdAnimeClient * @license MIT */ +use Aviat\AnimeClient\AnimeClient; use Whoops\Handler\PrettyPageHandler; use Whoops\Handler\JsonResponseHandler; @@ -53,6 +54,7 @@ spl_autoload_register(function($class) use ($SRC_DIR) { } }); +// Set up autoloader for third-party dependencies require _dir(__DIR__, '/vendor/autoload.php'); // ------------------------------------------------------------------------- @@ -65,8 +67,8 @@ $defaultHandler = new PrettyPageHandler(); $whoops->pushHandler($defaultHandler); // Set up json handler for ajax errors -$jsonHandler = new JsonResponseHandler(); -$whoops->pushHandler($jsonHandler); +//$jsonHandler = new JsonResponseHandler(); +//$whoops->pushHandler($jsonHandler); // Register as the error handler $whoops->register(); @@ -75,17 +77,18 @@ $whoops->register(); // Dependency Injection setup // ----------------------------------------------------------------------------- require _dir($CONF_DIR, 'base_config.php'); // $base_config -require _dir($CONF_DIR, 'config.php'); // $config -$config_array = array_merge($base_config, $config); $di = require _dir($APP_DIR, 'bootstrap.php'); +$config = AnimeClient::load_toml($CONF_DIR); +$config_array = array_merge($base_config, $config); + +$container = $di($config_array); + // Unset 'constants' unset($APP_DIR); unset($SRC_DIR); unset($CONF_DIR); -$container = $di($config_array); - // ----------------------------------------------------------------------------- // Dispatch to the current route // ----------------------------------------------------------------------------- diff --git a/src/Aviat/AnimeClient/AnimeClient.php b/src/Aviat/AnimeClient/AnimeClient.php index 940ed517..13fffc23 100644 --- a/src/Aviat/AnimeClient/AnimeClient.php +++ b/src/Aviat/AnimeClient/AnimeClient.php @@ -13,6 +13,8 @@ namespace Aviat\AnimeClient; +use Yosymfony\Toml\Toml; + define('SRC_DIR', realpath(__DIR__ . '/../../')); /** @@ -90,5 +92,37 @@ class AnimeClient { return ! $this->is_view_page(); } + /** + * Load configuration options from .toml files + * + * @param string $path - Path to load config + * @return array + */ + public static function load_toml($path) + { + $output = []; + $files = glob("{$path}/*.toml"); + + foreach ($files as $file) + { + $key = str_replace('.toml', '', basename($file)); + $toml = file_get_contents($file); + $config = Toml::Parse($toml); + + if ($key === 'config') + { + foreach($config as $name => $value) + { + $output[$name] = $value; + } + + continue; + } + + $output[$key] = $config; + } + + return $output; + } } -// End of anime_client.php \ No newline at end of file +// End of AnimeClient.php \ No newline at end of file diff --git a/src/Aviat/AnimeClient/Model/API.php b/src/Aviat/AnimeClient/Model/API.php index 4e8dc13d..8a789051 100644 --- a/src/Aviat/AnimeClient/Model/API.php +++ b/src/Aviat/AnimeClient/Model/API.php @@ -78,7 +78,7 @@ class API extends BaseModel { 'defaults' => [ 'cookies' => $this->cookieJar, 'headers' => [ - 'User-Agent' => "Tim's Anime Client/2.0", + 'User-Agent' => "Tim's Anime Client/3.0", 'Accept-Encoding' => 'application/json' ], 'timeout' => 25, diff --git a/src/Aviat/AnimeClient/Model/AnimeCollection.php b/src/Aviat/AnimeClient/Model/AnimeCollection.php index ea386764..905ccf87 100644 --- a/src/Aviat/AnimeClient/Model/AnimeCollection.php +++ b/src/Aviat/AnimeClient/Model/AnimeCollection.php @@ -75,6 +75,7 @@ class AnimeCollection extends DB { $this->valid_database = TRUE; } + // Do an import if an import file exists $this->json_import(); }