2006-03-19 03:26:43 +00:00
< ? php
/* SVN FILE: $Id$ */
/**
* Short description for file .
*
* Long description for file
*
* PHP versions 4 and 5
*
2007-02-02 10:39:45 +00:00
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
* Copyright 2005 - 2007 , Cake Software Foundation , Inc .
2006-05-26 05:29:17 +00:00
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
2006-03-19 03:26:43 +00:00
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
* @ filesource
2007-02-02 10:39:45 +00:00
* @ copyright Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
2006-05-26 05:29:17 +00:00
* @ package cake
* @ subpackage cake . cake . libs
2007-02-02 10:39:45 +00:00
* @ since CakePHP ( tm ) v 1.0 . 0.2363
2006-05-26 05:29:17 +00:00
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
2006-03-19 03:26:43 +00:00
*/
/**
* Short description for file .
*
* Long description for file
*
2006-05-26 05:29:17 +00:00
* @ package cake
* @ subpackage cake . cake . libs
2006-03-19 03:26:43 +00:00
*/
2006-06-14 18:02:37 +00:00
class Configure extends Object {
2006-03-19 03:26:43 +00:00
/**
* Hold array with paths to view files
*
* @ var array
* @ access public
*/
2006-05-26 05:29:17 +00:00
var $viewPaths = array ();
2006-03-19 03:26:43 +00:00
/**
* Hold array with paths to controller files
*
* @ var array
* @ access public
*/
2006-05-26 05:29:17 +00:00
var $controllerPaths = array ();
2006-03-19 03:26:43 +00:00
/**
2007-05-20 06:30:19 +00:00
* Hold array with paths to model files
2006-03-19 03:26:43 +00:00
*
* @ var array
* @ access public
*/
2006-05-26 05:29:17 +00:00
var $modelPaths = array ();
2006-06-14 18:02:37 +00:00
/**
2007-05-20 06:30:19 +00:00
* Hold array with paths to helper files
2006-06-14 18:02:37 +00:00
*
* @ var array
* @ access public
*/
var $helperPaths = array ();
/**
2007-05-20 06:30:19 +00:00
* Hold array with paths to component files
2006-06-14 18:02:37 +00:00
*
* @ var array
* @ access public
*/
var $componentPaths = array ();
2006-07-22 14:13:07 +00:00
/**
2007-05-20 06:30:19 +00:00
* Hold array with paths to behavior files
2006-07-22 14:13:07 +00:00
*
* @ var array
* @ access public
*/
var $behaviorPaths = array ();
2007-02-01 07:17:54 +00:00
/**
2007-05-20 06:30:19 +00:00
* Current debug level
2007-02-01 07:17:54 +00:00
*
* @ var integer
* @ access public
*/
var $debug = null ;
2006-03-19 03:26:43 +00:00
/**
* Return a singleton instance of Configure .
*
* @ return Configure instance
* @ access public
*/
2007-02-28 15:41:44 +00:00
function & getInstance ( $boot = true ) {
2006-05-26 05:29:17 +00:00
static $instance = array ();
if ( ! $instance ) {
$instance [ 0 ] =& new Configure ;
2007-02-28 15:41:44 +00:00
$instance [ 0 ] -> __loadBootstrap ( $boot );
2006-05-26 05:29:17 +00:00
}
return $instance [ 0 ];
}
2007-08-07 15:38:20 +00:00
/**
* Returns an index of objects of the given type , with the physical path to each object
*
* @ param string $type Type of object , i . e . 'model' , 'controller' , 'helper' , or 'plugin'
* @ param mixed $path Optional
* @ return Configure instance
* @ access public
*/
function listObjects ( $type , $path = null ) {
$_this =& Configure :: getInstance ();
$Inflector =& Inflector :: getInstance ();
$types = array (
'model' => array ( 'suffix' => '.php' , 'base' => 'AppModel' ),
'controller' => array ( 'suffix' => '_controller.php' , 'base' => 'AppController' ),
'helper' => array ( 'suffix' => '.php' , 'base' => 'AppHelper' ),
'plugin' => array ( 'suffix' => '' , 'base' => null ),
'class' => array ( 'suffix' => '.php' , 'base' => null )
);
if ( ! isset ( $types [ $type ])) {
return false ;
}
if ( empty ( $path )) {
$pathVar = $type . 'Paths' ;
$path = $_this -> { $pathVar };
}
$objects = array ();
foreach (( array ) $path as $dir ) {
$items = $_this -> __list ( $dir , $types [ $type ][ 'suffix' ]);
$objects = am ( $items , $objects );
/* if ( file_exists ( $path . $name . '.php' )) {
Configure :: store ( 'Models' , 'class.paths' , array ( $className => array ( 'path' => $path . $name . '.php' )));
require ( $path . $name . '.php' );
return true ;
} */
}
return array_map ( array ( & $Inflector , 'camelize' ), $objects );
}
/**
* Returns an array of filenames of PHP files in given directory .
*
* @ param string $path Path to scan for files
2007-08-07 21:41:38 +00:00
* @ param string $suffix if false , return only directories . if string , match and return files
* @ return array List of directories or files in directory
2007-08-07 15:38:20 +00:00
*/
2007-08-07 21:41:38 +00:00
function __list ( $path , $suffix = false ) {
2007-08-15 14:38:25 +00:00
if ( ! class_exists ( 'folder' )) {
2007-09-24 23:49:54 +00:00
uses ( 'folder' );
2007-08-15 14:38:25 +00:00
}
2007-08-24 17:00:32 +00:00
$items = array ();
2007-08-07 21:41:38 +00:00
$Folder =& new Folder ( $path );
$contents = $Folder -> read ( false , true );
if ( is_array ( $contents )) {
if ( ! $suffix ) {
return $contents [ 0 ];
} else {
2007-08-10 09:20:42 +00:00
foreach ( $contents [ 1 ] as $item ) {
2007-08-07 21:41:38 +00:00
if ( substr ( $item , - strlen ( $suffix )) == $suffix ) {
2007-08-24 17:00:32 +00:00
$items [] = substr ( $item , 0 , strlen ( $item ) - strlen ( $suffix ));
2007-08-07 15:38:20 +00:00
}
}
}
}
2007-08-24 17:00:32 +00:00
return $items ;
2007-08-07 15:38:20 +00:00
}
2006-12-05 09:49:59 +00:00
/**
* Used to write a dynamic var in the Configure instance .
*
* Usage
* Configure :: write ( 'One.key1' , 'value of the Configure::One[key1]' );
* Configure :: write ( array ( 'One.key1' => 'value of the Configure::One[key1]' ));
* Configure :: write ( 'One' , array ( 'key1' => 'value of the Configure::One[key1]' , 'key2' => 'value of the Configure::One[key2]' );
* Configure :: write ( array ( 'One.key1' => 'value of the Configure::One[key1]' , 'One.key2' => 'value of the Configure::One[key2]' ));
*
2007-05-20 06:30:19 +00:00
* @ param array $config Name of var to write
* @ param mixed $value Value to set for var
2006-12-05 09:49:59 +00:00
* @ access public
*/
2007-06-20 07:51:52 +00:00
function write ( $config , $value = null ) {
2006-12-05 09:49:59 +00:00
$_this =& Configure :: getInstance ();
2007-06-20 06:15:35 +00:00
if ( ! is_array ( $config ) && $value !== null ) {
2006-12-05 09:49:59 +00:00
$name = $_this -> __configVarNames ( $config );
2007-06-20 07:51:52 +00:00
if ( count ( $name ) > 1 ) {
2006-12-05 09:49:59 +00:00
$_this -> { $name [ 0 ]}[ $name [ 1 ]] = $value ;
} else {
$_this -> { $name [ 0 ]} = $value ;
}
} else {
2007-06-20 07:51:52 +00:00
foreach ( $config as $names => $value ) {
2006-12-05 09:49:59 +00:00
$name = $_this -> __configVarNames ( $names );
2007-06-20 07:51:52 +00:00
if ( count ( $name ) > 1 ) {
2006-12-05 09:49:59 +00:00
$_this -> { $name [ 0 ]}[ $name [ 1 ]] = $value ;
} else {
$_this -> { $name [ 0 ]} = $value ;
}
}
}
if ( $config == 'debug' || ( is_array ( $config ) && in_array ( 'debug' , $config ))) {
if ( $_this -> debug ) {
error_reporting ( E_ALL );
if ( function_exists ( 'ini_set' )) {
ini_set ( 'display_errors' , 1 );
}
2007-03-04 03:45:44 +00:00
2007-06-20 06:15:35 +00:00
if ( ! class_exists ( 'Debugger' )) {
2007-03-04 03:45:44 +00:00
require LIBS . 'debugger.php' ;
}
2007-04-12 00:34:16 +00:00
if ( ! class_exists ( 'CakeLog' )) {
uses ( 'cake_log' );
}
Configure :: write ( 'log' , LOG_NOTICE );
2006-12-05 09:49:59 +00:00
} else {
error_reporting ( 0 );
2007-04-12 00:34:16 +00:00
Configure :: write ( 'log' , LOG_NOTICE );
2006-12-05 09:49:59 +00:00
}
}
}
/**
* Used to read Configure :: $var
*
* Usage
* Configure :: read ( 'Name' ); will return all values for Name
* Configure :: read ( 'Name.key' ); will return only the value of Configure :: Name [ key ]
*
2007-05-20 06:30:19 +00:00
* @ param string $var Variable to obtain
2006-12-05 09:49:59 +00:00
* @ return string value of Configure :: $var
* @ access public
*/
2007-06-20 07:51:52 +00:00
function read ( $var = 'debug' ) {
2006-12-05 09:49:59 +00:00
$_this =& Configure :: getInstance ();
2007-06-20 06:15:35 +00:00
if ( $var === 'debug' ) {
2007-06-20 07:51:52 +00:00
if ( ! isset ( $_this -> debug )) {
2007-08-16 05:44:06 +00:00
if ( defined ( 'DEBUG' )) {
$_this -> debug = DEBUG ;
} else {
$_this -> debug = 0 ;
}
2006-12-05 09:49:59 +00:00
}
return $_this -> debug ;
}
$name = $_this -> __configVarNames ( $var );
2007-06-20 07:51:52 +00:00
if ( count ( $name ) > 1 ) {
2007-06-20 06:15:35 +00:00
if ( isset ( $_this -> { $name [ 0 ]}[ $name [ 1 ]])) {
2006-12-21 22:05:42 +00:00
return $_this -> { $name [ 0 ]}[ $name [ 1 ]];
}
return null ;
2006-12-05 09:49:59 +00:00
} else {
2007-06-20 06:15:35 +00:00
if ( isset ( $_this -> { $name [ 0 ]})) {
2006-12-21 22:05:42 +00:00
return $_this -> { $name [ 0 ]};
}
return null ;
2006-12-05 09:49:59 +00:00
}
}
/**
* Used to delete a var from the Configure instance .
*
* Usage :
* Configure :: delete ( 'Name' ); will delete the entire Configure :: Name
* Configure :: delete ( 'Name.key' ); will delete only the Configure :: Name [ key ]
*
* @ param string $var the var to be deleted
* @ access public
*/
2007-06-20 07:51:52 +00:00
function delete ( $var = null ) {
2006-12-05 09:49:59 +00:00
$_this =& Configure :: getInstance ();
$name = $_this -> __configVarNames ( $var );
2007-06-20 07:51:52 +00:00
if ( count ( $name ) > 1 ) {
2006-12-05 09:49:59 +00:00
unset ( $_this -> { $name [ 0 ]}[ $name [ 1 ]]);
} else {
unset ( $_this -> { $name [ 0 ]});
}
}
/**
* Will load a file from app / config / configure_file . php
* variables in the files should be formated like :
* $config [ 'name' ] = 'value' ;
* These will be used to create dynamic Configure vars .
*
* Usage Configure :: load ( 'configure_file' );
*
* @ param string $fileName name of file to load , extension must be . php and only the name should be used , not the extenstion
* @ access public
*/
function load ( $fileName ) {
$_this =& Configure :: getInstance ();
2007-01-09 22:32:48 +00:00
if ( file_exists ( CONFIGS . $fileName . '.php' )) {
include ( CONFIGS . $fileName . '.php' );
} elseif ( file_exists ( CACHE . 'persistent' . DS . $fileName . '.php' )) {
include ( CACHE . 'persistent' . DS . $fileName . '.php' );
2007-09-24 23:49:54 +00:00
} elseif ( file_exists ( CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'config' . DS . $fileName . '.php' )) {
include ( CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'config' . DS . $fileName . '.php' );
2007-01-09 22:32:48 +00:00
} else {
2006-12-05 09:49:59 +00:00
return false ;
}
2006-12-22 22:49:47 +00:00
2007-06-20 07:51:52 +00:00
if ( ! isset ( $config )) {
2006-12-22 22:49:47 +00:00
trigger_error ( sprintf ( __ ( " Configure::load() - no variable \$ config found in %s.php " , true ), $fileName ), E_USER_WARNING );
2006-12-05 09:49:59 +00:00
return false ;
}
return $_this -> write ( $config );
}
/**
* Used to determine the current version of CakePHP
*
* Usage Configure :: version ();
*
* @ return string Current version of CakePHP
* @ access public
*/
function version () {
$_this =& Configure :: getInstance ();
2007-06-20 06:15:35 +00:00
if ( ! isset ( $_this -> Cake [ 'version' ])) {
2006-12-05 09:49:59 +00:00
require ( CORE_PATH . 'cake' . DS . 'config' . DS . 'config.php' );
$_this -> write ( $config );
}
return $_this -> Cake [ 'version' ];
}
/**
2007-01-09 22:32:48 +00:00
* Used to write a config file to the server .
*
* Configure :: store ( 'Model' , 'class.paths' , array ( 'Users' => array ( 'path' => 'users' , 'plugin' => true )));
*
* @ param string $type Type of config file to write , ex : Models , Controllers , Helpers , Components
* @ param string $name file name .
* @ param array $data array of values to store .
* @ access public
*/
function store ( $type , $name , $data = array ()) {
$_this =& Configure :: getInstance ();
$write = true ;
$content = '' ;
foreach ( $data as $key => $value ) {
2007-02-08 03:25:37 +00:00
$content .= " \$ config[' $type '][' $key '] " ;
2007-06-20 07:51:52 +00:00
if ( is_array ( $value )) {
2007-02-08 03:25:37 +00:00
$content .= " = array( " ;
2007-06-20 07:51:52 +00:00
foreach ( $value as $key1 => $value2 ) {
2007-01-12 01:33:18 +00:00
$value2 = addslashes ( $value2 );
2007-01-09 22:32:48 +00:00
$content .= " ' $key1 ' => ' $value2 ', " ;
}
2007-02-08 03:25:37 +00:00
$content .= " ); \n " ;
2007-01-09 22:32:48 +00:00
} else {
2007-01-12 01:33:18 +00:00
$value = addslashes ( $value );
2007-02-08 03:25:37 +00:00
$content .= " = ' $value '; \n " ;
2007-01-09 22:32:48 +00:00
}
}
2007-06-20 06:15:35 +00:00
if ( is_null ( $type )) {
2007-01-09 22:32:48 +00:00
$write = false ;
}
$_this -> __writeConfig ( $content , $name , $write );
}
/**
* Creates a cached version of a configuration file .
* Appends values passed from Configure :: store () to the cached file
*
2007-05-20 06:30:19 +00:00
* @ param string $content Content to write on file
* @ param string $name Name to use for cache file
* @ param boolean $write true if content should be written , false otherwise
2007-01-09 22:32:48 +00:00
* @ access private
*/
2007-06-20 07:51:52 +00:00
function __writeConfig ( $content , $name , $write = true ) {
2007-01-09 22:32:48 +00:00
$file = CACHE . 'persistent' . DS . $name . '.php' ;
2007-01-14 01:29:20 +00:00
$_this =& Configure :: getInstance ();
if ( $_this -> read () > 0 ) {
2007-01-14 01:25:13 +00:00
$expires = " +10 seconds " ;
} else {
$expires = " +999 days " ;
2007-01-09 22:32:48 +00:00
}
2007-01-14 01:25:13 +00:00
$cache = cache ( 'persistent' . DS . $name . '.php' , null , $expires );
2007-06-20 07:51:52 +00:00
if ( $cache === null ) {
2007-01-14 01:25:13 +00:00
cache ( 'persistent' . DS . $name . '.php' , " <?php \n \$ config = array(); \n " , $expires );
}
2007-06-20 07:51:52 +00:00
if ( $write === true ) {
if ( ! class_exists ( 'File' )) {
2007-01-09 22:32:48 +00:00
uses ( 'File' );
}
$fileClass = new File ( $file );
2007-06-20 06:15:35 +00:00
if ( $fileClass -> writable ()) {
2007-05-18 21:16:13 +00:00
$fileClass -> append ( $content );
}
}
}
/**
* Checks $name for dot notation to create dynamic Configure :: $var as an array when needed .
*
2007-05-20 06:30:19 +00:00
* @ param mixed $name Name to split
* @ return array Name separated in items through dot notation
2007-05-18 21:16:13 +00:00
* @ access private
*/
function __configVarNames ( $name ) {
if ( is_string ( $name )) {
if ( strpos ( $name , " . " )) {
$name = explode ( " . " , $name );
} else {
$name = array ( $name );
}
}
return $name ;
}
/**
* Sets the var modelPaths
*
2007-05-20 06:30:19 +00:00
* @ param array $modelPaths Path to model files
2007-05-18 21:16:13 +00:00
* @ access private
*/
function __buildModelPaths ( $modelPaths ) {
$_this =& Configure :: getInstance ();
$_this -> modelPaths [] = MODELS ;
if ( isset ( $modelPaths )) {
2007-06-20 06:15:35 +00:00
foreach ( $modelPaths as $value ) {
2007-05-18 21:16:13 +00:00
$_this -> modelPaths [] = $value ;
}
}
}
/**
* Sets the var viewPaths
*
2007-05-20 06:30:19 +00:00
* @ param array $viewPaths Path to view files
2007-05-18 21:16:13 +00:00
* @ access private
*/
function __buildViewPaths ( $viewPaths ) {
$_this =& Configure :: getInstance ();
$_this -> viewPaths [] = VIEWS ;
if ( isset ( $viewPaths )) {
2007-06-20 06:15:35 +00:00
foreach ( $viewPaths as $value ) {
2007-05-18 21:16:13 +00:00
$_this -> viewPaths [] = $value ;
}
}
}
/**
* Sets the var controllerPaths
*
2007-05-20 06:30:19 +00:00
* @ param array $controllerPaths Path to controller files
2007-05-18 21:16:13 +00:00
* @ access private
*/
function __buildControllerPaths ( $controllerPaths ) {
$_this =& Configure :: getInstance ();
$_this -> controllerPaths [] = CONTROLLERS ;
if ( isset ( $controllerPaths )) {
2007-06-20 06:15:35 +00:00
foreach ( $controllerPaths as $value ) {
2007-05-18 21:16:13 +00:00
$_this -> controllerPaths [] = $value ;
}
}
}
/**
* Sets the var helperPaths
*
2007-05-20 06:30:19 +00:00
* @ param array $helperPaths Path to helper files
2007-05-18 21:16:13 +00:00
* @ access private
*/
function __buildHelperPaths ( $helperPaths ) {
$_this =& Configure :: getInstance ();
$_this -> helperPaths [] = HELPERS ;
if ( isset ( $helperPaths )) {
2007-06-20 06:15:35 +00:00
foreach ( $helperPaths as $value ) {
2007-05-18 21:16:13 +00:00
$_this -> helperPaths [] = $value ;
}
}
}
/**
* Sets the var componentPaths
*
2007-05-20 06:30:19 +00:00
* @ param array $componentPaths Path to component files
2007-05-18 21:16:13 +00:00
* @ access private
*/
function __buildComponentPaths ( $componentPaths ) {
$_this =& Configure :: getInstance ();
$_this -> componentPaths [] = COMPONENTS ;
if ( isset ( $componentPaths )) {
2007-06-20 06:15:35 +00:00
foreach ( $componentPaths as $value ) {
2007-05-18 21:16:13 +00:00
$_this -> componentPaths [] = $value ;
}
}
}
/**
* Sets the var behaviorPaths
*
2007-05-20 06:30:19 +00:00
* @ param array $behaviorPaths Path to behavior files
2007-05-18 21:16:13 +00:00
* @ access private
*/
function __buildBehaviorPaths ( $behaviorPaths ) {
$_this =& Configure :: getInstance ();
$_this -> behaviorPaths [] = BEHAVIORS ;
if ( isset ( $behaviorPaths )) {
2007-06-20 06:15:35 +00:00
foreach ( $behaviorPaths as $value ) {
2007-05-18 21:16:13 +00:00
$_this -> behaviorPaths [] = $value ;
}
}
}
2007-08-07 15:38:20 +00:00
/**
* Sets the var pluginPaths
*
* @ param array $pluginPaths Path to plugins
* @ access private
*/
function __buildPluginPaths ( $pluginPaths ) {
$_this =& Configure :: getInstance ();
$_this -> pluginPaths [] = APP . 'plugins' . DS ;
if ( isset ( $pluginPaths )) {
foreach ( $pluginPaths as $value ) {
$_this -> pluginPaths [] = $value ;
}
}
}
2007-05-18 21:16:13 +00:00
/**
* Loads the app / config / bootstrap . php
* If the alternative paths are set in this file
* they will be added to the paths vars
*
2007-05-20 06:30:19 +00:00
* @ param boolean $boot Load application bootstrap ( if true )
2007-05-18 21:16:13 +00:00
* @ access private
*/
function __loadBootstrap ( $boot ) {
$_this =& Configure :: getInstance ();
2007-08-02 00:16:28 +00:00
$baseUrl = false ;
if ( defined ( 'BASE_URL' )) {
$baseUrl = BASE_URL ;
}
2007-08-16 05:44:06 +00:00
$_this -> write ( 'App' , array ( 'base' => false , 'baseUrl' => $baseUrl , 'dir' => APP_DIR , 'webroot' => WEBROOT_DIR ));
if ( defined ( 'CAKE_ADMIN' )) {
$_this -> write ( 'Routing.admin' , CAKE_ADMIN );
}
if ( defined ( 'WEBSERVICES' )) {
$_this -> write ( 'Routing.webservices' , WEBSERVICES );
}
2007-05-18 21:16:13 +00:00
$modelPaths = null ;
$viewPaths = null ;
$controllerPaths = null ;
$helperPaths = null ;
$componentPaths = null ;
$behaviorPaths = null ;
2007-08-07 15:38:20 +00:00
$pluginPaths = null ;
2007-05-18 21:16:13 +00:00
if ( $boot ) {
if ( ! include ( APP_PATH . 'config' . DS . 'bootstrap.php' )) {
2007-05-20 06:30:19 +00:00
trigger_error ( sprintf ( __ ( " Can't find application bootstrap file. Please create %sbootstrap.php, and make sure it is readable by PHP. " , true ), CONFIGS ), E_USER_ERROR );
2007-05-18 21:16:13 +00:00
}
}
$_this -> __buildModelPaths ( $modelPaths );
$_this -> __buildViewPaths ( $viewPaths );
$_this -> __buildControllerPaths ( $controllerPaths );
$_this -> __buildHelperPaths ( $helperPaths );
$_this -> __buildComponentPaths ( $componentPaths );
$_this -> __buildBehaviorPaths ( $behaviorPaths );
2007-08-07 15:38:20 +00:00
$_this -> __buildPluginPaths ( $pluginPaths );
2007-05-18 21:16:13 +00:00
}
}
2006-03-19 03:26:43 +00:00
?>