Skip to content

Commit

Permalink
Broken up config data (Issue #52).
Browse files Browse the repository at this point in the history
  • Loading branch information
Marty Wallace committed Mar 23, 2015
1 parent d64821d commit 6042136
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
43 changes: 0 additions & 43 deletions config.php

This file was deleted.

16 changes: 16 additions & 0 deletions config/general.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* General configuration that is accessible via <code>tempest()->config(key, fallback)</code>.
*/
return array(

'*' => array(
'timezone' => 'Australia/Sydney'
),

'localhost' => array(
'dev' => true
)

);
12 changes: 12 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* Application routes.
*/
return array(

'*' => array(
'/sample' => array('controller' => 'SampleController', 'vars' => array('property' => 'value'))
)

);
12 changes: 12 additions & 0 deletions config/twig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/**
* Configuration data is accessible in Twig templates via <code>{{ config.<key> }}</code>.
*/
return array(

'*' => array(
'title' => 'New App'
)

);
7 changes: 0 additions & 7 deletions server/app/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 9 additions & 6 deletions server/tempest/Tempest/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@


/**
* Manages application configuration, defined in <code>/config.php</code>.
* Manages application configuration, defined in <code>/config/*.php</code>.
*
* @author Marty Wallace.
*/
class Config
Expand All @@ -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);
Expand All @@ -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'));
}


Expand Down
6 changes: 0 additions & 6 deletions server/tempest/Tempest/MySQL/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
class Database extends PDO implements IService
{

/**
* @var PDO
*/
private $provider;


public function __construct()
{
$connection = tempest()->config('db');
Expand Down
25 changes: 6 additions & 19 deletions server/tempest/Tempest/Tempest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -89,14 +90,16 @@ public function __construct()
*/
public function start()
{
$routes = new Config('routes');

$this->services = array_merge($this->defineServices(), array(
'twig' => new Twig(),
'db' => new Database(),
'platform' => new Platform()
));

$this->router = new Router();
$this->router->register($this->defineRoutes($this->router));
$this->router->register($routes->data());
$this->setup();

$request = $this->router->getRequest();
Expand Down Expand Up @@ -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);
}
Expand All @@ -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.
*
Expand Down
20 changes: 19 additions & 1 deletion server/tempest/Tempest/Twig/Twig.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Tempest\Twig;

use Tempest\IService;
use Tempest\Config;
use Tempest\Utils\Path;
use \Twig_Loader_Filesystem;
use \Twig_Environment;
Expand All @@ -18,10 +19,13 @@ class Twig implements IService
private $environment;
private $functions;
private $filters;
private $config;


public function __construct()
{
$this->config = new Config('twig');

$this->loader = new Twig_Loader_Filesystem(array(
Path::create(APP_ROOT . 'html', Path::DELIMITER_RETAIN)->rpad()
));
Expand Down Expand Up @@ -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()
))));
}
Expand All @@ -59,6 +63,20 @@ public function render($file, $context = array())
}


/**
* Returns configuration data stored in <code>/config/twig.php</code>.
*
* @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.
*
Expand Down

0 comments on commit 6042136

Please sign in to comment.