Name: Password: Want to register?

API Documentation

[Parent dir]

Information about libs/Config.php

Classes

Code

1<?php
Simple class for loading configuration files, and saving the data back when necessary. All config files are php files, each with a return statement followed by the configuration (array). All of them should be in configs/ folder. They should have the same name as the file that uses them. I.e. a configuration for libs/Article.php should also be in configs/Article.php. Naming is just a convention, though. (2009.02.10 18:11)[update][delete]
2
class Config{ 3 //private static $cache = array(); 4 5 static function get($name, $optimistic = TRUE){ 6 //if (isset(self::$cache[$name])) { 7 // return self::$cache[$name]; 8 //} 9 if ($optimistic) { //faster if we expect most configs to exist 10 $oldErrors = error_reporting(E_ERROR); 11 $data = include("configs/$name.php"); 12 error_reporting($oldErrors); 13 } else { //faster if we expect most files not to exist 14 $data = NULL; 15 if (file_exists("configs/$name.php")) { 16 $data = include "configs/$name.php"; 17 } 18 } 19 //self::$cache[$name] = $data; 20 return $data; 21 } 22
Writes configuration back to the file, or deletes the file if the configuration is NULL. (2009.02.10 18:18)[update][delete]
23 static function set($name, $data){ 24 if (empty($data)) { 25 if (file_exists("configs/$name.php")) { 26 return unlink("configs/$name.php"); 27 } else { 28 return TRUE; 29 } 30 } else { 31 $code = "<?php\nreturn ".var_export($data, TRUE)."\n?>"; 32 if (!file_put_contents("configs/$name.php", $code, LOCK_EX)) { 33 throw new Exception("Could not save config for '$name'"); 34 } 35 } 36 } 37}
[download]
Made with Notepad++ Also on SourceForge.net