phpDocumentor GloryLands
Common
[ class tree: GloryLands ] [ index: GloryLands ] [ all elements ]

Source for file glconfig.php

Documentation is available at glconfig.php

  1. <?php
  2. /**
  3.   * GloryLands Configuration Core
  4.   *
  5.   * <pre>
  6.   * GloryLands, a Web-Based, Massive Multiplayer Online RPG/Strategy Game
  7.   * Copyright (C) 2008-09  John Haralampidis <jïhnys2[at]gmail.cïm>
  8.   *
  9.   * This program is free software: you can redistribute it and/or modify
  10.   * it under the terms of the GNU General Public License as published by
  11.   * the Free Software Foundation, either version 3 of the License, or
  12.   * (at your option) any later version.
  13.   *
  14.   * This program is distributed in the hope that it will be useful,
  15.   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.   * GNU General Public License for more details.
  18.   *
  19.   * You should have received a copy of the GNU General Public License
  20.   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.   *
  22.   * For any help/suggestions or troubleshooting you can see the
  23.   * project community website at <http://www.glorylands.gr>
  24.   * </pre>
  25.   *
  26.   * @license GNU/GPLv3 GNU General Public License version 3
  27.   * @package GloryLands
  28.   * @subpackage Common
  29.   * @author John Haralampidis <jïhnys2[at]gmail.cïm>
  30.   * @copyright Copyright (C) 2007-2009, John Haralampidis
  31.   * @version 1.0
  32.   */
  33.  
  34. /**
  35.   * Dynamic URL
  36.   *
  37.   * This class provides a simple way to store and covert
  38.   * absolute disk paths into relative URLs and vice-versa.
  39.   *
  40.   * If you want to create a dynamic path, that can be quicly translated into URL, path, or
  41.   * anything else, you can use this class. For a sample usage, see the example.
  42.   *
  43.   * @example dynpath.php How to use Dynamic Path
  44.   * @subpackage Common
  45.   * @version 1.0
  46.   */
  47. class GLDynamicPath {
  48.     
  49.     private $dir;
  50.     private $url;
  51.     
  52.     /**
  53.       * Dynamic URL Constructor
  54.       *
  55.       * @param string    $dir_file    The absolute or relative (to the site root) location of the file on disk
  56.       */
  57.     public function __construct($dir_file{
  58.         if (substr($dir_file,0,1== '/'$dir_file ((string)GLConfig::$root).$dir_file;
  59.         $this->dir $dir_file;
  60.         $this->url str_replace(GLConfig::$root''$dir_file);
  61.     }
  62.         
  63.     public function url($sub=''{
  64.         if (substr($sub,0,1!= '/'$sub='/'.$sub;
  65.         $url $this->url.$sub;
  66.         if (substr($url,0,1== '/'$url=substr($url,1);
  67.         return $url;
  68.     }
  69.  
  70.     public function file($sub=''{
  71.         if (substr($sub,0,1!= '/'$sub='/'.$sub;
  72.         return $this->dir.$sub;
  73.     }
  74.  
  75.     public function path($sub=''{
  76.         if (substr($sub,0,1!= '/'$sub='/'.$sub;
  77.         if (substr($sub,-1== '/'$sub=substr($sub,0,-1);
  78.         return $this->dir.$sub;
  79.     }
  80.     
  81.     public function proxy($sub=''{
  82.         
  83.     }
  84.         
  85.     public function __toString({
  86.         return $this->dir;
  87.     }
  88.     
  89. }
  90.  
  91. /**
  92.   * Configuration file access
  93.   *
  94.   * This class provides read/write access to the database access
  95.   *
  96.   * @subpackage Common
  97.   * @version 1.0
  98.   */
  99. class GLConfigFile {
  100.     
  101.     /**
  102.       * The filename that hols the config
  103.       * @var string 
  104.       */
  105.     private $file;
  106.     
  107.     /**
  108.       * The data structure extracted from the config file
  109.       * @var array 
  110.       */
  111.     public $data;
  112.  
  113.     /**
  114.       * Flags if the file is changed
  115.       * @var bool 
  116.       */
  117.     private $changed;
  118.     
  119.     public function __construct($file{
  120.         $this->file $file;
  121.         $this->data = array();
  122.         $this->changed false;
  123.         $this->load();
  124.     }
  125.     
  126.     public function save({
  127.         if (!$this->changedreturn;
  128.         file_put_contents($this->filevar_export($this->data,true));
  129.         $this->changed false;
  130.     }
  131.     
  132.     public function load({
  133.         // If file does not exists and we have a default file,
  134.         // import the default for the config.
  135.         if (!is_file($this->file)) {
  136.             $path pathinfo($this->file);
  137.             $default $path['dirname'].'/'.$path['filename'].'.default.'.$path['extension'];
  138.             if (is_file($default)) {            
  139.                 $this->data = include($default);
  140.             }            
  141.         else {
  142.             $this->data = include($this->file);
  143.         }
  144.     }
  145.     
  146.     public function __destruct({
  147.         $this->save();
  148.     }    
  149.     
  150.     public function __get($var{
  151.         if (!isset($this->data[$var])) {
  152.             return '';
  153.         else {
  154.             return $this->data[$var];
  155.         }
  156.     }
  157.     
  158.     public function __set($var$value{
  159.         $this->data[$var$value;
  160.         $this->changed true;
  161.     }
  162.     
  163.     public function __isset($var{
  164.         return isset($this->data[$var]);
  165.     }
  166.     
  167.     public function __unset($var{
  168.         unset($this->data[$var]);
  169.     }
  170.     
  171. }
  172.  
  173. /**
  174.   * Glorylands Configuration Wrapper
  175.   *
  176.   * This class provides access to the global configuration
  177.   *
  178.   * @package GloryLands
  179.   * @subpackage Common
  180.   * @version 1.0
  181.   */
  182. class GLConfig {
  183.  
  184.     static $root;
  185.     static $resources;
  186.     static $modules;
  187.     static $engine;
  188.     
  189.     static $general;
  190.     
  191.     static public function initialize({
  192.         $root str_replace('\\','/',dirname(dirname(dirname(dirname(__FILE__)))));
  193.         GLConfig::$root new GLDynamicPath($root);
  194.         GLConfig::$resources new GLDynamicPath('/resources');
  195.         GLConfig::$modules new GLDynamicPath('/modules');
  196.         GLConfig::$engine new GLDynamicPath('/engine');
  197.         
  198.         GLConfig::$general new GLConfigFile(self::$engine->file('config/general.php'));
  199.     }
  200.  
  201.     static public function initialize_dbconf({
  202.  
  203.     }
  204. }
  205.  
  206. ?>

Documentation generated on Tue, 13 Oct 2009 23:48:58 +0300 by phpDocumentor 1.4.1