Start of migration from php to toml config, see #11

This commit is contained in:
Timothy Warren 2016-02-10 17:30:45 -05:00
parent ba4597973e
commit b6307eb88b
10 changed files with 84 additions and 77 deletions

View File

@ -20,6 +20,8 @@ $APP_DIR = realpath(__DIR__ . '/../');
$ROOT_DIR = realpath("{$APP_DIR}/../"); $ROOT_DIR = realpath("{$APP_DIR}/../");
$base_config = [ $base_config = [
'asset_dir' => "{$ROOT_DIR}/public",
// Template file path // Template file path
'view_path' => "{$APP_DIR}/views", 'view_path' => "{$APP_DIR}/views",
@ -28,7 +30,7 @@ $base_config = [
'img_cache_path' => "{$ROOT_DIR}/public/images", 'img_cache_path' => "{$ROOT_DIR}/public/images",
// Included config files // Included config files
'database' => require 'database.php', //'database' => require 'database.php',
'menus' => require 'menus.php', 'menus' => require 'menus.php',
'routes' => require 'routes.php', 'routes' => require 'routes.php',
]; ];

View File

@ -1,37 +0,0 @@
<?php
/**
* Hummingbird Anime Client
*
* An API client for Hummingbird to manage anime and manga watch lists
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
* @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT
*/
$config = [
// ----------------------------------------------------------------------------
// Username for anime and manga lists
// ----------------------------------------------------------------------------
'hummingbird_username' => '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'),
];

View File

@ -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"

View File

@ -1,25 +0,0 @@
<?php
/**
* Hummingbird Anime Client
*
* An API client for Hummingbird to manage anime and manga watch lists
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
* @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT
*/
return [
'collection' => [
'type' => 'sqlite',
'host' => '',
'user' => '',
'pass' => '',
'port' => '',
'name' => 'default',
'database' => '',
'file' => __DIR__ . '/../../anime_collection.sqlite',
]
];

View File

@ -0,0 +1,13 @@
################################################################################
# Database Configuration #
################################################################################
[collection]
type = "sqlite"
host = ""
user = ""
pass = ""
port = ""
name = "default"
database = ""
file = "/../../anime_collection.sqlite"

View File

@ -13,8 +13,6 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/* $config = */require 'config.php';
return [ return [
/* /*
@ -25,7 +23,7 @@ return [
| The folder where css files exist, in relation to the document root | 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 | 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 | 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 | 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 // End of minify_config.php

View File

@ -10,6 +10,7 @@
* @link https://github.com/timw4mail/HummingBirdAnimeClient * @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT * @license MIT
*/ */
use Aviat\AnimeClient\AnimeClient;
use Whoops\Handler\PrettyPageHandler; use Whoops\Handler\PrettyPageHandler;
use Whoops\Handler\JsonResponseHandler; 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'); require _dir(__DIR__, '/vendor/autoload.php');
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
@ -65,8 +67,8 @@ $defaultHandler = new PrettyPageHandler();
$whoops->pushHandler($defaultHandler); $whoops->pushHandler($defaultHandler);
// Set up json handler for ajax errors // Set up json handler for ajax errors
$jsonHandler = new JsonResponseHandler(); //$jsonHandler = new JsonResponseHandler();
$whoops->pushHandler($jsonHandler); //$whoops->pushHandler($jsonHandler);
// Register as the error handler // Register as the error handler
$whoops->register(); $whoops->register();
@ -75,17 +77,18 @@ $whoops->register();
// Dependency Injection setup // Dependency Injection setup
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
require _dir($CONF_DIR, 'base_config.php'); // $base_config 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'); $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 'constants'
unset($APP_DIR); unset($APP_DIR);
unset($SRC_DIR); unset($SRC_DIR);
unset($CONF_DIR); unset($CONF_DIR);
$container = $di($config_array);
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Dispatch to the current route // Dispatch to the current route
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@ -13,6 +13,8 @@
namespace Aviat\AnimeClient; namespace Aviat\AnimeClient;
use Yosymfony\Toml\Toml;
define('SRC_DIR', realpath(__DIR__ . '/../../')); define('SRC_DIR', realpath(__DIR__ . '/../../'));
/** /**
@ -90,5 +92,37 @@ class AnimeClient {
return ! $this->is_view_page(); 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;
} }
// End of anime_client.php
continue;
}
$output[$key] = $config;
}
return $output;
}
}
// End of AnimeClient.php

View File

@ -78,7 +78,7 @@ class API extends BaseModel {
'defaults' => [ 'defaults' => [
'cookies' => $this->cookieJar, 'cookies' => $this->cookieJar,
'headers' => [ 'headers' => [
'User-Agent' => "Tim's Anime Client/2.0", 'User-Agent' => "Tim's Anime Client/3.0",
'Accept-Encoding' => 'application/json' 'Accept-Encoding' => 'application/json'
], ],
'timeout' => 25, 'timeout' => 25,

View File

@ -75,6 +75,7 @@ class AnimeCollection extends DB {
$this->valid_database = TRUE; $this->valid_database = TRUE;
} }
// Do an import if an import file exists // Do an import if an import file exists
$this->json_import(); $this->json_import();
} }