easyappointments/application/helpers/config_helper.php

172 lines
4.7 KiB
PHP
Raw Normal View History

<?php defined('BASEPATH') or exit('No direct script access allowed');
/* ----------------------------------------------------------------------------
2022-01-18 15:05:42 +03:00
* Easy!Appointments - Online Appointment Scheduler
*
* @package EasyAppointments
* @author A.Tselegidis <alextselegidis@gmail.com>
2021-12-18 19:43:45 +03:00
* @copyright Copyright (c) Alex Tselegidis
* @license https://opensource.org/licenses/GPL-3.0 - GPLv3
* @link https://easyappointments.org
* @since v1.3.0
* ---------------------------------------------------------------------------- */
/**
* Get / set the specified config value.
*
2021-10-23 14:13:43 +03:00
* If an array is passed as the key, we will assume you want to set an array of values.
*
* Example "Get":
*
2021-10-23 14:17:38 +03:00
* $version = config('version', '1.0.0');
*
* Example "Set":
*
* config(['version' => '1.0.0']);
*
* @param array|string $key Configuration key.
2023-03-13 11:06:18 +03:00
* @param mixed|null $default Default value in case the requested config has no value.
*
2021-10-23 14:20:39 +03:00
* @return mixed|NULL Returns the requested value or NULL if you assign a new configuration value.
*
* @throws InvalidArgumentException
*/
function config(array|string $key, mixed $default = null): mixed
2018-01-23 12:08:37 +03:00
{
/** @var EA_Controller $CI */
$CI = &get_instance();
if (empty($key)) {
2021-10-23 14:08:24 +03:00
throw new InvalidArgumentException('The $key argument cannot be empty.');
}
if (is_array($key)) {
foreach ($key as $item => $value) {
$CI->config->set_item($item, $value);
}
return null;
}
$value = $CI->config->item($key);
return $value ?? $default;
}
if (!function_exists('script_vars')) {
/**
* 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 = script_vars('version', '1.0.0');
*
* Example "Set":
*
* script_vars(['version' => '1.0.0']);
*
2023-03-13 11:06:18 +03:00
* @param array|string|null $key Configuration key.
* @param mixed|null $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
*/
2024-12-19 21:13:51 +03:00
function script_vars(array|string|null $key = null, mixed $default = null): mixed
{
$script_vars = config('script_vars', []);
if (empty($key)) {
return $script_vars;
}
if (is_array($key)) {
foreach ($key as $item => $value) {
$script_vars[$item] = $value;
}
config(['script_vars' => $script_vars]);
return null;
}
$value = $script_vars[$key] ?? null;
return $value ?? $default;
}
}
if (!function_exists('html_vars')) {
/**
* Get / set the specified HTML variable.
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* Example "Get":
*
* $version = html_vars('title', 'Default Title');
*
* Example "Set":
*
* html_vars(['title' => 'Test Title']);
*
2023-03-13 11:06:18 +03:00
* @param array|string|null $key Variable key.
* @param mixed|null $default Default value in case the requested variable has no value.
*
* @return mixed|NULL Returns the requested value or NULL if you assign a new configuration value.
*
* @throws InvalidArgumentException
*/
2024-12-19 21:13:51 +03:00
function html_vars(array|string|null $key = null, mixed $default = null): mixed
{
$html_vars = config('html_vars', []);
if (empty($key)) {
return $html_vars;
}
if (is_array($key)) {
foreach ($key as $item => $value) {
$html_vars[$item] = $value;
}
config(['html_vars' => $html_vars]);
return null;
}
$value = $html_vars[$key] ?? null;
return $value ?? $default;
}
}
if (!function_exists('vars')) {
/**
* Get / set the specified HTML & 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 = vars('version', '1.0.0');
*
* Example "Set":
*
* vars(['version' => '1.0.0']);
*
2023-03-13 11:06:18 +03:00
* @param array|string|null $key Configuration key.
* @param mixed|null $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
*/
2024-12-19 21:13:51 +03:00
function vars(array|string|null $key = null, mixed $default = null): mixed
{
return html_vars($key) ?? (script_vars($key) ?? $default);
}
}