taskman_env/env.inc.php

132 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2022-05-17 19:01:32 +03:00
<?php
namespace taskman;
use Exception;
$GLOBALS['TASKMAN_ROOT_ENV_NAME'] = 'gamectl.props.php';
2022-05-17 19:01:32 +03:00
task('envs', function()
{
global $GAME_ROOT;
2023-10-25 20:28:19 +03:00
$dir = normalize_path($GAME_ROOT."/env/", true);
2022-05-17 19:01:32 +03:00
echo "=== Available envs ===\n";
$results = scan_files_rec(array($dir), array(".props.php"));
foreach($results as $file)
2023-10-25 20:28:19 +03:00
{
$file = str_replace($dir, '', normalize_path($file, true));
$file = str_replace(".props.php", '', $file);
$file = ltrim($file, '/');
echo $file . "\n";
}
2022-05-17 19:01:32 +03:00
});
function put_env(&$argv)
{
global $GAME_ROOT;
_set_env_vars_from_file();
2022-05-17 19:01:32 +03:00
$env = '';
$filtered = array();
for($i=0;$i<sizeof($argv);++$i)
{
$v = $argv[$i];
if(strpos($v, "--env") === 0)
{
list($tmp, $env) = explode("=", $v);
continue;
}
else
$filtered[] = $v;
}
$argv = $filtered;
//GAME_ENV env. variable has top priority
if(getenv("GAME_ENV"))
$env = getenv("GAME_ENV");
if($env)
putenv("GAME_ENV=$env");
//default one
else
putenv("GAME_ENV=");
}
function _set_env_vars_from_file()
{
global $GAME_ROOT;
if(!file_exists($GAME_ROOT . '/.env'))
return;
$var_arrs = array();
$lines = file($GAME_ROOT . '/.env');
foreach($lines as $line)
{
$line = trim($line);
$line_is_comment = (substr(trim($line),0 , 1) == '#') ? true: false;
if($line_is_comment || empty(trim($line)))
continue;
// Split the line variable and succeeding comment on line if exists
$line_no_comment = explode("#", $line, 2)[0];
// Split the variable name and value
$env_ex = preg_split('/(\s?)\=(\s?)/', $line_no_comment);
$env_name = trim($env_ex[0]);
$env_value = isset($env_ex[1]) ? trim($env_ex[1]) : "";
$var_arrs[$env_name] = $env_value;
}
foreach($var_arrs as $k => $v)
putenv("$k=$v");
}
function set_root_env_name($root_name)
{
$GLOBALS['TASKMAN_ROOT_ENV_NAME'] = $root_name;
}
function get_root_env_name()
{
return $GLOBALS['TASKMAN_ROOT_ENV_NAME'];
}
2022-05-17 19:01:32 +03:00
function include_env()
{
global $GAME_ROOT;
$env_filename = get_env_file();
if($env_filename && !file_exists($env_filename))
2023-10-25 20:28:19 +03:00
throw new Exception("No env file '$env_filename'");
2022-05-17 19:01:32 +03:00
if($env_filename)
include($env_filename);
}
function get_env_file()
{
global $GAME_ROOT;
$env = '';
if(getenv("GAME_ENV"))
$env = getenv("GAME_ENV");
if($env)
2023-10-25 20:28:19 +03:00
{
$env_filename = "$GAME_ROOT/env/$env.props.php";
2022-05-17 19:01:32 +03:00
return $env_filename;
2023-10-25 20:28:19 +03:00
}
//checking optional env.file
$env_filename = "$GAME_ROOT/" . get_root_env_name();
2022-05-17 19:01:32 +03:00
2023-10-25 20:28:19 +03:00
return file_exists($env_filename) ? $env_filename : null;
2022-05-17 19:01:32 +03:00
}