Bench.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. $query = DB::query(Database::SELECT, 'SELECT * FROM World WHERE id = :id')->bind(':id', $id);
  13. $world = $query->param(':id', mt_rand(1, 10000))->execute()->current();
  14. $this->response
  15. ->headers(array('Content-Type' => 'application/json'))
  16. ->body(json_encode($world));
  17. }
  18. public function action_queries()
  19. {
  20. $queries = $this->request->param('queries', false);
  21. $queries = is_numeric($queries) ? min(max($queries, 1), 500) : 1;
  22. $worlds = array();
  23. $query = DB::query(Database::SELECT, 'SELECT * FROM World WHERE id = :id')->bind(':id', $id);
  24. for ($i = 0; $i < $queries; $i++) {
  25. $worlds[] = $query->param(':id', mt_rand(1, 10000))->execute()->current();
  26. }
  27. $this->response
  28. ->headers(array('Content-Type' => 'application/json'))
  29. ->body(json_encode($worlds));
  30. }
  31. public function action_fortunes()
  32. {
  33. $fortunes = DB::select()->from('Fortune')
  34. ->execute()
  35. ->as_array();
  36. $fortunes[] = array(
  37. 'id' => 0,
  38. 'message' => 'Additional fortune added at request time.'
  39. );
  40. usort($fortunes, function($left, $right) {
  41. if ($left['message'] === $right['message']) {
  42. return 0;
  43. } else if ($left['message'] > $right['message']) {
  44. return 1;
  45. } else {
  46. return -1;
  47. }
  48. });
  49. $this->response->body(
  50. View::factory('bench/fortunes')
  51. ->bind('fortunes', $fortunes)
  52. ->render()
  53. );
  54. }
  55. }