Index.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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::$statement;
  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::$statement;
  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. $update = Db::$update;
  71. $query_count = 1;
  72. if ((int) $q > 1) {
  73. $query_count = \min($q, 500);
  74. }
  75. $arr = [];
  76. while ($query_count--) {
  77. $id = \mt_rand(1, 10000);
  78. $random->execute([$id]);
  79. //$random->fetchColumn(); //
  80. //$world = ['id' => $id, 'randomNumber' => \mt_rand(1, 10000)]; //
  81. $world = ['id' => $id, 'randomNumber' => $random->fetchColumn()];
  82. $update->execute(
  83. [$world['randomNumber'] = mt_rand(1, 10000), $id]
  84. );
  85. $arr[] = $world;
  86. }
  87. /*$pdo = Db::$pdo;
  88. $pdo->beginTransaction();
  89. foreach($arr as $world) {
  90. $update->execute([$world['randomNumber'], $world['id']]);
  91. }
  92. $pdo->commit();*/
  93. return new Response(200, [
  94. 'Content-Type' => 'application/json',
  95. 'Date' => Date::$date
  96. ], \json_encode($arr));
  97. }
  98. }