Added the js_config helper method that will assign public JS config values

This commit is contained in:
Alex Tselegidis 2021-12-14 07:47:35 +01:00
parent 9c44b8644f
commit 29b22ece3c

View file

@ -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;
}
}