Index.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 function json_encode;
  8. use function max;
  9. use function min;
  10. use function mt_rand;
  11. class Index
  12. {
  13. public function plaintext()
  14. {
  15. return new Response(200, [
  16. 'Content-Type' => 'text/plain',
  17. 'Date' => Date::$date
  18. ], 'Hello, World!');
  19. }
  20. public function json()
  21. {
  22. return new Response(200, [
  23. 'Content-Type' => 'application/json',
  24. 'Date' => Date::$date
  25. ], json_encode(['message' => 'Hello, World!']));
  26. }
  27. public function db()
  28. {
  29. $statement = Db::$random;
  30. $statement->execute([mt_rand(1, 10000)]);
  31. return new Response(200, [
  32. 'Content-Type' => 'application/json',
  33. 'Date' => Date::$date
  34. ], json_encode($statement->fetch()));
  35. }
  36. public function fortunes()
  37. {
  38. $fortune = Db::$fortune;
  39. $fortune->execute();
  40. $arr = $fortune->fetchAll(\PDO::FETCH_KEY_PAIR);
  41. $arr[0] = 'Additional fortune added at request time.';
  42. \asort($arr);
  43. $html = '';
  44. foreach ($arr as $id => $message) {
  45. $message = \htmlspecialchars($message, \ENT_QUOTES, 'UTF-8');
  46. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  47. }
  48. return new Response(200, [
  49. 'Date' => Date::$date
  50. ], "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>"
  51. );
  52. }
  53. public function queries(Request $request, $q = 1)
  54. {
  55. $statement = Db::$random;
  56. $query_count = min(max((int) $q, 1), 500);
  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 $request, $q = 1)
  68. {
  69. static $updates = [];
  70. $random = Db::$random;
  71. $pdo = Db::$pdo;
  72. $count = min(max((int) $q, 1), 500);
  73. $worlds = $keys = $values = [];
  74. for ($i = 0; $i < $count; ++ $i) {
  75. $values[] = $keys[] = $id = mt_rand(1, 10000);
  76. $random->execute([$id]);
  77. $row = $random->fetch();
  78. $values[] = $row['randomNumber'] = mt_rand(1, 10000);
  79. $worlds[] = $row;
  80. }
  81. if (!isset($updates[$count])) {
  82. $sql = 'UPDATE World SET randomNumber = CASE id' . str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $count) . 'END WHERE id IN (' . str_repeat('?::INTEGER,', $count - 1) . '?::INTEGER)';
  83. $updates[$count] = $pdo->prepare($sql);
  84. }
  85. $updates[$count]->execute([...$values, ...$keys]);
  86. return new Response(200, [
  87. 'Content-Type' => 'application/json',
  88. 'Date' => Date::$date
  89. ], json_encode($worlds));
  90. }
  91. }