From 02e25f755772018bff6c9d4e40ae6469a3246678 Mon Sep 17 00:00:00 2001 From: mark_story Date: Mon, 5 Jul 2010 21:50:36 -0400 Subject: [PATCH] Changing Configure::read() to not have a default value, and instead return all values in configure when no param is supplied. Test cases updated. Fixes #503 --- cake/basics.php | 4 +-- cake/libs/configure.php | 32 +++++++------------ cake/libs/controller/components/auth.php | 4 +-- .../controller/components/request_handler.php | 2 +- cake/libs/controller/controller.php | 2 +- cake/libs/debugger.php | 4 +-- .../libs/model/datasources/dbo/dbo_oracle.php | 2 +- cake/libs/model/datasources/dbo_source.php | 8 ++--- cake/libs/object.php | 2 +- cake/libs/view/helper.php | 2 +- cake/libs/view/view.php | 6 ++-- cake/tests/cases/libs/configure.test.php | 8 +++++ 12 files changed, 38 insertions(+), 38 deletions(-) diff --git a/cake/basics.php b/cake/basics.php index 2403b75bc..0e63fda86 100644 --- a/cake/basics.php +++ b/cake/basics.php @@ -91,7 +91,7 @@ * @link http://book.cakephp.org/view/1128/debug */ function debug($var = false, $showHtml = false, $showFrom = true) { - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { if ($showFrom) { $calledFrom = debug_backtrace(); echo '' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . ''; @@ -197,7 +197,7 @@ if (!function_exists('sortByKey')) { * @link http://book.cakephp.org/view/1136/pr */ function pr($var) { - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { echo '
';
 			print_r($var);
 			echo '
'; diff --git a/cake/libs/configure.php b/cake/libs/configure.php index de12d7bc0..d1c19982c 100644 --- a/cake/libs/configure.php +++ b/cake/libs/configure.php @@ -25,23 +25,16 @@ * @subpackage cake.cake.libs * @link http://book.cakephp.org/view/924/The-Configuration-Class */ -class Configure extends Object { - -/** - * Current debug level. - * - * @link http://book.cakephp.org/view/931/CakePHP-Core-Configuration-Variables - * @var integer - * @access public - */ - public $debug = 0; +class Configure { /** * Array of values currently stored in Configure. * * @var array */ - protected static $_values = array(); + protected static $_values = array( + 'debug' => 0 + ); /** * Initializes configure and runs the bootstrap process. @@ -147,11 +140,13 @@ class Configure extends Object { * @param string $var Variable to obtain. Use '.' to access array elements. * @return string value of Configure::$var */ - public static function read($var = 'debug') { - if ($var === 'debug') { - return self::$_values['debug']; + public static function read($var = null) { + if ($var === null) { + return self::$_values; + } + if (isset(self::$_values[$var])) { + return self::$_values[$var]; } - if (strpos($var, '.') !== false) { $names = explode('.', $var, 3); $var = $names[0]; @@ -159,9 +154,6 @@ class Configure extends Object { if (!isset(self::$_values[$var])) { return null; } - if (!isset($names[1])) { - return self::$_values[$var]; - } switch (count($names)) { case 2: if (isset(self::$_values[$var][$names[1]])) { @@ -315,7 +307,7 @@ class Configure extends Object { private static function __writeConfig($content, $name, $write = true) { $file = CACHE . 'persistent' . DS . $name . '.php'; - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { $expires = "+10 seconds"; } else { $expires = "+999 days"; @@ -369,7 +361,7 @@ class Configure extends Object { $prefix = $cache['settings']['prefix']; } - if (Configure::read() >= 1) { + if (Configure::read('debug') >= 1) { $duration = '+10 seconds'; } else { $duration = '+999 days'; diff --git a/cake/libs/controller/components/auth.php b/cake/libs/controller/components/auth.php index 01c9d88cc..d70d4797e 100644 --- a/cake/libs/controller/components/auth.php +++ b/cake/libs/controller/components/auth.php @@ -291,7 +291,7 @@ class AuthComponent extends Object { } } $this->_set($settings); - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { App::import('Debugger'); Debugger::checkSecurityKeys(); } @@ -307,7 +307,7 @@ class AuthComponent extends Object { public function startup(&$controller) { $isErrorOrTests = ( strtolower($controller->name) == 'cakeerror' || - (strtolower($controller->name) == 'tests' && Configure::read() > 0) + (strtolower($controller->name) == 'tests' && Configure::read('debug') > 0) ); if ($isErrorOrTests) { return true; diff --git a/cake/libs/controller/components/request_handler.php b/cake/libs/controller/components/request_handler.php index eedfc80cf..2a9e42ac5 100644 --- a/cake/libs/controller/components/request_handler.php +++ b/cake/libs/controller/components/request_handler.php @@ -718,7 +718,7 @@ class RequestHandlerComponent extends Object { if (!empty($options['attachment'])) { $this->_header("Content-Disposition: attachment; filename=\"{$options['attachment']}\""); } - if (Configure::read() < 2 && !defined('CAKEPHP_SHELL')) { + if (Configure::read('debug') < 2 && !defined('CAKEPHP_SHELL')) { $this->_header($header); } $this->__responseTypeSet = $cType; diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index 87cb2c440..0f8bfe189 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -826,7 +826,7 @@ class Controller extends Object { $this->params['models'] = $this->modelNames; - if (Configure::read() > 2) { + if (Configure::read('debug') > 2) { $this->set('cakeDebug', $this); } diff --git a/cake/libs/debugger.php b/cake/libs/debugger.php index d6b042727..dc9aa2c90 100644 --- a/cake/libs/debugger.php +++ b/cake/libs/debugger.php @@ -181,7 +181,7 @@ class Debugger { if (!empty($class)) { if (!$instance || strtolower($class) != strtolower(get_class($instance[0]))) { $instance[0] = & new $class(); - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { Configure::version(); // Make sure the core config is loaded $instance[0]->helpPath = Configure::read('Cake.Debugger.HelpPath'); } @@ -190,7 +190,7 @@ class Debugger { if (!$instance) { $instance[0] =& new Debugger(); - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { Configure::version(); // Make sure the core config is loaded $instance[0]->helpPath = Configure::read('Cake.Debugger.HelpPath'); } diff --git a/cake/libs/model/datasources/dbo/dbo_oracle.php b/cake/libs/model/datasources/dbo/dbo_oracle.php index 708c74250..063a445be 100644 --- a/cake/libs/model/datasources/dbo/dbo_oracle.php +++ b/cake/libs/model/datasources/dbo/dbo_oracle.php @@ -977,7 +977,7 @@ class DboOracle extends DboSource { function queryAssociation(&$model, &$linkModel, $type, $association, $assocData, &$queryData, $external = false, &$resultSet, $recursive, $stack) { if ($query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet)) { if (!isset($resultSet) || !is_array($resultSet)) { - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { echo '
' . sprintf(__('SQL Error in model %s:'), $model->alias) . ' '; if (isset($this->error) && $this->error != null) { echo $this->error; diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index 0067435e5..f3ef86eed 100755 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -134,7 +134,7 @@ class DboSource extends DataSource { $config['prefix'] = ''; } parent::__construct($config); - $this->fullDebug = Configure::read() > 1; + $this->fullDebug = Configure::read('debug') > 1; if (!$this->enabled()) { return false; } @@ -651,10 +651,10 @@ class DboSource extends DataSource { */ public function showQuery($sql) { $error = $this->error; - if (strlen($sql) > 200 && !$this->fullDebug && Configure::read() > 1) { + if (strlen($sql) > 200 && !$this->fullDebug && Configure::read('debug') > 1) { $sql = substr($sql, 0, 200) . '[...]'; } - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { $out = null; if ($error) { trigger_error('' . __('SQL Error:') . " {$this->error}", E_USER_WARNING); @@ -889,7 +889,7 @@ class DboSource extends DataSource { public function queryAssociation(&$model, &$linkModel, $type, $association, $assocData, &$queryData, $external = false, &$resultSet, $recursive, $stack) { if ($query = $this->generateAssociationQuery($model, $linkModel, $type, $association, $assocData, $queryData, $external, $resultSet)) { if (!isset($resultSet) || !is_array($resultSet)) { - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { echo '
' . sprintf(__('SQL Error in model %s:'), $model->alias) . ' '; if (isset($this->error) && $this->error != null) { echo $this->error; diff --git a/cake/libs/object.php b/cake/libs/object.php index dc7b0a1d6..b4da0bd68 100644 --- a/cake/libs/object.php +++ b/cake/libs/object.php @@ -229,7 +229,7 @@ class Object { $data = str_replace('\\', '\\\\', serialize($objectArray)); $data = ''; $duration = '+999 days'; - if (Configure::read() >= 1) { + if (Configure::read('debug') >= 1) { $duration = '+10 seconds'; } cache($file, $data, $duration); diff --git a/cake/libs/view/helper.php b/cake/libs/view/helper.php index 43005a220..0721c7466 100644 --- a/cake/libs/view/helper.php +++ b/cake/libs/view/helper.php @@ -237,7 +237,7 @@ class Helper extends Object { */ public function assetTimestamp($path) { $timestampEnabled = ( - (Configure::read('Asset.timestamp') === true && Configure::read() > 0) || + (Configure::read('Asset.timestamp') === true && Configure::read('debug') > 0) || Configure::read('Asset.timestamp') === 'force' ); if (strpos($path, '?') === false && $timestampEnabled) { diff --git a/cake/libs/view/view.php b/cake/libs/view/view.php index 98bdc46c8..c040f2d93 100644 --- a/cake/libs/view/view.php +++ b/cake/libs/view/view.php @@ -389,7 +389,7 @@ class View extends Object { } $file = $paths[0] . 'elements' . DS . $name . $this->ext; - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { return "Not Found: " . $file; } } @@ -524,7 +524,7 @@ class View extends Object { ob_start(); include ($filename); - if (Configure::read() > 0 && $this->layout != 'xml') { + if (Configure::read('debug') > 0 && $this->layout != 'xml') { echo ""; } $out = ob_get_clean(); @@ -707,7 +707,7 @@ class View extends Object { extract($___dataForView, EXTR_SKIP); ob_start(); - if (Configure::read() > 0) { + if (Configure::read('debug') > 0) { include ($___viewFn); } else { @include ($___viewFn); diff --git a/cake/tests/cases/libs/configure.test.php b/cake/tests/cases/libs/configure.test.php index aacd0729b..c7bf6ff71 100644 --- a/cake/tests/cases/libs/configure.test.php +++ b/cake/tests/cases/libs/configure.test.php @@ -99,6 +99,14 @@ class ConfigureTest extends CakeTestCase { $result = Configure::read('debug'); $this->assertTrue($result >= 0); + + $result = Configure::read(); + $this->assertTrue(is_array($result)); + $this->assertTrue(isset($result['debug'])); + $this->assertTrue(isset($result['level1'])); + + $result = Configure::read('something_I_just_made_up_now'); + $this->assertEquals(null, $result, 'Missing key should return null.'); } /**