taskman_env/env.inc.php

99 lines
1.8 KiB
PHP

<?php
namespace taskman;
use Exception;
$GLOBALS['TASKMAN_ROOT_ENV_NAME'] = 'gamectl.props.php';
task('envs', function()
{
global $GAME_ROOT;
$dir = normalize_path($GAME_ROOT."/env/", true);
echo "=== Available envs ===\n";
$results = scan_files_rec(array($dir), array(".props.php"));
foreach($results as $file)
{
$file = str_replace($dir, '', normalize_path($file, true));
$file = str_replace(".props.php", '', $file);
$file = ltrim($file, '/');
echo $file . "\n";
}
});
function put_env(&$argv)
{
global $GAME_ROOT;
$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_root_env_name($root_name)
{
$GLOBALS['TASKMAN_ROOT_ENV_NAME'] = $root_name;
}
function get_root_env_name()
{
return $GLOBALS['TASKMAN_ROOT_ENV_NAME'];
}
function include_env()
{
global $GAME_ROOT;
$env_filename = get_env_file();
if($env_filename && !file_exists($env_filename))
throw new Exception("No env file '$env_filename'");
if($env_filename)
include($env_filename);
}
function get_env_file()
{
global $GAME_ROOT;
$env = '';
if(getenv("GAME_ENV"))
$env = getenv("GAME_ENV");
if($env)
{
$env_filename = "$GAME_ROOT/env/$env.props.php";
return $env_filename;
}
//checking optional env.file
$env_filename = "$GAME_ROOT/" . get_root_env_name();
return file_exists($env_filename) ? $env_filename : null;
}