示例#1
0
 /**
  * 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;
     }
 }