Skip to content

Commit

Permalink
Added configurable default value
Browse files Browse the repository at this point in the history
  • Loading branch information
h-moriya committed Jul 10, 2017
1 parent 3bf71a5 commit 585d509
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Core/Configure.php
Expand Up @@ -114,16 +114,17 @@ public static function write($config, $value = null)
* ```
*
* @param string|null $var Variable to obtain. Use '.' to access array elements.
* @param mixed $default The return value when the configure does not exist
* @return mixed Value stored in configure, or null.
* @link https://book.cakephp.org/3.0/en/development/configuration.html#reading-configuration-data
*/
public static function read($var = null)
public static function read($var = null, $default = null)
{
if ($var === null) {
return static::$_values;
}

return Hash::get(static::$_values, $var);
return Hash::get(static::$_values, $var, $default);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase/Core/ConfigureTest.php
Expand Up @@ -117,6 +117,14 @@ public function testRead()

$result = Configure::read('something_I_just_made_up_now');
$this->assertEquals(null, $result, 'Missing key should return null.');

$default = 'default';
$result = Configure::read('something_I_just_made_up_now', $default);
$this->assertEquals($default, $result);

$default = ['default'];
$result = Configure::read('something_I_just_made_up_now', $default);
$this->assertEquals($default, $result);
}

/**
Expand Down

0 comments on commit 585d509

Please sign in to comment.