示例#1
0
 /**
  * Encapsulates global variables.
  */
 public final function __construct()
 {
     self::$getData = new Container();
     self::$postData = new Container();
     // self::$cookieData  = new Container();
     self::$inputData = new Container();
     self::merge('get', $_GET);
     self::merge('post', $_POST);
     // self::merge('cookie', $_COOKIE);
     $opts = ['http' => ['timeout' => 60]];
     $context = stream_context_create($opts);
     $input = file_get_contents('php://input', FALSE, $context);
     $data = json_decode($input, TRUE);
     if (TRUE === is_null($data) and Kit::len($input) > 0) {
         throw new UserException(json_last_error_msg(), $input);
     }
     if (FALSE === is_null($data)) {
         self::merge('post', $data);
     }
     $limit = 100000;
     if (Kit::len(json_encode(self::input())) > $limit) {
         throw new UserException("Input size exceeds limit({$limit}).");
     }
     self::deleteInput('_url');
 }
示例#2
0
 private final function getMethodVisibility($methods_visibility, $method_name)
 {
     if (TRUE === isset($methods_visibility[self::V_PUBLIC]) and TRUE === isset($methods_visibility[self::V_PROTECTED]) and Kit::len(array_intersect($methods_visibility[self::V_PUBLIC], $methods_visibility[self::V_PROTECTED])) > 0) {
         throw new UserException('Public duplicates protected.', $methods_visibility);
     }
     foreach ([self::V_PUBLIC, self::V_PROTECTED] as $type) {
         if (TRUE === isset($methods_visibility[$type]) and TRUE === Kit::in($method_name, $methods_visibility[$type])) {
             return $type;
         }
     }
     return self::V_PRIVATE;
 }
示例#3
0
 public static final function isLogin($user_type_list)
 {
     $result = (TRUE === isset(self::$currentUser) and TRUE === self::$currentUser instanceof UserEntity);
     if (FALSE === $result or 0 === Kit::len($user_type_list)) {
         return $result;
     }
     foreach ($user_type_list as $user_type) {
         if ($user_type === self::$currentUser->getType()) {
             return TRUE;
         }
     }
     return FALSE;
 }
示例#4
0
 /**
  * 
  * check all array keys and values in $pattern being consts in this class
  * check all array values in $pattern using several 'if'. DO NOT use 'else'
  * @param 
  * @return boolean
  * @throws UserException if there is unknown pattern tag found.
  */
 public static final function validate($pattern, $data)
 {
     if (TRUE === is_null(self::$patternTagNameList)) {
         self::$patternTagNameList = array_keys((new ReflectionClass(get_class()))->getConstants());
     }
     if (TRUE === is_null(self::$patternTagValueList)) {
         self::$patternTagValueList = array_values((new ReflectionClass(get_class()))->getConstants());
     }
     $tag_list = array_merge(self::$patternTagNameList, self::$patternTagValueList);
     if (Kit::len($unknown_tag_list = array_diff(array_keys($pattern), $tag_list)) > 0) {
         throw new UserException('Unknown pattern tag found.', $unknown_tag_list);
     }
     return TRUE;
 }
 public static final function rollback()
 {
     if (0 === Kit::len(self::$history)) {
         return FALSE;
     }
     foreach (Kit::reversed(self::$history) as $operation) {
         if (self::OP_INSERT === $operation['Type']) {
             $operation['Collection']->deleteTheOnlyOne(['_id' => $operation['Id']], TRUE);
         } elseif (self::OP_UPDATE === $operation['Type']) {
             $operation['Collection']->updateTheOnlyOne(['_id' => $operation['Document']['_id']], $operation['Document'], TRUE);
         } elseif (self::OP_REMOVE === $operation['Type']) {
             $operation['Collection']->addOne($operation['Document'], TRUE);
         }
     }
     self::$isChanged = FALSE;
     return TRUE;
 }
示例#6
0
 public final function count()
 {
     return Kit::len($this->itemList);
 }
示例#7
0
 /**
  * @param int     $execution_id
  * @param array   $execution_record
  * @param int     $status_code
  * @param boolean $close_cgi_only
  */
 private final function respond($execution_id, $execution_record, $status_code, $close_cgi_only = FALSE)
 {
     if (FALSE === $close_cgi_only) {
         $this->result['database'] = [];
         if (2 !== $this->getCode()) {
             $this->result['database']['rollbacked'] = MDBC::rollback();
         } else {
             $this->result['database']['rollbacked'] = FALSE;
         }
         $this->result['database']['changed'] = MDBC::isChanged();
         Debug::updateExecutionRecord($execution_id, $execution_record);
         Debug::popExecutionId($execution_id);
     }
     if (FALSE === is_null($error = error_get_last()) and TRUE === Debug::isErrorCared($error)) {
         Debug::handleFatalError($error);
     }
     header('Content-Type : application/json', TRUE, $status_code);
     if (FALSE === Debug::isProduction()) {
         $this->result['monitor'] = Debug::getMonitor();
         $this->result += Debug::getDebugInfo();
         $this->result += ['size' => sprintf('%.2fKB', Kit::len(json_encode($this->result)) / 1024)];
         if (TRUE === is_null($this->result['mainException'])) {
             unset($this->result['mainException']);
         }
         if (TRUE === is_null($this->result['monitor'])) {
             unset($this->result['monitor']);
         }
     } else {
         unset($this->result['mainException']);
         unset($this->result['monitor']);
         unset($this->result['database']);
         unset($this->result['process']);
     }
     Http::json($this->result);
     if (TRUE === $close_cgi_only) {
         fastcgi_finish_request();
         // DO NOT exit in order to run the subsequent scripts.
     } else {
         exit;
     }
 }
示例#8
0
 /**
  * eg. 'Collection/Content/ResourceCollection' => 'Content/Resource'
  * eg. 'Entity/Content/ResourceEntity'         => 'Content/Resource'
  */
 public static final function getModelPath($model_class_name, $delimiter = '\\')
 {
     Kit::ensureString($model_class_name);
     Kit::ensureString($delimiter);
     $handler_prefix = self::getHandlerPrefixFromPath($model_class_name);
     // 'Resource'
     $word_list = Kit::split($delimiter, $model_class_name);
     while (Kit::len($word_list) > 0 and 'Model' !== $word_list[0]) {
         $word_list = Kit::slice($word_list, 1);
     }
     $word_list = Kit::slice($word_list, 2);
     // [ 'Content', 'ResourceCollection' ]
     Kit::popList($word_list);
     // [ 'Content' ]
     $word_list[] = $handler_prefix;
     // [ 'Content', 'Resource' ]
     return Kit::join('/', $word_list);
     // 'Content/Resource'
 }