Index.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\controller;
  3. use support\Request;
  4. use support\bootstrap\Date;
  5. use support\bootstrap\db\Raw as Db;
  6. use support\Response;
  7. use PDO;
  8. class Index
  9. {
  10. public function plaintext($request)
  11. {
  12. return new Response(200, [
  13. 'Content-Type' => 'text/plain',
  14. 'Date' => Date::$date
  15. ], 'Hello, World!');
  16. }
  17. public function json($request)
  18. {
  19. return new Response(200, [
  20. 'Content-Type' => 'application/json',
  21. 'Date' => Date::$date
  22. ], json_encode(['message' => 'Hello, World!']));
  23. }
  24. public function db($request)
  25. {
  26. $statement = Db::$random;
  27. $statement->execute([\mt_rand(1, 10000)]);
  28. return new Response(200, [
  29. 'Content-Type' => 'application/json',
  30. 'Date' => Date::$date
  31. ], json_encode($statement->fetch()));
  32. }
  33. public function fortunes($request)
  34. {
  35. $fortune = Db::$fortune;
  36. $fortune->execute();
  37. $arr = $fortune->fetchAll(\PDO::FETCH_KEY_PAIR);
  38. $arr[0] = 'Additional fortune added at request time.';
  39. \asort($arr);
  40. $html = '';
  41. foreach ($arr as $id => $message) {
  42. $message = \htmlspecialchars($message, \ENT_QUOTES, 'UTF-8');
  43. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  44. }
  45. return new Response(200, [
  46. 'Date' => Date::$date
  47. ], "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>"
  48. );
  49. }
  50. public function queries($request, $q = 1)
  51. {
  52. $statement = Db::$random;
  53. $query_count = 1;
  54. if ((int) $q > 1) {
  55. $query_count = \min($q, 500);
  56. }
  57. $arr = [];
  58. while ($query_count--) {
  59. $statement->execute([\mt_rand(1, 10000)]);
  60. $arr[] = $statement->fetch();
  61. }
  62. return new Response(200, [
  63. 'Content-Type' => 'application/json',
  64. 'Date' => Date::$date
  65. ], json_encode($arr));
  66. }
  67. public function updates($request, $q = 1)
  68. {
  69. $random = Db::$random;
  70. $query_count = 1;
  71. if ((int) $q > 1) {
  72. $query_count = \min($q, 500);
  73. }
  74. $worlds = [];
  75. while ($query_count--) {
  76. $random->execute([\mt_rand(1, 10000)]);
  77. $world = $random->fetch();
  78. $world['randomNumber'] = \mt_rand(1, 10000);
  79. $worlds[] = $world;
  80. }
  81. Db::update($worlds);
  82. return new Response(200, [
  83. 'Content-Type' => 'application/json',
  84. 'Date' => Date::$date
  85. ], \json_encode($worlds));
  86. }
  87. }