From 604213604ead27bfd985cc5975c856a810679000 Mon Sep 17 00:00:00 2001 From: Marty Wallace Date: Mon, 23 Mar 2015 21:15:10 +1100 Subject: [PATCH] Broken up config data (Issue #52). --- README.md | 2 +- config.php | 43 ----------------------- config/general.php | 16 +++++++++ config/routes.php | 12 +++++++ config/twig.php | 12 +++++++ server/app/App.php | 7 ---- server/tempest/Tempest/Config.php | 15 ++++---- server/tempest/Tempest/MySQL/Database.php | 6 ---- server/tempest/Tempest/Tempest.php | 25 ++++--------- server/tempest/Tempest/Twig/Twig.php | 20 ++++++++++- 10 files changed, 75 insertions(+), 83 deletions(-) delete mode 100644 config.php create mode 100644 config/general.php create mode 100644 config/routes.php create mode 100644 config/twig.php diff --git a/README.md b/README.md index 76bed8e..0c4b0d9 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ Tempest makes use of various tools to streamline the development process: ### Configuration. -* Application configuration is stored in `config.php`. More instructions on configuration are provided at the top of the file. +* Application configuration is stored in `/config/*.php`. More instructions on configuration are provided at the top of the file. ## Releases. diff --git a/config.php b/config.php deleted file mode 100644 index 70be1d3..0000000 --- a/config.php +++ /dev/null @@ -1,43 +0,0 @@ - - * tempest()->config(property, fallback) - * - * - * Where 'property' is the key used here and 'fallback' is the value to use if no matching key is - * found after cascading the data. - */ - -return array( - - '*' => array( - // The application timezone. - 'timezone' => 'Australia/Sydney', - - // Variables that are accessible within Twig templates via {{ config. }}. - // Do not put sensitive data in this block! - 'twig' => array( - 'title' => 'New App' - ), - - // Routes that your application will handle and respond to. - // If not routes are matched, Tempest will look in the /html/ directory for templates that match - // the request URI. - // If no templates are found, a 404 will be sent. - 'routes' => array( - '/sample' => array('controller' => 'SampleController', 'vars' => array('property' => 'value')) - ) - ), - - 'localhost' => array( - // Dev mode shows more verbose errors. - 'dev' => true - ) - -); \ No newline at end of file diff --git a/config/general.php b/config/general.php new file mode 100644 index 0000000..513f67f --- /dev/null +++ b/config/general.php @@ -0,0 +1,16 @@ +tempest()->config(key, fallback). + */ +return array( + + '*' => array( + 'timezone' => 'Australia/Sydney' + ), + + 'localhost' => array( + 'dev' => true + ) + +); \ No newline at end of file diff --git a/config/routes.php b/config/routes.php new file mode 100644 index 0000000..49f1ea1 --- /dev/null +++ b/config/routes.php @@ -0,0 +1,12 @@ + array( + '/sample' => array('controller' => 'SampleController', 'vars' => array('property' => 'value')) + ) + +); \ No newline at end of file diff --git a/config/twig.php b/config/twig.php new file mode 100644 index 0000000..a19b86c --- /dev/null +++ b/config/twig.php @@ -0,0 +1,12 @@ +{{ config. }}. + */ +return array( + + '*' => array( + 'title' => 'New App' + ) + +); \ No newline at end of file diff --git a/server/app/App.php b/server/app/App.php index 127c2e9..0823cb0 100644 --- a/server/app/App.php +++ b/server/app/App.php @@ -14,13 +14,6 @@ protected function setup() } - protected function defineRoutes(Router $router) - { - // Use the routes defined in the app configuration. - return tempest()->config('routes', array()); - } - - protected function defineServices() { return array( diff --git a/server/tempest/Tempest/Config.php b/server/tempest/Tempest/Config.php index 2002d97..77a72a2 100644 --- a/server/tempest/Tempest/Config.php +++ b/server/tempest/Tempest/Config.php @@ -2,7 +2,8 @@ /** - * Manages application configuration, defined in /config.php. + * Manages application configuration, defined in /config/*.php. + * * @author Marty Wallace. */ class Config @@ -11,9 +12,14 @@ class Config private $data = array(); - public function __construct() + /** + * Constructor. + * + * @param string $name The name of the config file being managed by this instace. + */ + public function __construct($name) { - $data = require_once(APP_ROOT . 'config.php'); + $data = require_once(APP_ROOT . 'config/' . $name . '.php'); // Also allow www.{SERVER_NAME}, a common ServerAlias in Apache. $compareHost = preg_replace('/^www\./', '', HOST); @@ -33,9 +39,6 @@ public function __construct() // No cascading config - use the entire top level set. $this->data = $data; } - - // General configuration. - if ($this->data('timezone')) date_default_timezone_set($this->data('timezone')); } diff --git a/server/tempest/Tempest/MySQL/Database.php b/server/tempest/Tempest/MySQL/Database.php index a7ab9d7..491a60c 100644 --- a/server/tempest/Tempest/MySQL/Database.php +++ b/server/tempest/Tempest/MySQL/Database.php @@ -8,12 +8,6 @@ class Database extends PDO implements IService { - /** - * @var PDO - */ - private $provider; - - public function __construct() { $connection = tempest()->config('db'); diff --git a/server/tempest/Tempest/Tempest.php b/server/tempest/Tempest/Tempest.php index cad6502..8432c69 100644 --- a/server/tempest/Tempest/Tempest.php +++ b/server/tempest/Tempest/Tempest.php @@ -75,9 +75,10 @@ public static function getInstance() */ public function __construct() { - $this->config = new Config(); + $this->config = new Config('general'); error_reporting($this->config('dev', false) ? -1 : 0); + date_default_timezone_set($this->config('timezone', 'Australia/Sydney')); set_error_handler(array($this, 'error')); static::$instance = $this; @@ -89,6 +90,8 @@ public function __construct() */ public function start() { + $routes = new Config('routes'); + $this->services = array_merge($this->defineServices(), array( 'twig' => new Twig(), 'db' => new Database(), @@ -96,7 +99,7 @@ public function start() )); $this->router = new Router(); - $this->router->register($this->defineRoutes($this->router)); + $this->router->register($routes->data()); $this->setup(); $request = $this->router->getRequest(); @@ -159,7 +162,7 @@ public function start() * * @return mixed */ - public function config($prop, $fallback = null) + public function config($prop = null, $fallback = null) { return $this->config->data($prop, $fallback); } @@ -186,22 +189,6 @@ protected function defineServices() } - /** - * Defines the routes that this application will handle. - * Override in your application class to defined additional routes. - * - * @param Router $router The Router managing the returned routes. - * - * @return array - */ - protected function defineRoutes(Router $router) - { - return array( - // - ); - } - - /** * Abort the application. * diff --git a/server/tempest/Tempest/Twig/Twig.php b/server/tempest/Tempest/Twig/Twig.php index 5eef9db..2a109d5 100644 --- a/server/tempest/Tempest/Twig/Twig.php +++ b/server/tempest/Tempest/Twig/Twig.php @@ -1,6 +1,7 @@ config = new Config('twig'); + $this->loader = new Twig_Loader_Filesystem(array( Path::create(APP_ROOT . 'html', Path::DELIMITER_RETAIN)->rpad() )); @@ -50,7 +54,7 @@ public function render($file, $context = array()) return new TwigResponse($this->environment->render($file, array_merge($context, array( 'T' => tempest()->getServices(), 'root' => tempest()->getRoot(), - 'config' => tempest()->config('twig', array()), + 'config' => $this->config->data(), 'request' => tempest()->getRouter()->getRequest() )))); } @@ -59,6 +63,20 @@ public function render($file, $context = array()) } + /** + * Returns configuration data stored in /config/twig.php. + * + * @param string $prop The config property to get. + * @param mixed $fallback A fallback value to use if the property is not defined. + * + * @return mixed + */ + public function config($prop = null, $fallback = null) + { + return $this->config->data($prop, $fallback); + } + + /** * A reference to the Twig_Environment instance. *