Adding basic support for flt_str2num and flt_class validations by storing related info in config's extra data
Publish PHP Package / docker (push) Successful in 6s
Details
Publish PHP Package / docker (push) Successful in 6s
Details
This commit is contained in:
parent
d9aae4bee9
commit
f8a5f57505
|
@ -4,6 +4,10 @@ use Exception;
|
|||
|
||||
function flt_str2num($val, $name, $struct, $args)
|
||||
{
|
||||
$config_extra = $GLOBALS['CONFIG_EXTRAS'];
|
||||
if(!property_exists($config_extra, 'flt_str2num_entries'))
|
||||
$config_extra->flt_str2num_entries = array();
|
||||
|
||||
if(is_string($val))
|
||||
{
|
||||
if(strlen($val) === 0)
|
||||
|
@ -30,9 +34,27 @@ function flt_str2num($val, $name, $struct, $args)
|
|||
if(!is_numeric($val))
|
||||
throw new Exception("Bad value, not a number(" . serialize($val) . ")");
|
||||
|
||||
$config_extra->flt_str2num_entries[1*$val] = true;
|
||||
|
||||
return 1*$val;
|
||||
}
|
||||
|
||||
function validate_flt_str2num(\taskman\ConfigFetchResult $fetch_result, \taskman\ConfigCacheEntry $entry)
|
||||
{
|
||||
$config_extra = $entry->extras;
|
||||
|
||||
if(!property_exists($config_extra, 'flt_str2num_entries'))
|
||||
return;
|
||||
|
||||
foreach($config_extra->flt_str2num_entries as $conf_id => $_)
|
||||
{
|
||||
if($conf_id == 0)
|
||||
continue;
|
||||
|
||||
\taskman\config_find_by_id($fetch_result, $conf_id);
|
||||
}
|
||||
}
|
||||
|
||||
function flt_range($val, $name, $struct, $args)
|
||||
{
|
||||
if(count($args) != 2)
|
||||
|
@ -76,30 +98,65 @@ function flt_time($val, $name, $struct, $args)
|
|||
function flt_class($val, $name, $struct, $args)
|
||||
{
|
||||
global $GAME_ROOT;
|
||||
|
||||
if(is_string($val))
|
||||
{
|
||||
$conf_path = $val;
|
||||
|
||||
if(strpos($conf_path, '@') === 0)
|
||||
$conf_path = substr($conf_path, 1) . '.conf.js';
|
||||
|
||||
$conf_path = \taskman\config_real_path($conf_path);
|
||||
|
||||
$cce = \taskman\config_fetch_by_path($conf_path);
|
||||
$config_extra = $GLOBALS['CONFIG_EXTRAS'];
|
||||
if(!property_exists($config_extra, 'flt_class_entries'))
|
||||
$config_extra->flt_class_entries = array();
|
||||
|
||||
$conf_class = $cce->class;
|
||||
$target_class = $args;
|
||||
if(is_array($target_class))
|
||||
$target_class = $target_class[0];
|
||||
|
||||
$target_class = $args;
|
||||
if(is_array($target_class))
|
||||
$target_class = $target_class[0];
|
||||
if(!class_exists($target_class))
|
||||
throw new Exception($val.". Target class \"".$target_class."\" is not valid");
|
||||
|
||||
if(!class_exists($target_class))
|
||||
throw new Exception($val.". Target class \"".$target_class."\" is not valid");
|
||||
|
||||
if($conf_class != $target_class && !is_subclass_of($conf_class, $target_class))
|
||||
throw new Exception($val.". Config class is \"".$conf_class."\". Must be \"".$target_class."\" or it's child");
|
||||
}
|
||||
$config_extra->flt_class_entries[$val] = $target_class;
|
||||
|
||||
return $val;
|
||||
}
|
||||
|
||||
function validate_flt_class(\taskman\ConfigFetchResult $fetch_result, \taskman\ConfigCacheEntry $entry)
|
||||
{
|
||||
$config_extra = $entry->extras;
|
||||
|
||||
if(!property_exists($config_extra, 'flt_class_entries'))
|
||||
return;
|
||||
|
||||
foreach($config_extra->flt_class_entries as $conf_ref => $target_class)
|
||||
{
|
||||
$conf_cache = null;
|
||||
|
||||
if(is_string($conf_ref))
|
||||
{
|
||||
if(empty($conf_ref))
|
||||
continue;
|
||||
|
||||
$conf_path = $conf_ref;
|
||||
|
||||
if(strpos($conf_path, '@') === 0)
|
||||
$conf_path = substr($conf_path, 1) . '.conf.js';
|
||||
|
||||
$conf_path = \taskman\normalize_path(\taskman\config_real_path($conf_path), true);
|
||||
|
||||
$conf_cache = \taskman\config_find_by_path($fetch_result, $conf_path);
|
||||
}
|
||||
else if(is_int($conf_ref))
|
||||
{
|
||||
if($conf_ref == 0)
|
||||
continue;
|
||||
|
||||
$conf_cache = \taskman\config_find_by_id($fetch_result, $conf_ref);
|
||||
}
|
||||
else
|
||||
throw new Exception("Invalid config ref: '$conf_ref'");
|
||||
|
||||
if($conf_cache == null)
|
||||
throw new Exception("Config not found by ref: '$conf_ref'");
|
||||
|
||||
$conf_class = $conf_cache->class;
|
||||
|
||||
if($conf_class != $target_class && !is_subclass_of($conf_class, $target_class))
|
||||
throw new Exception("Config class is '$conf_class'. Must be '$target_class' or its child");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue