public static final function trySetCurrentUserEntity() { $token = Loader::loadInput()->input('token'); if (TRUE === Kit::isString($token) and '' !== $token) { $class_name = Loader::includeCore('User/User'); try { self::$currentUser = $class_name::getCurrentUserEntity($token); self::$currentInstitution = self::$currentUser->getInstitution()->setReadOnly(); } catch (Exception $e) { self::$currentUser = NULL; self::$currentInstitution = NULL; } } }
/** * Extracts args and handles the request, * by choosing the appropriate handler, * and calling the appropriate method * if $description CAN fit $this->uri. * @param string $description eg. '/project/(num)', '/(num)', '/', '/user/(any)', '(all)' * @param mixed $handler eg. 'Project', $this, an anonymous function * @param string|NULL $function eg. 'view' * @param boolean $is_time_consuming * @return boolean */ private final function fitGeneral($description, $handler, $function = NULL, $is_time_consuming = FALSE) { /** * eg. $description : '/project/(id:num)' => '/project/([0-9]+?)' * $this->uri : '/project/12' * $match_list : ['/project/12', '12'] */ $pattern = $this->getPattern($description); // It will attempt to match the whole $this->uri string. $uri = rtrim($this->uri, '/'); // $this->uri contains no GET args. if (1 === preg_match($pattern, $uri, $match_list)) { preg_match_all('@([^:\\(\\)]+):([^:\\(\\)]+)@', $description, $m, PREG_SET_ORDER); $mapping = []; foreach ($m as $value) { $mapping[$value[1]] = $value[2]; // 'id' => 'num' } $Input = Loader::loadInput(); foreach ($match_list as $key => $value) { // [0 => 12, 'id' => 12] if (TRUE === Kit::isInt($key)) { unset($match_list[$key]); } elseif ('num' === $mapping[$key]) { $Input->setInput($key, intval($value)); } else { $Input->setInput($key, $value); } } if (TRUE === Kit::isString($handler) or FALSE === $handler instanceof \Closure) { // $handler is a string or IS NOT an anonymous function, i.e., an instance. Kit::ensureString($function); $this->end(call_user_func_array([TRUE === Kit::isString($handler) ? Loader::loadService($handler) : $handler, $function], [$is_time_consuming])); } elseif (TRUE === is_callable($handler)) { // $handler is an anonymous function. $this->end(call_user_func_array($handler, [$is_time_consuming])); } // CAN FIT! return TRUE; } else { // CAN NOT FIT! return FALSE; } }
private final function handleResult($type, $name, $value, $is_list) { Kit::ensureString($name, TRUE); if (TRUE === Kit::isString($name)) { if (TRUE === Kit::isVacancy($value)) { // (valid) return $this->getResult($type, $name); } // (valid, valid/NULL) return $this->setResult($type, $name, $value, $is_list); } elseif (TRUE === is_null($name)) { if (TRUE === Kit::isVacancy($value)) { // (NULL) / () return $this->getResult($type, NULL); } // (NULL, valid/NULL) throw new UserException('Invalid $value when $name is NULL.', $value); } else { // (invalid, valid/NULL/empty) throw new UserException('Invalid $name.', $name); } }