Bench.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. if ($queries == 1) {
  22. $worlds = $worlds[0];
  23. }
  24. $this->response
  25. ->headers(array('Content-Type' => 'application/json'))
  26. ->body(json_encode($worlds));
  27. }
  28. public function action_fortunes()
  29. {
  30. $fortunes = DB::select()->from('Fortune')
  31. ->execute()
  32. ->as_array();
  33. $fortunes[] = array(
  34. 'id' => 0,
  35. 'message' => 'Additional fortune added at request time.'
  36. );
  37. usort($fortunes, function($left, $right) {
  38. if ($left['message'] === $right['message']) {
  39. return 0;
  40. } else if ($left['message'] > $right['message']) {
  41. return 1;
  42. } else {
  43. return -1;
  44. }
  45. });
  46. $this->response->body(
  47. View::factory('bench/fortunes')
  48. ->bind('fortunes', $fortunes)
  49. ->render()
  50. );
  51. }
  52. }