Skip to content

Commit

Permalink
Changing Configure::read() to not have a default value, and instead r…
Browse files Browse the repository at this point in the history
…eturn all values in configure when no param is supplied. Test cases updated. Fixes #503
  • Loading branch information
markstory committed Jul 6, 2010
1 parent 2fee0b5 commit 02e25f7
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions cake/basics.php
Expand Up @@ -91,7 +91,7 @@ function uses() {
* @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 '<strong>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</strong>';
Expand Down Expand Up @@ -197,7 +197,7 @@ function pluginSplit($name, $dotAppend = false, $plugin = null) {
* @link http://book.cakephp.org/view/1136/pr
*/
function pr($var) {
if (Configure::read() > 0) {
if (Configure::read('debug') > 0) {
echo '<pre>';
print_r($var);
echo '</pre>';
Expand Down
32 changes: 12 additions & 20 deletions cake/libs/configure.php
Expand Up @@ -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.
Expand Down Expand Up @@ -147,21 +140,20 @@ public static function write($config, $value = null) {
* @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];
}
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]])) {
Expand Down Expand Up @@ -315,7 +307,7 @@ public static function store($type, $name, $data = array()) {
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";
Expand Down Expand Up @@ -369,7 +361,7 @@ private static function __loadBootstrap($boot) {
$prefix = $cache['settings']['prefix'];
}

if (Configure::read() >= 1) {
if (Configure::read('debug') >= 1) {
$duration = '+10 seconds';
} else {
$duration = '+999 days';
Expand Down
4 changes: 2 additions & 2 deletions cake/libs/controller/components/auth.php
Expand Up @@ -291,7 +291,7 @@ public function initialize(&$controller, $settings = array()) {
}
}
$this->_set($settings);
if (Configure::read() > 0) {
if (Configure::read('debug') > 0) {
App::import('Debugger');
Debugger::checkSecurityKeys();
}
Expand All @@ -307,7 +307,7 @@ public function initialize(&$controller, $settings = array()) {
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;
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/controller/components/request_handler.php
Expand Up @@ -718,7 +718,7 @@ function respondAs($type, $options = array()) {
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;
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/controller/controller.php
Expand Up @@ -826,7 +826,7 @@ public function render($action = null, $layout = null, $file = null) {

$this->params['models'] = $this->modelNames;

if (Configure::read() > 2) {
if (Configure::read('debug') > 2) {
$this->set('cakeDebug', $this);
}

Expand Down
4 changes: 2 additions & 2 deletions cake/libs/debugger.php
Expand Up @@ -181,7 +181,7 @@ public static function &getInstance($class = null) {
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');
}
Expand All @@ -190,7 +190,7 @@ public static function &getInstance($class = null) {

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');
}
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/model/datasources/dbo/dbo_oracle.php
Expand Up @@ -977,7 +977,7 @@ function renderStatement($type, $data) {
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 '<div style = "font: Verdana bold 12px; color: #FF0000">' . sprintf(__('SQL Error in model %s:'), $model->alias) . ' ';
if (isset($this->error) && $this->error != null) {
echo $this->error;
Expand Down
8 changes: 4 additions & 4 deletions cake/libs/model/datasources/dbo_source.php
Expand Up @@ -134,7 +134,7 @@ public function __construct($config = null, $autoConnect = true) {
$config['prefix'] = '';
}
parent::__construct($config);
$this->fullDebug = Configure::read() > 1;
$this->fullDebug = Configure::read('debug') > 1;
if (!$this->enabled()) {
return false;
}
Expand Down Expand Up @@ -651,10 +651,10 @@ public function logQuery($sql) {
*/
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('<span style="color:Red;text-align:left"><b>' . __('SQL Error:') . "</b> {$this->error}</span>", E_USER_WARNING);
Expand Down Expand Up @@ -889,7 +889,7 @@ function __filterResults(&$results, &$model, $filtered = array()) {
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 '<div style = "font: Verdana bold 12px; color: #FF0000">' . sprintf(__('SQL Error in model %s:'), $model->alias) . ' ';
if (isset($this->error) && $this->error != null) {
echo $this->error;
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/object.php
Expand Up @@ -229,7 +229,7 @@ protected function _savePersistent($name, &$object) {
$data = str_replace('\\', '\\\\', serialize($objectArray));
$data = '<?php $' . $name . ' = \'' . str_replace('\'', '\\\'', $data) . '\' ?>';
$duration = '+999 days';
if (Configure::read() >= 1) {
if (Configure::read('debug') >= 1) {
$duration = '+10 seconds';
}
cache($file, $data, $duration);
Expand Down
2 changes: 1 addition & 1 deletion cake/libs/view/helper.php
Expand Up @@ -237,7 +237,7 @@ public function webroot($file) {
*/
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) {
Expand Down
6 changes: 3 additions & 3 deletions cake/libs/view/view.php
Expand Up @@ -389,7 +389,7 @@ public function element($name, $params = array(), $loadHelpers = false) {
}
$file = $paths[0] . 'elements' . DS . $name . $this->ext;

if (Configure::read() > 0) {
if (Configure::read('debug') > 0) {
return "Not Found: " . $file;
}
}
Expand Down Expand Up @@ -524,7 +524,7 @@ public function renderCache($filename, $timeStart) {
ob_start();
include ($filename);

if (Configure::read() > 0 && $this->layout != 'xml') {
if (Configure::read('debug') > 0 && $this->layout != 'xml') {
echo "<!-- Cached Render Time: " . round(microtime(true) - $timeStart, 4) . "s -->";
}
$out = ob_get_clean();
Expand Down Expand Up @@ -707,7 +707,7 @@ protected function _render($___viewFn, $___dataForView, $loadHelpers = true, $ca
extract($___dataForView, EXTR_SKIP);
ob_start();

if (Configure::read() > 0) {
if (Configure::read('debug') > 0) {
include ($___viewFn);
} else {
@include ($___viewFn);
Expand Down
8 changes: 8 additions & 0 deletions cake/tests/cases/libs/configure.test.php
Expand Up @@ -99,6 +99,14 @@ function testRead() {

$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.');
}

/**
Expand Down

0 comments on commit 02e25f7

Please sign in to comment.