Skip to content

Commit

Permalink
Use a configuration parser instead of hardcoded configuration variables
Browse files Browse the repository at this point in the history
  • Loading branch information
joepie91 committed Aug 1, 2012
1 parent 2e5590d commit 52b6216
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 24 deletions.
9 changes: 2 additions & 7 deletions base.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

require("include.constants.php");

require("cphp/config.php");
require("include.config.php");

require("include.dependencies.php");
require("include.exceptions.php");
Expand All @@ -28,11 +28,6 @@
require("class.templater.php");
require("class.localizer.php");

$locale = new Localizer();
$locale->Load($cphp_locale_name);

setlocale(LC_ALL, $locale->locale);

if(empty($not_html))
{
header("Content-Type:text/html; charset=UTF-8");
Expand All @@ -41,7 +36,7 @@
require("class.base.php");
require("class.databaserecord.php");

foreach($cphp_components as $component)
foreach($cphp_config->components as $component)
{
require("components/component.{$component}.php");
}
15 changes: 10 additions & 5 deletions class.databaserecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ abstract class CPHPDatabaseRecordClass extends CPHPBaseClass

public function __construct($uDataSource, $defaultable = null)
{
if(!isset($cphp_config->class_map))
{
die("No class map was specified. Refer to the CPHP manual for instructions.");
}

$this->ConstructDataset($uDataSource, $defaultable);
$this->EventConstructed();
}
Expand Down Expand Up @@ -173,7 +178,7 @@ public function ConstructDataset($uDataSource, $defaultable = null)

public function BindDataset($type, $dataset, $defaultable)
{
global $cphp_class_map;
global $cphp_config;

if(is_array($dataset))
{
Expand All @@ -191,7 +196,7 @@ public function BindDataset($type, $dataset, $defaultable)

public function SetField($type, $variable_name, $column_name)
{
global $cphp_class_map;
global $cphp_config;

if(!isset($this->uData[$column_name]))
{
Expand Down Expand Up @@ -236,7 +241,7 @@ public function SetField($type, $variable_name, $column_name)
break;
default:
$found = false;
foreach($cphp_class_map as $class_type => $class_name)
foreach(get_object_vars($cphp_config->class_map) as $class_type => $class_name)
{
if($type == $class_type)
{
Expand Down Expand Up @@ -477,13 +482,13 @@ public function RetrieveChildren($type, $field)
{
// Not done yet!

if(!isset($cphp_class_map[$type]))
if(!isset($cphp_config->class_map->$type))
{
$classname = get_class($this);
throw new NotFoundException("Non-existent 'type' argument passed on to {$classname}.RetrieveChildren function.");
}

$parent_type = get_parent_class($cphp_class_map[$type]);
$parent_type = get_parent_class($cphp_config->class_map->$type);
if($parent_type !== "CPHPDatabaseRecordClass")
{
$parent_type = ($parent_type === false) ? "NONE" : $parent_type;
Expand Down
26 changes: 26 additions & 0 deletions include.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/*
* CPHP is more free software. It is licensed under the WTFPL, which
* allows you to do pretty much anything with it, without having to
* ask permission. Commercial use is allowed, and no attribution is
* required. We do politely request that you share your modifications
* to benefit other developers, but you are under no enforced
* obligation to do so :)
*
* Please read the accompanying LICENSE document for the full WTFPL
* licensing text.
*/

if($_CPHP !== true) { die(); }

if(empty($_CPHP_CONFIG))
{
die("No valid CPHP configuration path was specified. Refer to the CPHP manual for instructions.");
}

$cphp_config = json_decode(file_get_contents($_CPHP_CONFIG));

if(json_last_error() != JSON_ERROR_NONE)
{
die("Failed to parse CPHP configuration. Refer to the CPHP manual for instructions.");
}
9 changes: 9 additions & 0 deletions include.datetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@

if($_CPHP !== true) { die(); }

if(!empty($cphp_config->locale->default_timezone))
{
$user_preferences[CPHP_SETTING_TIMEZONE] = $cphp_config->locale->default_timezone;
}
else
{
die("No default timezone was specified. Refer to the CPHP manual for instructions.");
}

$timezones = array(
'Pacific/Kwajalein' => '(GMT-12:00) International Date Line West',
'Pacific/Midway' => '(GMT-11:00) Midway Island',
Expand Down
26 changes: 26 additions & 0 deletions include.locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/*
* CPHP is more free software. It is licensed under the WTFPL, which
* allows you to do pretty much anything with it, without having to
* ask permission. Commercial use is allowed, and no attribution is
* required. We do politely request that you share your modifications
* to benefit other developers, but you are under no enforced
* obligation to do so :)
*
* Please read the accompanying LICENSE document for the full WTFPL
* licensing text.
*/

if($_CPHP !== true) { die(); }

if(!empty($cphp_config->locale->default_locale))
{
$locale = new Localizer();
$locale->Load($cphp_config->locale->default_locale);

setlocale(LC_ALL, $locale->locale);
}
else
{
die("No default locale was specified. Refer to the CPHP manual for instructions.");
}
18 changes: 9 additions & 9 deletions include.memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

if($_CPHP !== true) { die(); }

if($cphp_memcache_enabled)
if(!empty($cphp_config->memcache->enabled))
{
$cphp_memcache = new Memcache;
$cphp_memcache_established = $cphp_memcache->connect($cphp_memcache_server, $cphp_memcache_port);
$cphp_memcache_established = $cphp_memcache->connect($cphp_config->memcache->hostname, $cphp_config->memcache->port);

if($cphp_memcache_established !== false)
{
Expand All @@ -30,9 +30,9 @@

function mc_get($key)
{
global $cphp_memcache_enabled, $cphp_memcache_connected, $cphp_memcache;
global $cphp_config, $cphp_memcache_connected, $cphp_memcache;

if($cphp_memcache_enabled === false || $cphp_memcache_connected === false)
if(empty($cphp_config->memcache->enabled) || $cphp_memcache_connected === false)
{
return false;
}
Expand All @@ -52,15 +52,15 @@ function mc_get($key)

function mc_set($key, $value, $expiry)
{
global $cphp_memcache_enabled, $cphp_memcache_connected, $cphp_memcache_compressed, $cphp_memcache;
global $cphp_config, $cphp_memcache_connected, $cphp_memcache;

if($cphp_memcache_enabled === false || $cphp_memcache_connected === false)
if(empty($cphp_config->memcache->enabled) || $cphp_memcache_connected === false)
{
return false;
}
else
{
if($cphp_memcache_compressed === true)
if(!empty($cphp_config->memcache->compressed) === true)
{
$flag = MEMCACHE_COMPRESSED;
}
Expand All @@ -76,9 +76,9 @@ function mc_set($key, $value, $expiry)

function mc_delete($key)
{
global $cphp_memcache_enabled, $cphp_memcache_connected, $cphp_memcache;
global $cphp_config, $cphp_memcache_connected, $cphp_memcache;

if($cphp_memcache_enabled === false || $cphp_memcache_connected === false)
if(empty($cphp_config->memcache->enabled) || $cphp_memcache_connected === false)
{
return false;
}
Expand Down
19 changes: 16 additions & 3 deletions include.mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,26 @@

$cphp_mysql_connected = false;

if($cphp_mysql_enabled === true)
if(!empty($cphp_config->database->driver))
{
if(mysql_connect($cphp_mysql_host, $cphp_mysql_user, $cphp_mysql_pass))
if(empty($cphp_config->database->database))
{
if(mysql_select_db($cphp_mysql_db))
die("No database was configured. Refer to the CPHP manual for instructions.");
}

if(mysql_connect($cphp_config->database->hostname, $cphp_config->database->username, $cphp_config->database->password))
{
if(mysql_select_db($cphp_config->database->database))
{
$cphp_mysql_connected = true;
}
else
{
die("Could not connect to the specified database. Refer to the CPHP manual for instructions.");
}
}
else
{
die("Could not connect to the specified database server. Refer to the CPHP manual for instructions.");
}
}

0 comments on commit 52b6216

Please sign in to comment.