Example #1
0
 /**
  * trigger the handler
  *
  * @return void
  */
 public function handle()
 {
     $this->log();
     // when not in development we respond using a route
     if (!ClanCats::in_development() && !ClanCats::is_cli()) {
         CCResponse::error(500)->send(true);
     } else {
         $this->respond();
     }
 }
Example #2
0
 /**
  * Set up the basic uri filters in our static init and 
  * also add a default 404 response
  *
  * @return void
  */
 public static function _init()
 {
     // default string
     static::filter('any', '[a-zA-Z0-9' . ClanCats::$config->get('router.allowed_special_chars') . ']');
     // only numbers
     static::filter('num', '[0-9]');
     // only alpha characters
     static::filter('alpha', '[a-zA-Z]');
     // only alphanumeric characters
     static::filter('alphanum', '[a-zA-Z0-9]');
     // 404 not found error
     CCRouter::on('#404', function () {
         return CCResponse::create(CCView::create('Core::CCF/404')->render(), 404);
     });
 }
 /**
  * Sign out action
  */
 public function action_fortunes()
 {
     $view = CCView::create('bench/fortune');
     $fortunes = DB::select('Fortune')->run();
     $runtimeFortune = new stdClass();
     $runtimeFortune->id = 0;
     $runtimeFortune->message = 'Additional fortune added at request time.';
     $fortunes[] = $runtimeFortune;
     usort($fortunes, function ($left, $right) {
         if ($left->message === $right->message) {
             return 0;
         } else {
             if ($left->message > $right->message) {
                 return 1;
             } else {
                 return -1;
             }
         }
     });
     $view->fortunes = $fortunes;
     return CCResponse::create($view->render());
 }
Example #4
0
 /**
  * controller execution
  * 
  * @param string		$action
  * @param array 		$params
  * @return CCResponse
  */
 public function execute($action = null, $params = array())
 {
     $this->set_language_alias($action);
     // reset the response
     $this->response = null;
     $this->output = null;
     // fix notice
     $return = null;
     if (is_null($action)) {
         $action = static::$_default_action;
     }
     // capture all output made in the controller
     ob_start();
     // run wake event
     if (method_exists($this, 'wake')) {
         $return = call_user_func_array(array($this, 'wake'), $params);
     }
     // do we already have an resposne from the wake event?
     if (!$return instanceof CCResponse) {
         // check if we got a custom action handler
         if (!is_null(static::$_action_handler)) {
             $return = call_user_func(array($this, static::$_action_handler), $action, $params);
         } else {
             // check if the action exists
             if (method_exists($this, static::$_action_prefix . $action)) {
                 $return = call_user_func_array(array($this, static::$_action_prefix . $action), $params);
             } else {
                 $return = CCResponse::error(404);
             }
         }
     }
     // get the output
     $this->output = ob_get_clean();
     // do we got a response now?
     if ($return instanceof CCResponse) {
         $this->response = $return;
     } elseif (!empty($return) && is_string($return)) {
         $this->response = $this->_respond($return);
     }
     // do we got a valid response if not use controller output
     if (!$this->response instanceof CCResponse) {
         $this->response = $this->_respond($this->output);
     }
     // run sleep event
     if (method_exists($this, 'sleep')) {
         call_user_func_array(array($this, 'sleep'), $params);
     }
     // return thaaaaat
     return $this->response;
 }
Example #5
0
 /**
  * Execute the Request
  *
  * @param array 	$action
  * @param array 	$params
  *
  * @return self
  */
 public function perform()
 {
     // set the input
     if (!is_null($this->input)) {
         CCIn::instance($this->input);
     } else {
         CCIn::instance(CCServer::instance());
     }
     // set current request
     static::$_current =& $this;
     // route is invalid show 404
     if (!$this->route instanceof CCRoute) {
         $this->route = CCRouter::resolve('#404');
     }
     /*
      * call wake events
      * if one event returns an response all other calls will be skipped also events!
      */
     foreach (CCRouter::events_matching('wake', $this->route->uri) as $callback) {
         if (($return = CCContainer::call($callback)) instanceof CCResponse) {
             $this->response = $return;
             return $this;
         }
     }
     /*
      * a closure
      */
     if (!is_array($this->route->callback) && is_callable($this->route->callback)) {
         // execute and capture the output
         ob_start();
         // run the closure
         $return = call_user_func_array($this->route->callback, $this->route->params);
         // catch the output
         $output = ob_get_clean();
         // do we got a response?
         if (!$return instanceof CCResponse) {
             // if not create one with the captured output
             $return = CCResponse::create($output);
         }
     } elseif (is_callable($this->route->callback)) {
         // execute the callback and get the return
         $return = call_user_func_array($this->route->callback, array($this->route->action, $this->route->params));
         // do we got a response?
         if (!$return instanceof CCResponse) {
             // if not create one with the return as string
             $return = CCResponse::create((string) $return);
         }
     } else {
         $return = CCResponse::error(404);
     }
     // set the response
     $this->response = $return;
     /*
      * call sleep events
      * if one event returns an response all other calls will be skipped also events!
      */
     foreach (CCRouter::events_matching('sleep', $this->route->uri) as $callback) {
         if ($return = CCContainer::call($callback, $this->response) instanceof CCResponse) {
             $this->response = $return;
             return $this;
         }
     }
     return $this;
 }
Example #6
0
 /**
  * return a response
  */
 public function action_response()
 {
     return \CCResponse::create('Response');
 }
Example #7
0
 /**
  * Test response send
  */
 public function testSend()
 {
     // create an instance
     $response = CCResponse::create();
     // just run the method to check for errors
     $response->send();
     // just run the method to check for errors
     $response->send(true);
 }
Example #8
0
 /**
  * Server error 500
  */
 public function action_500()
 {
     return CCResponse::create(CCView::create('Core::CCF/500')->render(), 500);
 }
Example #9
0
 /**
  * Create a CCRespone of the image
  *
  * @param string 		$quality			The image quality
  * @param string 		$type			jpg|png|gif
  * @return CCresponse
  */
 public function response($quality = null, $type = null)
 {
     $response = CCResponse::create($this->stringify($quality, $type));
     if (!is_null($this->type)) {
         $response->header('Content-Type', 'image/' . $this->type);
     }
     return $response;
 }
Example #10
0
 /**
  * action with param
  */
 public function action_param($p1)
 {
     return \CCResponse::create('Test ' . $p1);
 }
Example #11
0
 /**
  * Returns an test CCResposne
  */
 public function test_response()
 {
     return CCResponse::create('Callbacks are pretty cool');
 }
    }
    if ($queries > 500) {
        $queries = 500;
    }
    $worlds = array();
    for ($i = 0; $i < $queries; ++$i) {
        $world = DB::select('World')->find(mt_rand(1, 10000));
        $world->id = intval($world->id);
        $world->randomNumber = intval($world->randomNumber);
        $worlds[] = $world;
    }
    return CCResponse::create(json_encode($worlds), 200)->header('Content-Type', 'application/json');
}, 'updates' => function () {
    $queries = CCIn::get('queries', 1);
    if ($queries < 1) {
        $queries = 1;
    }
    if ($queries > 500) {
        $queries = 500;
    }
    $worlds = array();
    for ($i = 0; $i < $queries; ++$i) {
        $id = mt_rand(1, 10000);
        DB::update('World')->set('randomNumber', mt_rand(1, 10000))->where('id', $id)->run();
        $world = DB::select('World')->find($id);
        $world->id = intval($world->id);
        $world->randomNumber = intval($world->randomNumber);
        $worlds[] = $world;
    }
    return CCResponse::create(json_encode($worlds), 200)->header('Content-Type', 'application/json');
}, 'fortunes' => 'Bench@fortunes');