diff --git a/application/helpers/config_helper.php b/application/helpers/config_helper.php index dce61839..23a80155 100644 --- a/application/helpers/config_helper.php +++ b/application/helpers/config_helper.php @@ -13,7 +13,7 @@ /** * Get / set the specified config value. - * + * * If an array is passed as the key, we will assume you want to set an array of values. * * Example "Get": @@ -55,3 +55,50 @@ function config($key, $default = NULL) return $value ?? $default; } + +if ( ! function_exists('js_config')) +{ + /** + * Get / set the specified JS config value. + * + * If an array is passed as the key, we will assume you want to set an array of values. + * + * Example "Get": + * + * $version = js_config('version', '1.0.0'); + * + * Example "Set": + * + * js_config(['version' => '1.0.0']); + * + * @param array|string $key Configuration key. + * @param mixed $default Default value in case the requested config has no value. + * + * @return mixed|NULL Returns the requested value or NULL if you assign a new configuration value. + * + * @throws InvalidArgumentException + */ + function js_config($key = NULL, $default = NULL) + { + $js_config = config('js_config', []); + + if (empty($key)) + { + return $js_config; + } + + if (is_array($key)) + { + foreach ($key as $item => $value) + { + $js_config[$item] = $value; + } + + return NULL; + } + + $value = $js_config[$key]; + + return $value ?? $default; + } +}