Added Canvas 1.1.0, originally not under SCM so no historical development records...
[canvas.git] / library / Config2.php
blob6f264994431a5e24f5c759eebcd088c3580c3425
1 <?php
3 // @title Configuration Component
4 // @author Matt Todd
5 // @updated 2006-05-24 16:22:35
6 // @desc The module for storing and retreiving configuration data
8 include_once('stdexception.php');
10 class Config2 {
11 public static $environment = '';
12 public static $config = array();
14 // instance methods (for accessing)
15 public function __construct() {
16 // make sure that the config data has been loaded
17 if(empty(self::$config)) self::load();
19 public function __get($name) {
20 return self::$config[$name];
22 public function __set($name, $value) {
23 return false;
26 // static methods (for loading)
27 public static function load() {
28 // load cached/serialized configuration
29 self::$config = self::load_cache();
31 // check for cache expiration (by updating the originals)
32 if(self::cache_expired()) {
33 $begin_time = microtime(true);
35 // cache has expired, so update it
36 self::$config = self::load_config();
38 // cache config
39 self::save_cache();
41 $time = microtime(true) - $begin_time;
43 // log that config files were reloaded and cached
44 Debug::log("Config files were reloaded and cached ({$time}sec)", 'config', 'notice', 'Config');
47 // set the environment static property
48 self::$environment = self::$config['environment'];
50 // map convention-modifying configurations
51 self::map_config();
53 // return the configuration
54 return self::$config;
57 public static function load_cache() {
58 return unserialize(@file_get_contents(Conventions::config_cache_file()));
61 public static function load_config() {
62 // get the YAML generic configurations and database configurations
63 $config = YAML::load(Conventions::config_file('config'));
64 $config['database'] = YAML::load(Conventions::config_file('database'));
66 // disabled because the Routes file is PHP instead of YAML
67 // $config['routes'] = YAML::load(Conventions::config_file('routes'));
69 return $config;
72 public static function save_cache() {
73 self::$config['checksum'] = self::config_checksum();
74 $cache = serialize(self::$config);
75 @file_put_contents(Conventions::config_cache_file(), $cache);
78 public static function cache_expired() {
79 if(self::$config['checksum'] != self::config_checksum()) {
80 // cache expired!
81 return true;
82 } else {
83 // cache is fine
84 return false;
88 public static function config_checksum() {
89 return md5(
90 @file_get_contents(Conventions::config_file('config'))
91 . @file_get_contents(Conventions::config_file('database'))
92 // disabled because the Routes file is PHP instead of YAML
93 // . @file_get_contents(Conventions::config_file('routes'))
97 // map convention-modifying configs
98 public static function map_config() {
99 // map directories (may add new ones, which is OK)
100 foreach(self::$config['directories'] as $convention=>$dir) {
101 Conventions::$directories[$convention] = $dir;
104 // map others