From 41d40e3c04d62d277ac0bff39e112ca8b337fd4d Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Wed, 22 Aug 2012 10:01:22 +0000 Subject: [PATCH] Split mibew_api.php into several files --- .../webim/MibewAPIExecutionContextTest.php | 2 +- .../tests/server_side/webim/MibewAPITest.php | 22 +- .../webim/libs/{ => classes}/mibew_api.php | 251 +----------------- .../classes/mibew_api_execution_context.php | 119 +++++++++ .../libs/classes/mibew_api_interaction.php | 97 +++++++ .../classes/mibew_api_window_interaction.php | 44 +++ 6 files changed, 283 insertions(+), 252 deletions(-) rename src/messenger/webim/libs/{ => classes}/mibew_api.php (60%) create mode 100644 src/messenger/webim/libs/classes/mibew_api_execution_context.php create mode 100644 src/messenger/webim/libs/classes/mibew_api_interaction.php create mode 100644 src/messenger/webim/libs/classes/mibew_api_window_interaction.php diff --git a/src/messenger/tests/server_side/webim/MibewAPIExecutionContextTest.php b/src/messenger/tests/server_side/webim/MibewAPIExecutionContextTest.php index 6b78c730..a35f1a94 100644 --- a/src/messenger/tests/server_side/webim/MibewAPIExecutionContextTest.php +++ b/src/messenger/tests/server_side/webim/MibewAPIExecutionContextTest.php @@ -1,6 +1,6 @@ assertEquals($mibew_api, MibewAPI::getAPI('base')); + $mibew_api = MibewAPI::getAPI('MibewAPIWindowInteraction'); + $this->assertEquals($mibew_api, MibewAPI::getAPI('MibewAPIWindowInteraction')); } /** * @depends testGetAPI */ public function testCheckFunction() { - $api = MibewAPI::getAPI('base'); + $api = MibewAPI::getAPI('MibewAPIWindowInteraction'); // Wrong function. Function name is absent $wrong_function = array(); @@ -165,7 +167,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase { * @depends testCheckFunction */ public function testCheckRequest($correct_function) { - $api = MibewAPI::getAPI('base'); + $api = MibewAPI::getAPI('MibewAPIWindowInteraction'); // Wrong request. Its 'token' element is absent. $wrong_request = array(); @@ -243,7 +245,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase { * @depends testCheckRequest */ public function testCheckPackage($correct_request) { - $api = MibewAPI::getAPI('base'); + $api = MibewAPI::getAPI('MibewAPIWindowInteraction'); $trusted_signatures = array('some_trusted_signature'); // Wrong package. Signature element is absent. @@ -415,7 +417,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase { * @depends testCheckPackage */ public function testEncodePackage($correct_package) { - $api = MibewAPI::getAPI('base'); + $api = MibewAPI::getAPI('MibewAPIWindowInteraction'); // Get package values $requests = $correct_package['requests']; @@ -437,7 +439,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase { * @depends testEncodePackage */ public function testDecodePackage($vars) { - $api = MibewAPI::getAPI('base'); + $api = MibewAPI::getAPI('MibewAPIWindowInteraction'); // Try to catch MibewAPIException with MibewAPIException::NOT_VALID_JSON code try { @@ -470,7 +472,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase { * @depends testGetAPI */ public function testGetResultFunction() { - $api = MibewAPI::getAPI('base'); + $api = MibewAPI::getAPI('MibewAPIWindowInteraction'); // Wrong functions list. More than one result function. $functions_list = array( @@ -578,7 +580,7 @@ class MibewAPITest extends PHPUnit_Framework_TestCase { * @depends testGetAPI */ public function testBuildResult() { - $api = MibewAPI::getAPI('base'); + $api = MibewAPI::getAPI('MibewAPIWindowInteraction'); $token = 'some_test_token'; $package = array( diff --git a/src/messenger/webim/libs/mibew_api.php b/src/messenger/webim/libs/classes/mibew_api.php similarity index 60% rename from src/messenger/webim/libs/mibew_api.php rename to src/messenger/webim/libs/classes/mibew_api.php index e283bd9c..108aa858 100644 --- a/src/messenger/webim/libs/mibew_api.php +++ b/src/messenger/webim/libs/classes/mibew_api.php @@ -15,6 +15,8 @@ * limitations under the License. */ +//require_once('mibew_api_interaction.php'); + /** * Implements Mibew API specification version 1.0 * @@ -43,31 +45,30 @@ Class MibewAPI { /** * Returns MibewAPI object * - * @param string $interaction_type A name of the interaction type + * @param string $class_name A name of the interaction type class * @return MibeAPI object * @throws MibewAPIException */ - public static function getAPI($interaction_type) { - $class_name = "MibewAPI". ucfirst($interaction_type) . "Interaction"; + public static function getAPI($class_name) { if (! class_exists($class_name)) { throw new MibewAPIException( "Wrong interaction type", MibewAPIException::WRONG_INTERACTION_TYPE ); } - if (empty(self::$interactions[$interaction_type])) { - self::$interactions[$interaction_type] = new self(new $class_name()); + if (empty(self::$interactions[$class_name])) { + self::$interactions[$class_name] = new self(new $class_name()); } - return self::$interactions[$interaction_type]; + return self::$interactions[$class_name]; } /** * Class constructor * - * @param MibewAPIInteraction $interaction_type Interaction type object + * @param MibewAPIInteraction $interaction Interaction type object */ - protected function __construct(MibewAPIInteraction $interaction_type) { - $this->interaction = $interaction_type; + protected function __construct(MibewAPIInteraction $interaction) { + $this->interaction = $interaction; } /** @@ -332,238 +333,6 @@ Class MibewAPI { } } -/** - * Implements functions execution context - */ -Class MibewAPIExecutionContext { - /** - * Values which returns after execution of all functions in request - * @var array - */ - protected $return = array(); - - /** - * Results of execution of all function in request - * @var array - */ - protected $functions_results = array(); - - /** - * Returns requets results - * - * @return array Request results - * @see MibewAPIExecutionContext::$return - */ - public function getResults () { - return $this->return; - } - - /** - * Build arguments list by replace all references by values of execution context - * - * @param array $function Function array. See MibewAPI for details. - * @return array Arguments list - * @throws MibewAPIException - */ - public function getArgumentsList ($function) { - $arguments = $function['arguments']; - $references = $function['arguments']['references']; - foreach ($references as $variable => $func_num) { - // Check target function in context - if (! isset($this->functions_results[$func_num - 1])) { - // Wrong function num - throw new MibewAPIException( - "Wrong reference in '{$function['function']}' function. " . - "Function #{$func_num} does not call yet.", - MibewAPIException::WRONG_FUNCTION_NUM_IN_REFERENCE - ); - } - - // Check reference - if (empty($arguments[$variable])) { - // Empty argument that should contains reference - throw new MibewAPIException( - "Wrong reference in '{$function['function']}' function. " . - "Empty {$variable} argument.", - MibewAPIException::EMPTY_VARIABLE_IN_REFERENCE - ); - } - $reference_to = $arguments[$variable]; - - // Check target value - if (! isset($this->functions_results[$func_num - 1][$reference_to])) { - // Undefined target value - throw new MibewAPIException( - "Wrong reference in '{$function['function']}' function. " . - "There is no '{$reference_to}' argument in #{$func_num} " . - "function results", - MibewAPIException::VARIABLE_IS_UNDEFINED_IN_REFERENCE - ); - } - - // Replace reference by target value - $arguments[$variable] = $this->functions_results[$func_num - 1][$reference_to]; - } - return $arguments; - } - - /** - * Stores functions results in execution context and add values to request result - * - * @param array $function Function array. See MibewAPI for details. - * @param array $results Associative array of the function results. - * @throws MibewAPIException - */ - public function storeFunctionResults ($function, $results) { - // Add value to request results - foreach ($function['arguments']['return'] as $name => $alias) { - if (! isset($results[$name])) { - // Value that defined in 'return' argument is undefined - throw new MibewAPIException( - "Variable with name '{$name}' is undefined in the " . - "results of the '{$function['function']}' function", - MibewAPIException::VARIABLE_IS_UNDEFINED_IN_RESULT - ); - } - $this->return[$alias] = $results[$name]; - } - // Store function results in execution context - $this->functions_results[] = $results; - } - -} - -/** - * Encapsulates interaction type - */ -abstract class MibewAPIInteraction { - /** - * Defines obligatory arguments and default values for them - * - * @var array Keys of the array are function names ('*' for all functions). Values are arrays of obligatory - * arguments with key for name of an argument and value for default value. - * - * For example: - * - * protected $obligatoryArguments = array( - * '*' => array( // Obligatory arguments for all functions are - * 'return' => array(), // 'return' with array() by default and - * 'references' => array() // 'references' with array() by default - * ), - * 'result' => array( // There is an additional argument for the result function - * 'errorCode' => 0 // This is 'error_code' with 0 by default - * ) - * ); - * - */ - protected $obligatoryArguments = array(); - - /** - * Reserved function's names - * - * Defines reserved(system) function's names described in the Mibew API. - * @var array - */ - public $reservedFunctionNames = array(); - - /** - * Returns obligatory arguments for the $function_name function - * - * @param string $function_name Function name - * @return array An array of obligatory arguments - */ - public function getObligatoryArguments($function_name) { - $obligatory_arguments = array(); - // Add obligatory for all functions arguments - if (! empty($this->obligatoryArguments['*'])) { - $obligatory_arguments = array_merge( - $obligatory_arguments, - array_keys($this->obligatoryArguments['*']) - ); - } - // Add obligatory arguments for given function - if (! empty($this->obligatoryArguments[$function_name])) { - $obligatory_arguments = array_merge( - $obligatory_arguments, - array_keys($this->obligatoryArguments[$function_name]) - ); - } - return array_unique($obligatory_arguments); - } - - /** - * Returns default values of obligatory arguments for the $function_name function - * - * @param string $function_name Function name - * @return array Associative array with keys are obligatory arguments and values are default - * values of them - */ - public function getDefaultObligatoryArguments($function_name) { - $obligatory_arguments = array(); - // Add obligatory for all functions arguments - if (! empty($this->obligatoryArguments['*'])) { - $obligatory_arguments = array_merge($obligatory_arguments, $this->obligatoryArguments['*']); - } - // Add obligatory arguments for given function - if (! empty($this->obligatoryArguments[$function_name])) { - $obligatory_arguments = array_merge($obligatory_arguments, $this->obligatoryArguments[$function_name]); - } - return $obligatory_arguments; - } -} - -/** - * Implements Base Mibew Interaction - */ -class MibewAPIBaseInteraction extends MibewAPIInteraction { - /** - * Defines obligatory arguments and default values for them - * @var array - * @see MibewAPIInteraction::$obligatoryArgumnents - */ - protected $obligatoryArguments = array( - '*' => array( - 'references' => array(), - 'return' => array() - ) - ); - - /** - * Reserved function's names - * @var array - * @see MibewAPIInteraction::$reservedFunctionNames - */ - public $reservedFunctionNames = array( - 'result' - ); -} - -/** - * Implements Mibew Core - Mibew Chat Window interaction - */ -class MibewAPIWindowInteraction extends MibewAPIInteraction { - /** - * Defines obligatory arguments and default values for them - * @var array - * @see MibewAPIInteraction::$obligatoryArgumnents - */ - protected $obligatoryArguments = array( - '*' => array( - 'references' => array(), - 'return' => array() - ) - ); - - /** - * Reserved function's names - * @var array - * @see MibewAPIInteraction::$reservedFunctionNames - */ - public $reservedFunctionNames = array( - 'result' - ); -} - /** * Mibew API Exception class. */ diff --git a/src/messenger/webim/libs/classes/mibew_api_execution_context.php b/src/messenger/webim/libs/classes/mibew_api_execution_context.php new file mode 100644 index 00000000..3aefe686 --- /dev/null +++ b/src/messenger/webim/libs/classes/mibew_api_execution_context.php @@ -0,0 +1,119 @@ +return; + } + + /** + * Build arguments list by replace all references by values of execution context + * + * @param array $function Function array. See MibewAPI for details. + * @return array Arguments list + * @throws MibewAPIException + */ + public function getArgumentsList ($function) { + $arguments = $function['arguments']; + $references = $function['arguments']['references']; + foreach ($references as $variable => $func_num) { + // Check target function in context + if (! isset($this->functions_results[$func_num - 1])) { + // Wrong function num + throw new MibewAPIException( + "Wrong reference in '{$function['function']}' function. " . + "Function #{$func_num} does not call yet.", + MibewAPIException::WRONG_FUNCTION_NUM_IN_REFERENCE + ); + } + + // Check reference + if (empty($arguments[$variable])) { + // Empty argument that should contains reference + throw new MibewAPIException( + "Wrong reference in '{$function['function']}' function. " . + "Empty {$variable} argument.", + MibewAPIException::EMPTY_VARIABLE_IN_REFERENCE + ); + } + $reference_to = $arguments[$variable]; + + // Check target value + if (! isset($this->functions_results[$func_num - 1][$reference_to])) { + // Undefined target value + throw new MibewAPIException( + "Wrong reference in '{$function['function']}' function. " . + "There is no '{$reference_to}' argument in #{$func_num} " . + "function results", + MibewAPIException::VARIABLE_IS_UNDEFINED_IN_REFERENCE + ); + } + + // Replace reference by target value + $arguments[$variable] = $this->functions_results[$func_num - 1][$reference_to]; + } + return $arguments; + } + + /** + * Stores functions results in execution context and add values to request result + * + * @param array $function Function array. See MibewAPI for details. + * @param array $results Associative array of the function results. + * @throws MibewAPIException + */ + public function storeFunctionResults ($function, $results) { + // Add value to request results + foreach ($function['arguments']['return'] as $name => $alias) { + if (! isset($results[$name])) { + // Value that defined in 'return' argument is undefined + throw new MibewAPIException( + "Variable with name '{$name}' is undefined in the " . + "results of the '{$function['function']}' function", + MibewAPIException::VARIABLE_IS_UNDEFINED_IN_RESULT + ); + } + $this->return[$alias] = $results[$name]; + } + // Store function results in execution context + $this->functions_results[] = $results; + } + +} + +?> \ No newline at end of file diff --git a/src/messenger/webim/libs/classes/mibew_api_interaction.php b/src/messenger/webim/libs/classes/mibew_api_interaction.php new file mode 100644 index 00000000..7688db98 --- /dev/null +++ b/src/messenger/webim/libs/classes/mibew_api_interaction.php @@ -0,0 +1,97 @@ + + * protected $obligatoryArguments = array( + * '*' => array( // Obligatory arguments for all functions are + * 'return' => array(), // 'return' with array() by default and + * 'references' => array() // 'references' with array() by default + * ), + * 'result' => array( // There is an additional argument for the result function + * 'errorCode' => 0 // This is 'error_code' with 0 by default + * ) + * ); + * + */ + protected $obligatoryArguments = array(); + + /** + * Reserved function's names + * + * Defines reserved(system) function's names described in the Mibew API. + * @var array + */ + public $reservedFunctionNames = array(); + + /** + * Returns obligatory arguments for the $function_name function + * + * @param string $function_name Function name + * @return array An array of obligatory arguments + */ + public function getObligatoryArguments($function_name) { + $obligatory_arguments = array(); + // Add obligatory for all functions arguments + if (! empty($this->obligatoryArguments['*'])) { + $obligatory_arguments = array_merge( + $obligatory_arguments, + array_keys($this->obligatoryArguments['*']) + ); + } + // Add obligatory arguments for given function + if (! empty($this->obligatoryArguments[$function_name])) { + $obligatory_arguments = array_merge( + $obligatory_arguments, + array_keys($this->obligatoryArguments[$function_name]) + ); + } + return array_unique($obligatory_arguments); + } + + /** + * Returns default values of obligatory arguments for the $function_name function + * + * @param string $function_name Function name + * @return array Associative array with keys are obligatory arguments and values are default + * values of them + */ + public function getDefaultObligatoryArguments($function_name) { + $obligatory_arguments = array(); + // Add obligatory for all functions arguments + if (! empty($this->obligatoryArguments['*'])) { + $obligatory_arguments = array_merge($obligatory_arguments, $this->obligatoryArguments['*']); + } + // Add obligatory arguments for given function + if (! empty($this->obligatoryArguments[$function_name])) { + $obligatory_arguments = array_merge($obligatory_arguments, $this->obligatoryArguments[$function_name]); + } + return $obligatory_arguments; + } +} + +?> \ No newline at end of file diff --git a/src/messenger/webim/libs/classes/mibew_api_window_interaction.php b/src/messenger/webim/libs/classes/mibew_api_window_interaction.php new file mode 100644 index 00000000..b550badf --- /dev/null +++ b/src/messenger/webim/libs/classes/mibew_api_window_interaction.php @@ -0,0 +1,44 @@ + array( + 'references' => array(), + 'return' => array() + ) + ); + + /** + * Reserved function's names + * @var array + * @see MibewAPIInteraction::$reservedFunctionNames + */ + public $reservedFunctionNames = array( + 'result' + ); +} + +?> \ No newline at end of file