Exemplo n.º 1
0
 private final function execute($method_name, $arg_list, $call_parent = FALSE)
 {
     $execution_record = [];
     $execution_id = Debug::addExecutionRecord($execution_record);
     Debug::pushExecutionId($execution_id);
     try {
         $execution_record = $this->prepareExecutionRecord($method_name, $arg_list, $call_parent);
         $class_name = $execution_record['class'];
         $declaring_class_name = $execution_record['declaring_class'];
         $method_accessibility = $execution_record['method_accessibility'];
         $handler_prefix = $execution_record['handler_prefix'];
         $handler_suffix = $execution_record['handler_suffix'];
         Debug::updateExecutionRecord($execution_id, $execution_record);
         if (($count = Debug::countExecutionRecord()) > 3000000) {
             throw new UserException('Abnormal execution record count.', $count);
         }
         if (FALSE === $method_accessibility) {
             throw new UserException("Handler({$declaring_class_name} :: {$method_name}) is not accessible.", $execution_record);
         }
         $config_model_name = $this->loadConfig(Loader::getModelPath($declaring_class_name));
         // Method validateModelPrivilege should throw exception if the validation fails.
         $execution_record['validateModelPrivilege'] = $this->{$config_model_name}->validateModelPrivilege($handler_suffix, $method_name);
         $data_model_name = $this->loadData(Loader::getModelPath($declaring_class_name));
         // Method validateArgs should throw exception if the validation fails,
         // and it should load the config model and fetch the config info itself.
         $args_validation_result = $execution_record['validateArgs'] = $this->{$data_model_name}->validateArgs($handler_suffix, $method_name, $arg_list);
         // Now the validation passed.
         // Method sanitizeArgs should load the config model and fetch the config info itself.
         $args_sanitization_result = $execution_record['sanitizeArgs'] = $this->{$data_model_name}->sanitizeArgs($handler_suffix, $method_name, $arg_list, $args_validation_result);
         // @TODO: check it, can be called with context info?
         // so that XCollection will work correctly.
         // $method = new ReflectionMethod($declaring_class_name, $method_name);
         // $method->setAccessible(TRUE);
         $result = $execution_record['result'] = call_user_func_array([$declaring_class_name, $method_name], $args_sanitization_result);
         // Method validateResult should throw exception if the validation fails,
         // and it should load the config model and fetch the config info itself.
         $result_validation_result = $execution_record['validateResult'] = $this->{$data_model_name}->validateResult($handler_suffix, $method_name, $result);
         // Now the validation passed.
         // Method sanitizeResult should load the config model and fetch the config info itself.
         $result_sanitization_result = $execution_record['sanitizeResult'] = $this->{$data_model_name}->sanitizeResult($handler_suffix, $method_name, $result, $result_validation_result);
         $execution_record['success'] = TRUE;
         Debug::updateExecutionRecord($execution_id, $execution_record);
         Debug::popExecutionId($execution_id);
         return $result;
     } catch (Exception $e) {
         $execution_record['success'] = FALSE;
         Debug::updateExecutionRecord($execution_id, $execution_record);
         Debug::popExecutionId($execution_id);
         throw new UserException('Model execution failed.', $execution_record, $e);
     }
 }
Exemplo n.º 2
0
 /**
  * @param string $method_name
  * @param array  $arg_list
  */
 public final function __call($method_name, $arg_list)
 {
     $execution_record = [];
     $execution_id = Debug::addExecutionRecord($execution_record);
     Debug::pushExecutionId($execution_id);
     try {
         $execution_record = $this->prepareExecutionRecord($method_name, $arg_list);
         $class_name = $execution_record['class'];
         $input = $execution_record['input'];
         $method_accessibility = $execution_record['method_accessibility'];
         $handler_prefix = $execution_record['handler_prefix'];
         $handler_suffix = $execution_record['handler_suffix'];
         Debug::updateExecutionRecord($execution_id, $execution_record);
         if (FALSE === $method_accessibility) {
             throw new UserException("Handler({$class_name} :: {$method_name}) is not accessible.", $execution_record);
         }
         $config_model_name = $this->configModelName;
         if (TRUE === is_null($config_model_name) or TRUE === is_null($this->{$config_model_name})) {
             throw new UserException("Config model({$config_model_name}) not loaded in {$class_name}.");
         }
         // Method validateModelPrivilege should throw exception if the validation fails.
         $execution_record['validateModelPrivilege'] = $this->{$config_model_name}->validateModelPrivilege($handler_suffix, $method_name);
         $data_model_name = $this->dataModelName;
         if (TRUE === is_null($data_model_name) or TRUE === is_null($this->{$data_model_name})) {
             throw new UserException("Data model({$data_model_name}) not loaded in {$class_name}.");
         }
         // Method validateInput should throw exception if the validation fails,
         // and it should load the config model and fetch the config info itself.
         $input_validation_result = $execution_record['validateInput'] = $this->{$data_model_name}->validateInput($method_name, $input);
         // Now the validation passed.
         $execution_record['is_time_consuming'] = $is_time_consuming = $arg_list[0];
         if (TRUE === $is_time_consuming) {
             $this->succeedRequest(NULL, NULL, TRUE);
         }
         // Method sanitizeInput should load the config model and fetch the config info itself.
         $input_sanitization_result = $execution_record['sanitizeInput'] = $this->{$data_model_name}->sanitizeInput($method_name, $input, $input_validation_result);
         $this->{$method_name}($input_sanitization_result);
         $service_result = $execution_record['service_result'] = Kit::extract($this->result, ['data', 'status']);
         $code = $this->getCode();
         if (TRUE === is_null($code)) {
             $this->fail();
         }
         $code = $this->getCode();
         if (FALSE === Kit::in($code, [1, 2])) {
             throw new UserException('Invalid code after service has finished.', $code);
         }
         if (1 === $code) {
             $this->succeedRequest($execution_id, $execution_record);
         }
         // END NOW
         // Now code must be 2.
         // Method validateServiceResult should throw exception if the validation fails,
         // and it should load the config model and fetch the config info itself.
         // @CAUTION
         $service_result_validation_result = $execution_record['validateServiceResult'] = $this->{$data_model_name}->validateServiceResult($method_name, $service_result);
         // Now the validation passed.
         // Method sanitizeServiceResult should load the config model
         // and fetch the config info itself.
         $service_result_sanitization_result = $execution_record['sanitizeServiceResult'] = $this->{$data_model_name}->sanitizeServiceResult($method_name, $service_result, $service_result_validation_result);
         // $service_result_validation_result should contains
         // and only contains three fields: code, data, status.
         $this->result['data'] = $computation_data = $service_result_sanitization_result['data'];
         $this->result['status'] = $operation_status = $service_result_sanitization_result['status'];
         // $this->loadLog('Request');
         // $this->RequestLog->addRequestLog(
         //     $execution_record['class'],
         //     $execution_record['method'],
         //     $input,
         //     $code,
         //     $operation_status
         // );
         $this->succeedRequest($execution_id, $execution_record);
     } catch (Exception $e) {
         $this->failRequest($execution_id, $execution_record, new UserException('Service execution failed.', $execution_record, $e));
     }
 }