From f43b055195696f22bd3984f0b23d93f4d5deaa00 Mon Sep 17 00:00:00 2001 From: David Haywood Smith Date: Thu, 20 Nov 2014 01:22:08 +0000 Subject: [PATCH] Config consolidation - Consolidate application and server configs into one - Use json for config file - Split config loading into separate lib file - Add config exception --- config/application.json | 63 --------- config/application.php | 113 ---------------- config/config.json | 87 ++++++++++++ config/server-sample.php | 40 ------ controllers/comments_controller.php | 2 +- lib/application.php | 57 +------- lib/config.php | 201 ++++++++++++++++++++++++++++ lib/exceptions/config_exception.php | 15 +++ lib/routing.php | 3 +- 9 files changed, 313 insertions(+), 268 deletions(-) delete mode 100644 config/application.json delete mode 100755 config/application.php create mode 100644 config/config.json delete mode 100644 config/server-sample.php create mode 100644 lib/config.php create mode 100644 lib/exceptions/config_exception.php diff --git a/config/application.json b/config/application.json deleted file mode 100644 index c96ee81..0000000 --- a/config/application.json +++ /dev/null @@ -1,63 +0,0 @@ -{ - "name": "Ratter", - "tagline": "Ratter is an app to demonstrate the functionality of Rat", - "default_controller": "items", - "beta": 1, - "private": 1, - "signup_email_notifications": 0, - "items": { - "name": "post", - "name_plural": "posts", - "titles": { - "enabled" : 1, - "name" : "Title", - "name_plural" : "Titles" - }, - "content": { - "enabled" : 1, - "name" : "Content", - "name_plural" : "Contents" - }, - "uploads": { - "enabled" : 1, - "name" : "Image", - "directory" : "uploads", - "max-size" : "5242880", - "mime-types" : [ - "image/jpeg", - "image/png", - "image/gif", - "image/pjpeg" - ] - }, - "comments" : { - "enabled" : 1, - "name" : "Comment", - "name_plural" : "Comments" - }, - "likes" : { - "enabled" : 1, - "name" : "Like", - "name_plural" : "Likes", - "opposite_name" : "Unlike", - "past_tense" : "Liked by" - } - }, - "timezone": "Europe/London", - "invites": { - "enabled": 1 - }, - "friends": { - "enabled" : 0, - "asymmetric" : 0 - }, - "admin_users": [1], - "theme": "default", - "plugins": { - "log" : 1, - "gravatar" : 1, - "points" : 0, - "analytics" : 0 - }, - "send_emails_from": "support@blah.com" -} diff --git a/config/application.php b/config/application.php deleted file mode 100755 index d8da378..0000000 --- a/config/application.php +++ /dev/null @@ -1,113 +0,0 @@ -Rat'; - public $default_controller = 'items'; - - // Beta - users can't signup, can only enter their email addresses - public $beta = TRUE; - - // Private app - requires login to view pages (except public_pages) - // no share buttons - public $private = TRUE; - public $signup_email_notifications = TRUE; - - // Items - // Notes about uploads: max-size is in bytes (default: 5MB), directory - // should contain three subdirectories: originals, thumbnails, stream - public $items = array( - 'name' => 'post', - 'name_plural' => 'posts', - - 'titles' => array( - 'enabled' => TRUE, - 'name' => 'Title', - 'name_plural' => 'Titles' - ), - - 'content' => array( - 'enabled' => TRUE, - 'name' => 'Content', - 'name_plural' => 'Contents' - ), - - // Remember to update the permissions for your - // upload dir e.g. chmod -R 777 uploads - 'uploads' => array( - 'enabled' => TRUE, - 'name' => 'Image', - 'directory' => 'uploads', - 'max-size' => '5242880', - 'mime-types' => array( - 'image/jpeg', - 'image/png', - 'image/gif', - 'image/pjpeg' - ) - ), - - 'comments' => array( - 'enabled' => TRUE, - 'name' => 'Comment', - 'name_plural' => 'Comments' - ), - - 'likes' => array( - 'enabled' => TRUE, - 'name' => 'Like', - 'name_plural' => 'Likes', - 'opposite_name' => 'Unlike', - 'past_tense' => 'Liked by' - ) - - ); - - // Locale - public $timezone = 'Europe/London'; - - // Invites system - public $invites = array('enabled' => TRUE); - - // Friends - still testing, works with asymmetric set to true... just! - // (Shows 'Follow' link & generates homepage feed) - public $friends = array( - 'enabled' => FALSE, - 'asymmetric' => FALSE - ); - - // Admin users - array of user IDs who have access to admin area - public $admin_users = array(1); - - // Theme - public $theme = 'default'; - - // Plugins - public $plugins = array( - 'log' => TRUE, - 'gravatar' => TRUE, - 'points' => FALSE, - 'analytics' => FALSE - ); - - // Send emails from what address? - public $send_emails_from = 'support@blah.com'; - -} diff --git a/config/config.json b/config/config.json new file mode 100644 index 0000000..e2711dd --- /dev/null +++ b/config/config.json @@ -0,0 +1,87 @@ +{ + "name": "Ratter", + "tagline": "Ratter is an app to demonstrate the functionality of Rat", + "default_controller": "items", + "beta": 1, + "private": 1, + "signup_email_notifications": 0, + "items": { + "name": "post", + "name_plural": "posts", + "titles": { + "enabled": 1, + "name": "Title", + "name_plural": "Titles" + }, + "content": { + "enabled": 1, + "name": "Content", + "name_plural": "Contents" + }, + "uploads": { + "enabled": 1, + "name": "Image", + "directory": "uploads", + "max-size": "5242880", + "mime_types": [ + "image/jpeg", + "image/png", + "image/gif", + "image/pjpeg" + ] + }, + "comments": { + "enabled": 1, + "name": "Comment", + "name_plural": "Comments" + }, + "likes": { + "enabled": 1, + "name": "Like", + "name_plural": "Likes", + "opposite_name": "Unlike", + "past_tense": "Liked by" + } + }, + "timezone": "Europe/London", + "invites": { + "enabled": 1 + }, + "friends": { + "enabled": 0, + "asymmetric": 0 + }, + "admin_users": [ + 1 + ], + "theme": "default", + "plugins": { + "log": 1, + "gravatar": 1, + "points": 0, + "analytics": 0 + }, + "send_emails_from": "support@blah.com", + "url": "http://example.com", + "dev_url": "http://127.0.0.1", + "live_base_dir": "/", + "dev_base_dir": "/rat", + "send_emails": 1, + "encryption_salt": "hw9e46", + "database": { + "dev": { + "host": "localhost", + "username": "root", + "passsword": "", + "database": "rat", + "prefix": "" + }, + "live": { + "host": "localhost", + "username": "root", + "passsword": "root", + "database": "rat", + "prefix": "" + } + } +} diff --git a/config/server-sample.php b/config/server-sample.php deleted file mode 100644 index 27e05f2..0000000 --- a/config/server-sample.php +++ /dev/null @@ -1,40 +0,0 @@ - array( - 'host' => 'localhost', - 'username' => 'root', - 'password' => 'root', - 'database' => 'rat', - 'prefix' => '' - ), - 'live' => array( - 'host' => 'localhost', - 'username' => 'root', - 'password' => 'root', - 'database' => 'rat', - 'prefix' => '' - ) - ); - -} diff --git a/controllers/comments_controller.php b/controllers/comments_controller.php index c948d58..fef0ec3 100644 --- a/controllers/comments_controller.php +++ b/controllers/comments_controller.php @@ -17,7 +17,7 @@ function add() { if ($item->user->email_notifications['item_comment']) { $to = array('name' => $item->user->username, 'email' => $item->user->email); - $subject = '[' . $this->config->name . '] Someone left a ' . strtolower($this->config->items['comments']['name']) . ' on your ' . strtolower($this->config->items['titles']['name']) . ' on ' . $this->config->name . '!'; + $subject = '[' . $this->config->name . '] Someone left a ' . strtolower($this->config->items->comments->name) . ' on your ' . strtolower($this->config->items->titles->name) . ' on ' . $this->config->name . '!'; $link = substr($this->config->url, 0, -1) . $this->url_for('items', 'show', $item->id); $body = $this->twig_string->render(file_get_contents("themes/{$this->config->theme}/emails/item_comment.html"), array('link' => $link, 'app' => $this, 'user' => $item->user)); diff --git a/lib/application.php b/lib/application.php index 672d6f0..2e5148e 100755 --- a/lib/application.php +++ b/lib/application.php @@ -1,27 +1,22 @@ config = new Config; + } public static function initialise() { - // Check for server config - if ( ! file_exists('config/server.php')) { - throw new ApplicationException(null, "Server config doesn't exist yet. Copy config/server-sample.php to config/server.php and update the variables."); - } - - require_once 'config/server.php'; - require_once 'config/application.php'; - $config = new AppConfig; - require_once 'lib/routing.php'; try { - $uri = Routing::fetch_uri($config); + $uri = Routing::fetch_uri(new Config); $controller = ucfirst($uri['controller']) . 'Controller'; @include "controllers/{$uri['controller']}_controller.php"; @@ -50,7 +45,6 @@ public static function initialise() { } - $app->loadConfig($config); $app->loadTwig(); $app->loadModels(); $app->loadPlugins(); @@ -87,7 +81,6 @@ public static function initialise() { // RoutingExceptions only thrown from static context // so must set up new Application before rendering 404 $app = new Application; - $app->loadConfig($config); $app->loadView('pages/404'); } catch (ApplicationException $e) { @@ -99,42 +92,6 @@ public static function initialise() { } - private function loadConfig($config) { - - $this->config = $config; - - // Work out the live domain from config - $domain = substr($this->config->url, 7); - - // Determine if site is live or dev, set site_identifier constant and base_dir - if ($_SERVER['HTTP_HOST'] == $domain || $_SERVER['HTTP_HOST'] == 'www.' . $domain) { - define('SITE_IDENTIFIER', 'live'); - $base_dir = $this->config->base_dir; - } else { - define('SITE_IDENTIFIER', 'dev'); - $base_dir = $this->config->dev_base_dir; - } - - // Add trailing slash if necessary - if (substr($base_dir, -1) != '/') { - $base_dir = $base_dir.'/'; - } - - // Set base_dir constant - $this->config->base_dir = $base_dir; - - // Remove this eventually - define('BASE_DIR', $base_dir); - - // Update config->url and append base_dir - if (SITE_IDENTIFIER == 'live') { - $this->config->url .= $base_dir; - } else { - $this->config->url = $this->config->dev_url . $base_dir; - } - - } - private function loadTwig() { $twig_config['cache'] = 'static/template_cache'; @@ -284,7 +241,7 @@ public function url_for_route($route, array $params) { $route = implode($param, explode('*', $route, 2)); } - return substr(BASE_DIR, 0, -1) . $route; + return substr($this->config->base_dir, 0, -1) . $route; } diff --git a/lib/config.php b/lib/config.php new file mode 100644 index 0000000..1ba2e97 --- /dev/null +++ b/lib/config.php @@ -0,0 +1,201 @@ + array( + 'host' => 'localhost', + 'username' => 'root', + 'password' => '', + 'database' => 'rat', + 'prefix' => '' + ), + 'live' => array( + 'host' => 'localhost', + 'username' => 'root', + 'password' => 'root', + 'database' => 'rat', + 'prefix' => '' + ) + ); + + // Basic app variables + public $name = 'Ratter'; + public $tagline = 'deaveRat'; + public $default_controller = 'items'; + + // Beta - users can't signup, can only enter their email addresses + public $beta = TRUE; + + // Private app - requires login to view pages (except public_pages) + // no share buttons + public $private = FALSE; + public $signup_email_notifications = TRUE; + + // Items + // Notes about uploads: max-size is in bytes (default: 5MB), directory + // should contain three subdirectories: originals, thumbnails, stream + public $items = array( + 'name' => 'post', + 'name_plural' => 'posts', + + 'titles' => array( + 'enabled' => TRUE, + 'name' => 'Title', + 'name_plural' => 'Titles' + ), + + 'content' => array( + 'enabled' => TRUE, + 'name' => 'Content', + 'name_plural' => 'Contents' + ), + + // Remember to update the permissions for your + // upload dir e.g. chmod -R 777 uploads + 'uploads' => array( + 'enabled' => TRUE, + 'name' => 'Image', + 'directory' => 'uploads', + 'max-size' => '5242880', + 'mime-types' => array( + 'image/jpeg', + 'image/png', + 'image/gif', + 'image/pjpeg' + ) + ), + + 'comments' => array( + 'enabled' => TRUE, + 'name' => 'Comment', + 'name_plural' => 'Comments' + ), + + 'likes' => array( + 'enabled' => TRUE, + 'name' => 'Like', + 'name_plural' => 'Likes', + 'opposite_name' => 'Unlike', + 'past_tense' => 'Liked by' + ) + + ); + + // Invites system + public $invites = array('enabled' => TRUE); + + // Friends - still testing, works with asymmetric set to true... just! + // (Shows 'Follow' link & generates homepage feed) + public $friends = array( + 'enabled' => FALSE, + 'asymmetric' => FALSE + ); + + // Admin users - array of user IDs who have access to admin area + public $admin_users = array(1); + + // Theme + public $theme = 'default'; + + // Plugins + public $plugins = array( + 'log' => TRUE, + 'gravatar' => TRUE, + 'points' => FALSE, + 'analytics' => FALSE + ); + + // Send emails from what address? + public $send_emails_from = 'support@blah.com'; + + public $site_identifier; + public $base_dir; + + public function __construct() { + + $raw_config = $this->loadConfigFile(); + $this->fillObject($raw_config); + $this->processConfig(); + + } + + private function loadConfigFile() { + + $config_contents = file_get_contents($this->_config_path); + + if ($config_contents === false) { + throw new ConfigException($this->uri, 'Config file could not be read'); + } + + if ($config_contents == '') { + throw new ConfigException($this->uri, 'Config file appears to be empty'); + } + + $decoded_config = json_decode($config_contents); + + if ($decoded_config == null) { + throw new ConfigException($this->uri, 'Config file appears to contain invalid json'); + } + + return $decoded_config; + + } + + private function fillObject($raw_config) { + + foreach ($this as $key => $value) { + $this->$key = $raw_config->$key; + } + + } + + private function processConfig() { + + // Work out the live domain from config + $domain = substr($this->url, 7); + + // Determine if site is live or dev, set site_identifier constant and base_dir + if ($_SERVER['HTTP_HOST'] == $domain || $_SERVER['HTTP_HOST'] == 'www.' . $domain) { + $this->site_identifier = 'live'; + $this->base_dir = $this->live_base_dir; + } else { + $this->site_identifier = 'dev'; + $this->base_dir = $this->dev_base_dir; + } + + // Add trailing slash if necessary + if (substr($this->base_dir, -1) != '/') { + $this->base_dir = $this->base_dir.'/'; + } + + // Update config->url and append base_dir + if ($this->site_identifier == 'live') { + $this->url .= $this->base_dir; + } else { + $this->url = $this->dev_url . $this->base_dir; + } + + } + +} diff --git a/lib/exceptions/config_exception.php b/lib/exceptions/config_exception.php new file mode 100644 index 0000000..607b0c6 --- /dev/null +++ b/lib/exceptions/config_exception.php @@ -0,0 +1,15 @@ +app = $app; + + } + +} diff --git a/lib/routing.php b/lib/routing.php index 3a14a11..a49f45c 100644 --- a/lib/routing.php +++ b/lib/routing.php @@ -222,7 +222,8 @@ public static function url_for($controller, $action = '', $id = '', $params = ar } - return BASE_DIR . $route; + $config = new Config; + return $config->base_dir . $route; }