summaryrefslogtreecommitdiffstats
path: root/system/classes
diff options
context:
space:
mode:
authorDracony <draconyster@gmail.com>2013-01-10 16:25:50 +0200
committerDracony <draconyster@gmail.com>2013-01-10 16:25:50 +0200
commit4161d1d639dee2df73a30f4872e5280205e5da2d (patch)
tree0e791ba6b4a7853fb66bbf947ab49880e47c07cf /system/classes
parenta5d20f2082ac7de829996d8df23af6bbc27a91d5 (diff)
downloadPHPixie-4161d1d639dee2df73a30f4872e5280205e5da2d.zip
PHPixie-4161d1d639dee2df73a30f4872e5280205e5da2d.tar.gz
PHPixie-4161d1d639dee2df73a30f4872e5280205e5da2d.tar.bz2
New format for config files.
Writing of config files now supported
Diffstat (limited to 'system/classes')
-rw-r--r--system/classes/config.php195
-rw-r--r--system/classes/misc.php46
-rw-r--r--system/classes/view.php191
3 files changed, 266 insertions, 166 deletions
diff --git a/system/classes/config.php b/system/classes/config.php
index b0550b4..7d32f69 100644
--- a/system/classes/config.php
+++ b/system/classes/config.php
@@ -1,51 +1,146 @@
-<?php
-
-/**
- * Handles retrieving of the configuration options.
- * You can add any configuration values to your /application/config.php file
- * as associative array and get those values using the get() method.
- */
-class Config {
-
- /**
- * Array of configuration options
- * @var array
- * @access public
- * @static
- */
- public static $data=array();
-
- /**
- * Retrieves a configuration value. You can use a dot notation
- * to access properties in nested arrays like this:
- * <code>
- * Config::get('database.default.user');
- * </code>
- *
- * @param string $key Configuration key to retrieve.
- * @param string $default Default value to return if the key is not found.
- * @return mixed Configuration value
- * @access public
- * @throws Exception If default value is not specified and the key is not found
- * @static
- */
- public static function get() {
- $p = func_get_args();
- $keys = explode('.', $p[0]);
- $group=Config::$data;
- for ($i = 0; $i < count($keys); $i++) {
- if ($i == count($keys) - 1) {
- if (isset($group[$keys[$i]]))
- return $group[$keys[$i]];
- break;
- }
- $group = Misc::arr($group, $keys[$i], null);
- if (!is_array($group))
- break;
- }
- if (isset($p[1]))
- return $p[1];
- throw new Exception("Configuration not set for {$p[0]}.");
- }
-
+<?php
+
+/**
+ * Handles retrieving of the configuration options.
+ * You can add configuration files to /application/config folder
+ * and later access them via the get() method.
+ * @package Core
+ */
+class Config {
+
+ /**
+ * Array of configuration files and values loaded from them
+ * @var array
+ * @access protected
+ * @static
+ */
+ protected static $groups = array();
+
+ /**
+ * Loads a group configuration file it has not been loaded before
+ * and returns its options.
+ *
+ * @param string $name Name of the configuration group to load
+ * @return array Array of options for this group
+ * @access public
+ * @throws Exception If a configuration file for this group does not exist
+ * @static
+ */
+ public static function get_group($name) {
+
+ if (!isset(Config::$groups[$name])) {
+
+ $file = Misc::find_file('config', $name);
+
+ if (!$file)
+ throw new Exception("Configuration file {$name}.php was not found.");
+
+ Config::load_group($name,$file);
+ }
+
+ return Config::$groups[$name]['options'];
+ }
+
+ /**
+ * Loads group from file
+ *
+ * @param string $name Name to assign the loaded group
+ * @param string $file File to load
+ * @access public
+ * @static
+ */
+ public static function load_group($name, $file) {
+
+ Config::$groups[$name] = array(
+ 'file' => $file,
+ 'options' => include($file)
+ );
+ }
+
+ /**
+ * Retrieves a configuration value. You can use a dot notation
+ * to access properties in group arrays. The first part of the key
+ * specifies the configuration file from which options should be loaded from
+ * <code>
+ * //Loads ['default']['user'] option
+ * //from database.php configuration file
+ * Config::get('database.default.user');
+ * </code>
+ *
+ * @param string $key Configuration key to retrieve.
+ * @param string $default Default value to return if the key is not found.
+ * @return mixed Configuration value
+ * @access public
+ * @throws Exception If default value is not specified and the key is not found
+ * @static
+ */
+ public static function get() {
+ $p = func_get_args();
+
+ $keys = explode('.', $p[0]);
+ $group_name = array_shift($keys);
+ $group = Config::get_group($group_name);
+
+ for ($i = 0; $i < count($keys); $i++) {
+ if ($i == count($keys) - 1) {
+ if (isset($group[$keys[$i]]))
+ return $group[$keys[$i]];
+ break;
+ }
+ $group = Misc::arr($group, $keys[$i], null);
+ if (!is_array($group))
+ break;
+ }
+
+ if (isset($p[1]))
+ return $p[1];
+
+ throw new Exception("Configuration not set for {$p[0]}.");
+ }
+
+ /**
+ * Sets a configuration option.
+ *
+ * @param string $key Configuration key to set
+ * @param string $value Value to set for this option
+ * @access public
+ * @static
+ */
+ public static function set($key,$value){
+ $keys = explode('.', $key);
+ $group_name = array_shift($keys);
+ $group = Config::get_group($group_name);
+ $subgroup = & $group;
+
+ foreach($keys as $i => $key) {
+
+ if ($i == count($keys) - 1) {
+
+ $subgroup[$key] = $value;
+
+ } else {
+
+ if(!isset($subgroup[$key])||!is_array($subgroup[$key]))
+ $subgroup[$key]=array();
+ $subgroup = & $subgroup[$key];
+
+ }
+ }
+
+ Config::$groups[$group_name]['options'] = $group;
+ }
+
+ /**
+ * Writes a configuration group back to the file it was loaded from
+ *
+ * @param string $group Name of the group to write
+ * @access public
+ * @static
+ */
+ public static function write($group){
+ Config::get_group($group);
+ $group=Config::$groups[$group];
+ file_put_contents($group['file'],"<?php\r\nreturn ".var_export($group['options'],true).";");
+ }
+
} \ No newline at end of file
diff --git a/system/classes/misc.php b/system/classes/misc.php
index 8aad4ec..b4dba6d 100644
--- a/system/classes/misc.php
+++ b/system/classes/misc.php
@@ -22,40 +22,42 @@ class Misc{
}
/**
- * Find full path to either a class or view by name.
- * It will search in the /application folder first, then all enabled modules
+ * Finds full path to a specified file
+ * It will search in the /application folder first, then in all enabled modules
* and then the /system folder
*
- * @param string $type Type of the file to find. Either 'class' or 'view'
- * @param string $name Name of the file to find
- * @return boolean Return Full path to the file or False if it is not found
+ * @param string $subfolder Subfolder to search in e.g. 'classes' or 'views'
+ * @param string $name Name of the file without extension
+ * @param string $extension File extension
+ * @param boolean $return_all If 'true' returns all mathced files as array,
+ * otherwise returns the first file found
+ * @return mixed Full path to the file or False if it is not found
* @access public
* @static
*/
- public static function find_file($type, $name) {
+ public static function find_file($subfolder, $name, $extension = 'php', $return_all = false ) {
+
$folders = array(APPDIR);
- foreach(Config::get('modules') as $module)
- $folders[] = MODDIR.$module.'/';
- $folders[]=SYSDIR;
- if($type=='class'){
- $subfolder = 'classes/';
- $dirs = array_reverse(explode('_', strtolower($name)));
- $fname = array_pop($dirs);
- $subfolder.=implode('/',$dirs).'/';
- }
- if ($type == 'view') {
- $subfolder = 'views/';
- $fname=$name;
- }
+ foreach(Config::get('core.modules',array()) as $module)
+ $folders[] = MODDIR.$module.'/';
+ $folders[] = SYSDIR;
- foreach($folders as $folder) {
- $file = $folder.$subfolder.$fname.'.php';
+ $fname=$name.'.'.$extension;
+ $found_files = array();
+ foreach($folders as $folder) {
+ $file = $folder.$subfolder.'/'.$fname;
if (file_exists($file)) {
- return($file);
+ if(!$return_all)
+ return($file);
+ $found_files[]=$file;
}
}
+
+ if(!empty($found_files))
+ return $found_files;
+
return false;
}
} \ No newline at end of file
diff --git a/system/classes/view.php b/system/classes/view.php
index 5b7b6e7..8dbb85e 100644
--- a/system/classes/view.php
+++ b/system/classes/view.php
@@ -1,95 +1,98 @@
-<?php
-
-/**
- * Manages passing variables to templates and rendering them
- */
-class View{
-
- /**
- * Full path to template file
- * @var string
- * @access private
- */
- private $path;
-
- /**
- * The name of the view.
- * @var string
- * @access public
- */
- public $name;
-
- /**
- * Stores all the variables passed to the view
- * @var array
- * @access private
- */
- private $_data = array();
-
- /**
- * Manages storing the data passed to the view as properties
- *
- * @param string $key Property name
- * @param string $val Property value
- * @return void
- * @access public
- */
- public function __set($key, $val) {
- $this->_data[$key]=$val;
- }
-
- /**
- * Manages accessing passed data as properties
- *
- * @param string $key Property name
- * @return mixed Property value
- * @access public
- * @throws Exception If the property is not found
- */
- public function __get($key){
- if (isset($this->_data[$key]))
- return $this->_data[$key];
- throw new Exception("Value {$key} not set for view {$name}");
- }
-
- /**
- * Renders the template, all dynamically set properties
- * will be available inside the view file as variables.
- * Example:
- * <code>
- * $view = View::get('frontpage');
- * $view->title = "Page title";
- * echo $view->render();
- * </code>
- *
- * @return string Rendered template
- * @access public
- */
- public function render() {
- extract($this->_data);
- ob_start();
- include($this->path);
- $out = ob_get_contents();
- ob_end_clean();
- return $out;
- }
-
- /**
- * Constructs the view
- *
- * @param string $name The name of the template to use
- * @return View
- * @access public
- * @throws Exception If specified template is not found
- * @static
- */
- public static function get($name){
- $view = new View();
- $view->name = $name;
- $file = Misc::find_file('view', $name);
- if ($file == false)
- throw new Exception("View {$name} not found.");
- $view->path=$file;
- return $view;
- }
+<?php
+
+/**
+ * Manages passing variables to templates and rendering them
+ * @package Core
+ */
+class View{
+
+ /**
+ * Full path to template file
+ * @var string
+ * @access private
+ */
+ private $path;
+
+ /**
+ * The name of the view.
+ * @var string
+ * @access public
+ */
+ public $name;
+
+ /**
+ * Stores all the variables passed to the view
+ * @var array
+ * @access private
+ */
+ private $_data = array();
+
+ /**
+ * Manages storing the data passed to the view as properties
+ *
+ * @param string $key Property name
+ * @param string $val Property value
+ * @return void
+ * @access public
+ */
+ public function __set($key, $val) {
+ $this->_data[$key]=$val;
+ }
+
+ /**
+ * Manages accessing passed data as properties
+ *
+ * @param string $key Property name
+ * @return mixed Property value
+ * @access public
+ * @throws Exception If the property is not found
+ */
+ public function __get($key){
+ if (isset($this->_data[$key]))
+ return $this->_data[$key];
+ throw new Exception("Value {$key} not set for view {$name}");
+ }
+
+ /**
+ * Renders the template, all dynamically set properties
+ * will be available inside the view file as variables.
+ * Example:
+ * <code>
+ * $view = View::get('frontpage');
+ * $view->title = "Page title";
+ * echo $view->render();
+ * </code>
+ *
+ * @return string Rendered template
+ * @access public
+ */
+ public function render() {
+ extract($this->_data);
+ ob_start();
+ include($this->path);
+ $out = ob_get_contents();
+ ob_end_clean();
+ return $out;
+ }
+
+ /**
+ * Constructs the view
+ *
+ * @param string $name The name of the template to use
+ * @return View
+ * @access public
+ * @throws Exception If specified template is not found
+ * @static
+ */
+ public static function get($name){
+ $view = new View();
+ $view->name = $name;
+ $file = Misc::find_file('views', $name);
+
+ if ($file == false)
+ throw new Exception("View {$name} not found.");
+
+ $view->path=$file;
+ return $view;
+ }
} \ No newline at end of file