From 29b22ece3c890cbbd6435e92b5c728a011daaa89 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Tue, 14 Dec 2021 07:47:35 +0100 Subject: [PATCH] Added the js_config helper method that will assign public JS config values --- application/helpers/config_helper.php | 49 ++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) 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; + } +}