Browse Source

Merge pull request #10890 from h-moriya/config

Added configurable default value to Configure::read()
Mark Story 8 years ago
parent
commit
65c90183f1
2 changed files with 11 additions and 2 deletions
  1. 3 2
      src/Core/Configure.php
  2. 8 0
      tests/TestCase/Core/ConfigureTest.php

+ 3 - 2
src/Core/Configure.php

@@ -114,16 +114,17 @@ class Configure
      * ```
      *
      * @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);
     }
 
     /**

+ 8 - 0
tests/TestCase/Core/ConfigureTest.php

@@ -117,6 +117,14 @@ class ConfigureTest extends TestCase
 
         $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);
     }
 
     /**