mirror of
https://gitlab.easter-eggs.com/ee/ldapsaisie.git
synced 2024-11-18 00:09:06 +01:00
Add test_form_rule CLI command
You have to manually load LSformRule class to use this command, for instance : ldapsaisie -L LSformRule test_form_rule integer 12
This commit is contained in:
parent
8cb36c7aa9
commit
f635ad609a
2 changed files with 223 additions and 2 deletions
|
@ -564,9 +564,10 @@ class LScli extends LSlog_staticLoggerClass {
|
||||||
*
|
*
|
||||||
* @retval array List of matched class names
|
* @retval array List of matched class names
|
||||||
**/
|
**/
|
||||||
public static function autocomplete_class_name($prefix='') {
|
public static function autocomplete_class_name($prefix='', $quote_char=null) {
|
||||||
$classes = array();
|
$classes = array();
|
||||||
$quote_char = self :: unquote_word($prefix);
|
if (is_null($quote_char))
|
||||||
|
$quote_char = self :: unquote_word($prefix);
|
||||||
$regex = "/^class\.($prefix.*)\.php$/";
|
$regex = "/^class\.($prefix.*)\.php$/";
|
||||||
foreach(array(LS_ROOT_DIR."/".LS_CLASS_DIR, LS_ROOT_DIR."/".LS_LOCAL_DIR."/".LS_CLASS_DIR) as $dir_path) {
|
foreach(array(LS_ROOT_DIR."/".LS_CLASS_DIR, LS_ROOT_DIR."/".LS_LOCAL_DIR."/".LS_CLASS_DIR) as $dir_path) {
|
||||||
foreach (listFiles($dir_path, $regex) as $file) {
|
foreach (listFiles($dir_path, $regex) as $file) {
|
||||||
|
@ -763,6 +764,22 @@ class LScli extends LSlog_staticLoggerClass {
|
||||||
return self :: autocomplete_opts($ioFormats, $prefix, $case_sensitive, $quote_char);
|
return self :: autocomplete_opts($ioFormats, $prefix, $case_sensitive, $quote_char);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Autocomplete LSformRule name
|
||||||
|
*
|
||||||
|
* @param[in] $prefix string LSformRule name prefix (optional, default=empty string)
|
||||||
|
*
|
||||||
|
* @retval array List of matched LSformRule names
|
||||||
|
**/
|
||||||
|
public static function autocomplete_LSformRule_name($prefix='', $quote_char=null) {
|
||||||
|
$rules = array();
|
||||||
|
$quote_char = self :: unquote_word($prefix);
|
||||||
|
foreach(self :: autocomplete_class_name('LSformRule_'.$prefix, false) as $class) {
|
||||||
|
$rules[] = substr($class, 11);
|
||||||
|
}
|
||||||
|
return self :: autocomplete_opts($rules, $prefix, true, $quote_char);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unquote a word
|
* Unquote a word
|
||||||
*
|
*
|
||||||
|
|
|
@ -32,6 +32,14 @@ class LSformRule extends LSlog_staticLoggerClass {
|
||||||
// Validate values one by one or all together
|
// Validate values one by one or all together
|
||||||
const validate_one_by_one = True;
|
const validate_one_by_one = True;
|
||||||
|
|
||||||
|
// CLI parameters autocompleters
|
||||||
|
//
|
||||||
|
// This array accept as key the parameter name and as value a callable
|
||||||
|
// to autocomplete the parameter value. This callable will receive as
|
||||||
|
// first parameter the prefix of the parameter value already enter by
|
||||||
|
// user and must return
|
||||||
|
protected static $cli_params_autocompleters = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate form element values with specified rule
|
* Validate form element values with specified rule
|
||||||
*
|
*
|
||||||
|
@ -89,6 +97,181 @@ class LSformRule extends LSlog_staticLoggerClass {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CLI test_form_rule command
|
||||||
|
*
|
||||||
|
* @param[in] $command_args array Command arguments :
|
||||||
|
* - Positional arguments :
|
||||||
|
* - LSformRule type
|
||||||
|
* - values to test
|
||||||
|
* - Optional arguments :
|
||||||
|
* - -p|--param: LSformRule parameters (format: param=value)
|
||||||
|
*
|
||||||
|
* @retval boolean True on succes, false otherwise
|
||||||
|
**/
|
||||||
|
public static function cli_test_form_rule($command_args) {
|
||||||
|
$rule_name = null;
|
||||||
|
$values = array();
|
||||||
|
$params = array();
|
||||||
|
for ($i=0; $i < count($command_args); $i++) {
|
||||||
|
LScli :: unquote_word($command_args[$i]);
|
||||||
|
if (in_array($command_args[$i], array('-p', '--param'))) {
|
||||||
|
$i++;
|
||||||
|
LScli :: unquote_word($command_args[$i]);
|
||||||
|
$param_parts = explode('=', $command_args[$i]);
|
||||||
|
if (count($param_parts) != 2)
|
||||||
|
LScli :: usage('Invalid parameter string ('.$command_args[$i].').');
|
||||||
|
if (array_key_exists($param_parts[0], $params))
|
||||||
|
LScli :: usage('Parameter "'.$param_parts[0].'" already specified.');
|
||||||
|
$params[$param_parts[0]] = $param_parts[1];
|
||||||
|
}
|
||||||
|
else if (is_null($rule_name)) {
|
||||||
|
$rule_name = $command_args[$i];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$values[] = $command_args[$i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_null($rule_name) || empty($values))
|
||||||
|
LScli :: usage('You must provide LSformRule type and at least one value to test.');
|
||||||
|
|
||||||
|
self :: log_trace("test_form_rule($rule_name): params=".varDump($params));
|
||||||
|
$formElement = null;
|
||||||
|
$errors = self :: validate_values($rule_name, $values, array('params' => $params), $formElement);
|
||||||
|
if (is_array($errors)) {
|
||||||
|
print "Test triggered errors :\n - ".implode("\n - ", $errors)."\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print "No error detected in provided values.\n";
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Args autocompleter for CLI test_form_rule command
|
||||||
|
*
|
||||||
|
* @param[in] $command_args array List of already typed words of the command
|
||||||
|
* @param[in] $comp_word_num int The command word number to autocomplete
|
||||||
|
* @param[in] $comp_word string The command word to autocomplete
|
||||||
|
* @param[in] $opts array List of global available options
|
||||||
|
*
|
||||||
|
* @retval array List of available options for the word to autocomplete
|
||||||
|
**/
|
||||||
|
public static function cli_test_form_rule_args_autocompleter($command_args, $comp_word_num, $comp_word, $opts) {
|
||||||
|
$opts = array_merge($opts, array('-p', '--param'));
|
||||||
|
|
||||||
|
// Handle positional args
|
||||||
|
$rule_name = null;
|
||||||
|
$rule_class = null;
|
||||||
|
$rule_name_arg_num = null;
|
||||||
|
$params = array();
|
||||||
|
for ($i=0; $i < count($command_args); $i++) {
|
||||||
|
switch ($command_args[$i]) {
|
||||||
|
case '-p':
|
||||||
|
case '--params':
|
||||||
|
$i++;
|
||||||
|
$quote_char = LScli :: unquote_word($command_args[$i]);
|
||||||
|
$param_parts = explode('=', $command_args[$i]);
|
||||||
|
if (count($param_parts) > 2)
|
||||||
|
return;
|
||||||
|
$params[$i] = array(
|
||||||
|
'name' => $param_parts[0],
|
||||||
|
'value' => (isset($param_parts[1])?$param_parts[1]:null),
|
||||||
|
'quote_char' => $quote_char,
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
// If rule name not defined
|
||||||
|
if (is_null($rule_name)) {
|
||||||
|
// Defined it
|
||||||
|
$rule_name_quote_char = LScli :: unquote_word($command_args[$i]);
|
||||||
|
$rule_name = $command_args[$i];
|
||||||
|
LScli :: unquote_word($rule_name);
|
||||||
|
$rule_name_arg_num = $i;
|
||||||
|
|
||||||
|
// Check rule type exists
|
||||||
|
$rule_names = LScli :: autocomplete_LSformRule_name($rule_name);
|
||||||
|
|
||||||
|
// Load it if exist and not trying to complete it
|
||||||
|
if (in_array($rule_name, $rule_names) && $i != $comp_word_num) {
|
||||||
|
$rule_class = "LSformRule_$rule_name";
|
||||||
|
LSsession :: loadLSclass($rule_class, null, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, its value to test: can't complete it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self :: log_debug("rule type :'$rule_name' (#$rule_name_arg_num, class=$rule_class)");
|
||||||
|
self :: log_debug("params :".varDump($params));
|
||||||
|
|
||||||
|
// If rule name not already choiced (or currently autocomplete), add LSformRule types to available options
|
||||||
|
if (!$rule_name || $rule_name_arg_num == $comp_word_num)
|
||||||
|
$opts = array_merge($opts, LScli :: autocomplete_LSformRule_name($comp_word, $rule_name_quote_char));
|
||||||
|
|
||||||
|
else if ($rule_class && array_key_exists($comp_word_num, $params) && class_exists($rule_class)) {
|
||||||
|
if (is_null($params[$comp_word_num]['value'])) {
|
||||||
|
// Auto-complete parameter name
|
||||||
|
self :: log_debug("Auto-complete parameter name with prefix=".$params[$comp_word_num]['name']);
|
||||||
|
return $rule_class :: cli_test_form_rule_param_name_autocompleter(
|
||||||
|
$params[$comp_word_num]['name'], $params[$comp_word_num]['quote_char']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Auto-complete param value
|
||||||
|
self :: log_debug("Auto-complete parameter ".$params[$comp_word_num]['name']." value with prefix=".$params[$comp_word_num]['value']);
|
||||||
|
return $rule_class :: cli_test_form_rule_param_value_autocompleter(
|
||||||
|
$params[$comp_word_num]['name'], $params[$comp_word_num]['value'], $params[$comp_word_num]['quote_char']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return LScli :: autocomplete_opts($opts, $comp_word);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Args autocompleter for parameter name of CLI test_form_rule command
|
||||||
|
*
|
||||||
|
* @param[in] $prefix string Parameter name prefix (optional, default=empty string)
|
||||||
|
* @param[in] $quote_char $quote_char string Quote character (optional, default=empty string)
|
||||||
|
*
|
||||||
|
* @retval array List of available options for the word to autocomplete
|
||||||
|
**/
|
||||||
|
public static function cli_test_form_rule_param_name_autocompleter($prefix='', $quote_char='') {
|
||||||
|
$opts = LScli :: autocomplete_opts(array_keys(static :: $cli_params_autocompleters), $prefix);
|
||||||
|
self :: log_debug("cli_test_form_rule_param_name_autocompleter($prefix): opts = ".varDump($opts));
|
||||||
|
for($i=0; $i<count($opts); $i++)
|
||||||
|
$opts[$i] .= '=';
|
||||||
|
return LScli :: autocomplete_opts($opts, $prefix, true, $quote_char);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Args autocompleter for parameter name of CLI test_form_rule command
|
||||||
|
*
|
||||||
|
* @param[in] $param_name string The parameter name
|
||||||
|
* @param[in] $prefix string Parameter name prefix (optional, default=empty string)
|
||||||
|
* @param[in] $quote_char $quote_char string Quote character (optional, default=empty string)
|
||||||
|
*
|
||||||
|
* @retval array List of available options for the word to autocomplete
|
||||||
|
**/
|
||||||
|
public static function cli_test_form_rule_param_value_autocompleter($param_name, $prefix='', $quote_char='') {
|
||||||
|
if (
|
||||||
|
!array_key_exists($param_name, static :: $cli_params_autocompleters) ||
|
||||||
|
!is_callable(static :: $cli_params_autocompleters[$param_name])
|
||||||
|
)
|
||||||
|
return;
|
||||||
|
$opts = call_user_func(static :: $cli_params_autocompleters[$param_name], $prefix);
|
||||||
|
if (!is_array($opts))
|
||||||
|
return;
|
||||||
|
for($i=0; $i<count($opts); $i++)
|
||||||
|
$opts[$i] = $param_name."=".$opts[$i];
|
||||||
|
self :: log_debug("Options: ".varDump($opts));
|
||||||
|
return LScli :: autocomplete_opts($opts, "$param_name=$prefix", true, $quote_char);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class LSformRuleException extends Exception {
|
class LSformRuleException extends Exception {
|
||||||
|
@ -110,3 +293,24 @@ class LSformRuleException extends Exception {
|
||||||
LSerror :: defineError('LSformRule_01',
|
LSerror :: defineError('LSformRule_01',
|
||||||
___("LSformRule_%{type}: Parameter %{param} is not found.")
|
___("LSformRule_%{type}: Parameter %{param} is not found.")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// LScli
|
||||||
|
LScli :: add_command(
|
||||||
|
'test_form_rule',
|
||||||
|
array('LSformRule', 'cli_test_form_rule'),
|
||||||
|
'Test LSformRule',
|
||||||
|
'[LSformRule type] [-p param1=value] [value1] [value2]',
|
||||||
|
array(
|
||||||
|
' - Positional arguments :',
|
||||||
|
' - LSformRule type',
|
||||||
|
' - values to test',
|
||||||
|
'',
|
||||||
|
' - Optional arguments :',
|
||||||
|
' -p|--param LSformRule parameter using format:',
|
||||||
|
' param_name=param_value',
|
||||||
|
' Multiple parameters could be specified by using this',
|
||||||
|
' optional argument multiple time',
|
||||||
|
),
|
||||||
|
true,
|
||||||
|
array('LSformRule', 'cli_test_form_rule_args_autocompleter')
|
||||||
|
);
|
||||||
|
|
Loading…
Reference in a new issue