diff --git a/inc/cache.php b/inc/cache.php index 33e03746..6e81039a 100644 --- a/inc/cache.php +++ b/inc/cache.php @@ -12,10 +12,10 @@ defined('TINYBOARD') or exit; class Cache { private static function buildCache(): CacheDriver { global $config; - // Determine if Redis is configured via environment variables - getenv('VICHAN_CACHE_ENGINE') && $config['cache']['enabled'] = getenv('VICHAN_CACHE_ENGINE'); + // Determine if the cache engine is configured via environment variables. + $engine = \getenv('VICHAN_CACHE_ENGINE') ?: $config['cache']['enabled']; - switch ($config['cache']['enabled']) { + switch ($engine) { case 'memcached': return new MemcachedCacheDriver( $config['cache']['prefix'], @@ -31,7 +31,7 @@ class Cache { $port = getenv('VICHAN_CACHE_PORT') ?: $port; $password = getenv('VICHAN_CACHE_PASSWORD') ?: $password; $database = getenv('VICHAN_CACHE_DATABASE') ?: $database; - + return new RedisCacheDriver( $config['cache']['prefix'], $host, diff --git a/inc/template.php b/inc/template.php index 47b7bc0f..11d9ea10 100644 --- a/inc/template.php +++ b/inc/template.php @@ -142,7 +142,8 @@ class Tinyboard extends Twig\Extension\AbstractExtension new Twig\TwigFunction('ratio', 'twig_ratio_function'), new Twig\TwigFunction('secure_link_confirm', 'twig_secure_link_confirm'), new Twig\TwigFunction('secure_link', 'twig_secure_link'), - new Twig\TwigFunction('link_for', 'link_for') + new Twig\TwigFunction('link_for', 'link_for'), + new Twig\TwigFunction('check_container', 'twig_check_container') ); } @@ -226,18 +227,18 @@ function twig_secure_link($href) { return $href . '/' . make_secure_link_token($href); } -// /* -// * ==================== -// * Container Detection -// * =================== -// */ +/* + * ==================== + * Container Detection + * =================== + */ function twig_check_container() { - global $config; - $isDocker = is_file("/.dockerenv") || is_file("/run/.containerenv"); - $isKubernetes = is_file("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); - $config['is_container'] = $isDocker || $isKubernetes ? true : false; - $config['is_docker'] = $isDocker; - $config['is_kubernetes'] = $isKubernetes; - return $config['is_container']; -} \ No newline at end of file + static $is_container = null; + if ($is_container === null) { + $is_docker = \is_file("/.dockerenv") || \is_file("/run/.containerenv"); + $is_kubernetes = \is_file("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); + $is_container = $is_docker || $is_kubernetes; + } + return $is_container; +}