configure.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: configure_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * Short description for file.
00005  *
00006  * Long description for file
00007  *
00008  * PHP versions 4 and 5
00009  *
00010  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00011  * Copyright 2005-2008, Cake Software Foundation, Inc.
00012  *                              1785 E. Sahara Avenue, Suite 490-204
00013  *                              Las Vegas, Nevada 89104
00014  *
00015  * Licensed under The MIT License
00016  * Redistributions of files must retain the above copyright notice.
00017  *
00018  * @filesource
00019  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00020  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00021  * @package         cake
00022  * @subpackage      cake.cake.libs
00023  * @since           CakePHP(tm) v 1.0.0.2363
00024  * @version         $Revision: 675 $
00025  * @modifiedby      $LastChangedBy: gwoo $
00026  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00027  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00028  */
00029 /**
00030  * Short description for file.
00031  *
00032  * Long description for file
00033  *
00034  * @package     cake
00035  * @subpackage  cake.cake.libs
00036  */
00037 class Configure extends Object {
00038 /**
00039  * Hold array with paths to view files
00040  *
00041  * @var array
00042  * @access public
00043  */
00044     var $viewPaths = array();
00045 /**
00046  * Hold array with paths to controller files
00047  *
00048  * @var array
00049  * @access public
00050  */
00051     var $controllerPaths = array();
00052 /**
00053  * Enter description here...
00054  *
00055  * @var array
00056  * @access public
00057  */
00058     var $modelPaths = array();
00059 /**
00060  * Enter description here...
00061  *
00062  * @var array
00063  * @access public
00064  */
00065     var $helperPaths = array();
00066 /**
00067  * Enter description here...
00068  *
00069  * @var array
00070  * @access public
00071  */
00072     var $componentPaths = array();
00073 /**
00074  * Enter description here...
00075  *
00076  * @var integer
00077  * @access public
00078  */
00079     var $debug = null;
00080 /**
00081  * Return a singleton instance of Configure.
00082  *
00083  * @return Configure instance
00084  * @access public
00085  */
00086     function &getInstance() {
00087         static $instance = array();
00088         if (!$instance) {
00089             $instance[0] =& new Configure;
00090             $instance[0]->__loadBootstrap();
00091         }
00092         return $instance[0];
00093     }
00094 /**
00095  * Used to write a dynamic var in the Configure instance.
00096  *
00097  * Usage
00098  * Configure::write('One.key1', 'value of the Configure::One[key1]');
00099  * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
00100  * Configure::write('One', array('key1'=>'value of the Configure::One[key1]', 'key2'=>'value of the Configure::One[key2]');
00101  * Configure::write(array('One.key1' => 'value of the Configure::One[key1]', 'One.key2' => 'value of the Configure::One[key2]'));
00102  *
00103  * @param array $config
00104  * @return void
00105  * @access public
00106  */
00107     function write($config, $value = null) {
00108         $_this =& Configure::getInstance();
00109 
00110         if (!is_array($config) && $value !== null) {
00111             $name = $_this->__configVarNames($config);
00112 
00113             if (count($name) > 1) {
00114                 $_this->{$name[0]}[$name[1]] = $value;
00115             } else {
00116                 $_this->{$name[0]} = $value;
00117             }
00118         } else {
00119 
00120             foreach ($config as $names => $value) {
00121                 $name = $_this->__configVarNames($names);
00122                 if (count($name) > 1) {
00123                     $_this->{$name[0]}[$name[1]] = $value;
00124                 } else {
00125                     $_this->{$name[0]} = $value;
00126                 }
00127             }
00128         }
00129 
00130         if ($config == 'debug' || (is_array($config) && in_array('debug', $config))) {
00131             if ($_this->debug) {
00132                 error_reporting(E_ALL);
00133 
00134                 if (function_exists('ini_set')) {
00135                     ini_set('display_errors', 1);
00136                 }
00137             } else {
00138                 error_reporting(0);
00139             }
00140         }
00141     }
00142 /**
00143  * Used to read Configure::$var
00144  *
00145  * Usage
00146  * Configure::read('Name'); will return all values for Name
00147  * Configure::read('Name.key'); will return only the value of Configure::Name[key]
00148  *
00149  * @param string $var
00150  * @return string value of Configure::$var
00151  * @access public
00152  */
00153     function read($var = 'debug') {
00154         $_this =& Configure::getInstance();
00155         if ($var === 'debug') {
00156             if (!isset($_this->debug)) {
00157                 $_this->debug = DEBUG;
00158             }
00159             return $_this->debug;
00160         }
00161 
00162         $name = $_this->__configVarNames($var);
00163         if (count($name) > 1) {
00164             if (isset($_this->{$name[0]}[$name[1]])) {
00165                 return $_this->{$name[0]}[$name[1]];
00166             }
00167             return null;
00168         } else {
00169             if (isset($_this->{$name[0]})) {
00170                 return $_this->{$name[0]};
00171             }
00172             return null;
00173         }
00174     }
00175 /**
00176  * Used to delete a var from the Configure instance.
00177  *
00178  * Usage:
00179  * Configure::delete('Name'); will delete the entire Configure::Name
00180  * Configure::delete('Name.key'); will delete only the Configure::Name[key]
00181  *
00182  * @param string $var the var to be deleted
00183  * @return void
00184  * @access public
00185  */
00186     function delete($var = null) {
00187         $_this =& Configure::getInstance();
00188 
00189         $name = $_this->__configVarNames($var);
00190         if (count($name) > 1) {
00191             unset($_this->{$name[0]}[$name[1]]);
00192         } else {
00193             unset($_this->{$name[0]});
00194         }
00195     }
00196 /**
00197  * Will load a file from app/config/configure_file.php
00198  * variables in the files should be formated like:
00199  *  $config['name'] = 'value';
00200  * These will be used to create dynamic Configure vars.
00201  *
00202  * Usage Configure::load('configure_file');
00203  *
00204  * @param string $fileName name of file to load, extension must be .php and only the name should be used, not the extenstion
00205  * @return Configure::write
00206  * @access public
00207  */
00208     function load($fileName) {
00209         $_this =& Configure::getInstance();
00210 
00211         if (!file_exists(CONFIGS . $fileName . '.php')) {
00212             trigger_error("Configure::load() - $fileName.php not found", E_USER_WARNING);
00213             return false;
00214         }
00215         include(CONFIGS . $fileName . '.php');
00216         if (!isset($config)) {
00217             trigger_error("Configure::load() - no variable \$config found in $fileName.php", E_USER_WARNING);
00218             return false;
00219         }
00220         return $_this->write($config);
00221     }
00222 
00223 /**
00224  * Used to determine the current version of CakePHP
00225  *
00226  * Usage Configure::version();
00227  *
00228  * @return string Current version of CakePHP
00229  * @access public
00230  */
00231     function version() {
00232         $_this =& Configure::getInstance();
00233         if (!isset($_this->Cake['version'])) {
00234             require(CORE_PATH . 'cake' . DS . 'config' . DS . 'config.php');
00235             $_this->write($config);
00236         }
00237         return $_this->Cake['version'];
00238     }
00239 /**
00240  * Checks $name for dot notation to create dynamic Configure::$var as an array when needed.
00241  *
00242  * @param mixed $name
00243  * @return array
00244  * @access private
00245  */
00246     function __configVarNames($name) {
00247         if (is_string($name)) {
00248             if (strpos($name, ".")) {
00249                 $name = explode(".", $name);
00250             } else {
00251                 $name = array($name);
00252             }
00253         }
00254         return $name;
00255     }
00256 /**
00257  * Sets the var modelPaths
00258  *
00259  * @param array $modelPaths
00260  * @access private
00261  */
00262     function __buildModelPaths($modelPaths) {
00263         $_this =& Configure::getInstance();
00264         $_this->modelPaths[] = MODELS;
00265         if (isset($modelPaths)) {
00266             foreach ($modelPaths as $value) {
00267                 $_this->modelPaths[] = $value;
00268             }
00269         }
00270     }
00271 /**
00272  * Sets the var viewPaths
00273  *
00274  * @param array $viewPaths
00275  * @access private
00276  */
00277     function __buildViewPaths($viewPaths) {
00278         $_this =& Configure::getInstance();
00279         $_this->viewPaths[] = VIEWS;
00280         if (isset($viewPaths)) {
00281             foreach ($viewPaths as $value) {
00282                 $_this->viewPaths[] = $value;
00283             }
00284         }
00285     }
00286 /**
00287  * Sets the var controllerPaths
00288  *
00289  * @param array $controllerPaths
00290  * @access private
00291  */
00292     function __buildControllerPaths($controllerPaths) {
00293         $_this =& Configure::getInstance();
00294         $_this->controllerPaths[] = CONTROLLERS;
00295         if (isset($controllerPaths)) {
00296             foreach ($controllerPaths as $value) {
00297                 $_this->controllerPaths[] = $value;
00298             }
00299         }
00300     }
00301 /**
00302  * Sets the var helperPaths
00303  *
00304  * @param array $helperPaths
00305  * @access private
00306  */
00307     function __buildHelperPaths($helperPaths) {
00308         $_this =& Configure::getInstance();
00309         $_this->helperPaths[] = HELPERS;
00310         if (isset($helperPaths)) {
00311             foreach ($helperPaths as $value) {
00312                 $_this->helperPaths[] = $value;
00313             }
00314         }
00315     }
00316 /**
00317  * Sets the var componentPaths
00318  *
00319  * @param array $componentPaths
00320  * @access private
00321  */
00322     function __buildComponentPaths($componentPaths) {
00323         $_this =& Configure::getInstance();
00324         $_this->componentPaths[] = COMPONENTS;
00325         if (isset($componentPaths)) {
00326             foreach ($componentPaths as $value) {
00327                 $_this->componentPaths[] = $value;
00328             }
00329         }
00330     }
00331 /**
00332  * Loads the app/config/bootstrap.php
00333  * If the alternative paths are set in this file
00334  * they will be added to the paths vars
00335  *
00336  * @access private
00337  */
00338     function __loadBootstrap() {
00339         $_this =& Configure::getInstance();
00340         $_this->write('Session.checkAgent', true);
00341         $modelPaths = null;
00342         $viewPaths = null;
00343         $controllerPaths = null;
00344         $helperPaths = null;
00345         $componentPaths = null;
00346         require APP_PATH . 'config' . DS . 'bootstrap.php';
00347         $_this->__buildModelPaths($modelPaths);
00348         $_this->__buildViewPaths($viewPaths);
00349         $_this->__buildControllerPaths($controllerPaths);
00350         $_this->__buildHelperPaths($helperPaths);
00351         $_this->__buildComponentPaths($componentPaths);
00352     }
00353 }
00354 ?>