Пример #1
0
 /**
  * It returns a closure that calls the given function and validates it
  * It allows the function to be called in a safe way
  * 
  * It returns a closure that validates the parameters of the user given function
  * It then calls the function
  * After that it validates the return value of the function
  * In case of validation errors, the closure displays an error message and ends script execution
  * 
  * @since 1.0.0
  * 
  * @return object $closure an object of class Closure is returned. The closure function calls the given function with the given parameters	 
  */
 public static final function GetClosure()
 {
     /** The closure that validates and calls the user given function */
     $closure = function ($class_object, $function_name, $parameters, $application_context, $custom_validation_callback) {
         /** The reflection object is created. It provides the functions for parsing comments */
         $reflection = new Reflection();
         /** The parsed method comments */
         $parsed_comments = $reflection->ParseMethodDocBlockComments($class_object, $function_name);
         /** The internal tags. They are extracted using regular expressions */
         $interal_tags = $parsed_comments['internal'];
         /** The list of allowed application contexts for the method */
         $method_context = $interal_tags['context'];
         /** The application context is validated */
         $validation_result = $reflection->ValidateMethodContext($method_context, $application_context);
         /** The validation result is checked */
         if ($validation_result['is_valid'] === false) {
             throw new \Exception("Invalid method context. Details: " . $validation_result['validation_message']);
         }
         /** The test function parameters are validated */
         $validation_result = $reflection->ValidateMethodParameters($class_object, $function_name, $parameters, $custom_validation_callback);
         /** The validation result is checked */
         if ($validation_result['is_valid'] === false) {
             throw new \Exception("Function parameters could not be validated. Details: " . $validation_result['validation_message']);
         }
         /** The parameter values are extracted */
         $parameters = array_values($parameters);
         /** The test function is called */
         $result = $class_object->{$function_name}($parameters[0], $parameters[1], $parameters[2], $parameters[3]);
         /** The test function return value is validated */
         $validation_result = $reflection->ValidateMethodReturnValue($class_object, "AddNumbers", $result, $custom_validation_callback, $parameters);
         /** The validation result is checked */
         if ($validation_result['is_valid'] === false) {
             throw new \Exception("Function return value could not be validated. Details: " . $validation_result['validation_message']);
         }
         return $result;
     };
     return $closure;
 }