diff --git a/functions/functions.php b/functions/functions.php index 505d5ff..472b5c5 100644 --- a/functions/functions.php +++ b/functions/functions.php @@ -4,7 +4,7 @@ interface ArrisFunctionsInterface { - function setOption(array $options, string $key, $env_key = null, $default_value = ''); + function setOption(array $options, $key, $env_key = null, $default_value = ''); function checkAllowedValue( $value, $allowed_values_array , $invalid_value = NULL ); function mb_trim_text($input, $length, $ellipses = true, $strip_html = true, $ellipses_text = '...'):string; @@ -47,7 +47,11 @@ function checkAllowedValue( $value, $allowed_values_array , $invalid_value = NUL * * use function ArrisFunctions\setOption as setOption; * - * $nginx_cache_levels = setOption($options, 'cache_levels', 'NGINX::NGINX_CACHE_LEVELS', '1:2'); + * ($options, $key, $env_key, $default) => $options[ $key ] + * ([], $key, $env_key, $default) => get_env( $env_key ) + * ($arr, null, $env_key, $default) => get_env( $env_key ) + * ([], null, null, $default) => default + * ([], null, null, null) => null * * @param array $options * @param string $key @@ -55,13 +59,23 @@ function checkAllowedValue( $value, $allowed_values_array , $invalid_value = NUL * @param string $default_value * @return string */ - function setOption(array $options, string $key, $env_key = null, $default_value = '') + function setOption(array $options, $key, $env_key = null, $default_value = '') { - // return (array_key_exists($key, $options) ? $options[$key] : null) ?: getenv($env_key) ?: $default_value; + if (empty($options) || is_null($key)) { - return (array_key_exists($key, $options) ? $options[$key] : null) - ?: (!is_null($env_key) ? getenv($env_key) : null ) - ?: $default_value; + if (is_null($env_key)) { + return $default_value; + } + + return getenv($env_key); + + } elseif (array_key_exists($key, $options)) { + return $options[$key]; + } elseif (!is_null($env_key)) { + return getenv($env_key); + } else { + return $default_value; + } } } diff --git a/sources/Toolkit/NginxToolkit.php b/sources/Toolkit/NginxToolkit.php index 98b0197..a993cdd 100644 --- a/sources/Toolkit/NginxToolkit.php +++ b/sources/Toolkit/NginxToolkit.php @@ -90,18 +90,18 @@ public static function init($options = [], $logger = null) self::$is_logging = setOption($options, 'isLogging', 'NGINX.LOG_CACHE_CLEANING', false); - self::$is_using_cache = setOption($options, 'isUseCache', 'NGINX.NGINX_CACHE_USE', false); + self::$is_using_cache = setOption($options, 'isUseCache', 'NGINX.CACHE_USE', false); - self::$nginx_cache_root = setOption($options, 'cache_root', 'NGINX.NGINX_CACHE_PATH'); + self::$nginx_cache_root = setOption($options, 'cache_root', 'NGINX.CACHE_PATH'); self::$nginx_cache_root = rtrim(self::$nginx_cache_root, DIRECTORY_SEPARATOR); if (empty(self::$nginx_cache_root)) { - throw new \Exception(__METHOD__ . ' throws error, NGINX.NGINX_CACHE_PATH is empty'); + throw new \Exception(__METHOD__ . ' throws error, NGINX.CACHE_PATH is empty'); } - self::$nginx_cache_levels = setOption($options, 'cache_levels', 'NGINX.NGINX_CACHE_LEVELS', '1:2'); + self::$nginx_cache_levels = setOption($options, 'cache_levels', 'NGINX.CACHE_LEVELS', '1:2'); self::$nginx_cache_levels = explode(':', self::$nginx_cache_levels); - self::$nginx_cache_key = setOption($options, 'cache_key_format', 'NGINX.NGINX_CACHE_KEY_FORMAT', 'GET|||HOST|PATH'); + self::$nginx_cache_key = setOption($options, 'cache_key_format', 'NGINX.CACHE_KEY_FORMAT', 'GET|||HOST|PATH'); } public static function clear_nginx_cache(string $url) diff --git a/tests/test_setoption.php b/tests/test_setoption.php new file mode 100644 index 0000000..22bdb9a --- /dev/null +++ b/tests/test_setoption.php @@ -0,0 +1,53 @@ + 'key1 is 1', + 'key2' => 'key2 is 4' +]; + +putenv("FOO=BAR"); + +// 'key1 is 1' +echo __LINE__, ' : '; var_dump( test($options, 'key1', 'FOO', __LINE__) ); + +// BAR +echo __LINE__, ' : '; var_dump( test($options, 'key3', 'FOO', __LINE__) ); + +// 22 +echo __LINE__, ' : '; var_dump( test($options, 'key3', null, __LINE__) ); + +// false (because getenv return false) +echo __LINE__, ' : '; var_dump( test($options, 'key3', 'XXX', __LINE__) ); + +// BAR +echo __LINE__, ' : '; var_dump( test([], 'key3', 'FOO', __LINE__) ); + +// false +echo __LINE__, ' : '; var_dump( test([], 'key3', 'XXX', __LINE__) ); + +// 34 +echo __LINE__, ' : '; var_dump( test([], 'key3', null, __LINE__) ); + +// BAR +echo __LINE__, ' : '; var_dump( test($options, null, 'FOO', __LINE__) ); + +// false +echo __LINE__, ' : '; var_dump( test($options, null, 'XXX', __LINE__) ); + +// 43 +echo __LINE__, ' : '; var_dump( test($options, null, null, __LINE__) ); + +// BAR +echo __LINE__, ' : '; var_dump( test([], null, 'FOO', __LINE__) ); + +// false +echo __LINE__, ' : '; var_dump( test([], null, 'XXX', __LINE__) ); + +// 52 +echo __LINE__, ' : '; var_dump( test([], null, null, __LINE__) ); +