Skip to content

Commit

Permalink
1.39.0
Browse files Browse the repository at this point in the history
[*] changed NginxToolkit env keys format and names (simplified)
[!] changed setOption function
  • Loading branch information
wombat committed Sep 25, 2019
1 parent 083a1fc commit a8fc731
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
28 changes: 21 additions & 7 deletions functions/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,21 +47,35 @@ 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
* @param mixed $env_key
* @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;
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions sources/Toolkit/NginxToolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
53 changes: 53 additions & 0 deletions tests/test_setoption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

require_once __DIR__ . DIRECTORY_SEPARATOR . '../vendor/autoload.php';

use function Arris\setOption;
use function Arris\setOption as test;

$options = [
'key1' => '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__) );

0 comments on commit a8fc731

Please sign in to comment.