Update CRM_Utils_Constant::value to support env variables
[civicrm-core.git] / CRM / Utils / Constant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class CRM_Utils_Constant
20 */
21 class CRM_Utils_Constant {
22
23 /**
24 * Determine the value of a constant, if any.
25 *
26 * If the specified constant is undefined, check for an environment
27 * variable, defaulting the passed in default value.
28 *
29 * @param string $name
30 * @param mixed $default
31 * (optional)
32 * @return mixed
33 */
34 public static function value(string $name, $default = NULL) {
35 if (defined($name)) {
36 return constant($name);
37 }
38 if (($value = getenv($name)) !== FALSE) {
39 define($name, $value);
40 return $value;
41 }
42 return $default;
43 }
44
45 }