Bench.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php defined('SYSPATH') OR die('No Direct Script Access');
  2. Class Controller_Bench extends Controller
  3. {
  4. public function action_json()
  5. {
  6. $this->response
  7. ->headers(array('Content-Type' => 'application/json'))
  8. ->body(json_encode(array('message' => 'Hello, World!')));
  9. }
  10. public function action_db()
  11. {
  12. $queries = $this->request->param('queries', false);
  13. $queries = $queries
  14. ? $queries
  15. : 1;
  16. $worlds = array();
  17. $query = DB::query(Database::SELECT, 'SELECT * FROM World WHERE id = :id')->bind(':id', $id);
  18. for ($i = 0; $i < $queries; $i++) {
  19. $worlds[] = $query->param(':id', mt_rand(1, 10000))->execute()->current();
  20. }
  21. $this->response
  22. ->headers(array('Content-Type' => 'application/json'))
  23. ->body(json_encode($worlds));
  24. }
  25. public function action_fortunes()
  26. {
  27. $fortunes = DB::select()->from('Fortune')
  28. ->execute()
  29. ->as_array();
  30. $fortunes[] = array(
  31. 'id' => 0,
  32. 'message' => 'Additional fortune added at request time.'
  33. );
  34. usort($fortunes, function($left, $right) {
  35. if ($left['message'] === $right['message']) {
  36. return 0;
  37. } else if ($left['message'] > $right['message']) {
  38. return 1;
  39. } else {
  40. return -1;
  41. }
  42. });
  43. $this->response->body(
  44. View::factory('bench/fortunes')
  45. ->bind('fortunes', $fortunes)
  46. ->render()
  47. );
  48. }
  49. }