Beispiel #1
0
 public function test_session()
 {
     $app = super_app::get_app();
     $new_val = rand(1, 9999);
     $app->session->set('once', $new_val);
     $domain = AppServer::get_instance()->config->get('session_domain', __METHOD__);
     $test = $app->session->get('once');
     $this->_assert_true($new_val === $test && $new_val === $_SESSION[$domain]['once'], 'Run task');
 }
Beispiel #2
0
 /**
  * _get_test_methods : list all public method for current requested ctontroller
  * @return array function name list
  */
 private function _get_test_methods()
 {
     $app = super_app::get_app();
     $methods = get_class_methods($app->router->fetch_class());
     $testMethods = array();
     foreach ($methods as $method) {
         if (substr(strtolower($method), 0, 5) == 'test_') {
             $testMethods[] = $method;
         }
     }
     return $testMethods;
 }
Beispiel #3
0
 /**
  * on_request : Callback function after received HTTP request
  * @param  swoole_http_request  $request  request object
  * @param  swoole_http_response $response response object
  * @return None
  */
 public function on_request($request, $response)
 {
     // fire event
     $this->fire_event(__FUNCTION__, func_get_args(), false);
     // set super global variable
     if (method_exists($request, 'setGlobal')) {
         $request->setGlobal(HTTP_GLOBAL_ALL);
     } else {
         $this->_set_global_vars($request);
     }
     // output access log
     log_message('info', sprintf('%s %s........%s', $request->server['REQUEST_METHOD'], $request->server['REQUEST_URI'], ip_address()));
     $blResult = true;
     // load plugins
     $app = new super_app($this->load_http_plugins($request, $response));
     // save app instance into current process's slot
     $this->_apps[$this->_server->worker_id] = $app;
     $http_code = 200;
     $time_cost = 0;
     try {
         // cache the current ouput buffer
         $app->buffer->cache_output();
         // output example
         // echo '<h1>'.$request->server['REQUEST_METHOD'].' '.$request->server['REQUEST_URI'].' : '.rand(1000, 9999).'</h1><hr />';
         // parse url
         $segment = $app->router->parse_uri($request->server['REQUEST_URI'], $this->config->get('http_default_ctrl', 'base_ctrl'), $this->config->get('http_url_prefix', ''), $this->config->get('http_web_root', '.'));
         // check security
         if ($app->has('auth')) {
             if ($this->config->get('acl_on', true) && false === $app->auth->can()) {
                 throw new Exception('No access : ' . $request->server['REQUEST_URI'], 403);
             }
         }
         $time_start = microtime(true);
         // route the relevant class
         if (is_string($segment)) {
             // output directtly for the specified file
             $this->_server->sendfile($segment, $request->fd);
         } else {
             $cls_name = $app->router->fetch_class();
             $action_name = $app->router->fetch_action();
             // load class
             if (!self::load_class($cls_name, 'controllers', true)) {
                 throw new Exception('Failed to load : ' . $cls_name, 404);
             }
             // call to current action
             $http_code = $app->router->route();
         }
         $time_cost = 1000 * (microtime(true) - $time_start);
         // // change back to previous folder
         // if( !chdir($old_dir) ){
         //     throw new Exception('Failed to change back to folder : '.$old_dir, -5);
         // }
     } catch (Exception $e) {
         if ($e->getCode() > 0) {
             $http_code = $e->getCode();
         } else {
             $http_code = 500;
         }
         log_message('error', $e->getMessage());
         $blResult = false;
     }
     // flash all ouput buffer
     $content = $app->buffer->flash_output();
     // output access log
     log_message($http_code >= 400 ? 'error' : 'info', sprintf('%s %s........%d (%04f ms) : %d B: %s : %s', $request->server['REQUEST_METHOD'], $request->server['REQUEST_URI'], $http_code, $time_cost, strlen($content), $request->server['REMOTE_ADDR'], $request->header['user-agent']));
     // release the plugins
     $this->release_http_plugins($app->get_raw_data());
     unset($this->_apps[$this->_server->worker_id]);
     // finanlly, send out the response data to client
     $response->status($http_code);
     $response->end($content);
 }
Beispiel #4
0
 /**
  * get_model : get instance of specified model
  * @param  string $model  model name
  * @param  mixed  $params parameter for new model ctor
  * @return object         instance of model
  */
 public function get_model($model = '', $params = null)
 {
     return super_app::get_app()->get_model($model, $params);
 }