IndexController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://doc.hyperf.io
  8. * @contact [email protected]
  9. * @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
  10. */
  11. namespace App\Controller;
  12. use App\Model\Fortune;
  13. use App\Model\World;
  14. use App\Render;
  15. use Hyperf\DbConnection\Db;
  16. use Hyperf\Di\Annotation\Inject;
  17. use Hyperf\HttpMessage\Stream\SwooleStream;
  18. use Hyperf\HttpServer\Annotation\Controller;
  19. use Hyperf\HttpServer\Annotation\GetMapping;
  20. use Hyperf\HttpServer\Contract\ResponseInterface;
  21. /**
  22. * @Controller
  23. */
  24. class IndexController
  25. {
  26. /**
  27. * @Inject()
  28. * @var Render
  29. */
  30. private $render;
  31. /**
  32. * @Inject()
  33. * @var ResponseInterface
  34. */
  35. private $response;
  36. /**
  37. * @GetMapping(path="/json")
  38. */
  39. public function json()
  40. {
  41. return $this->response->json(['message' => 'Hello, World!']);
  42. }
  43. /**
  44. * @GetMapping(path="/db")
  45. */
  46. public function db()
  47. {
  48. return $this->response->json(World::find(random_int(1, 10000)));
  49. }
  50. /**
  51. * @GetMapping(path="/raw-db")
  52. */
  53. public function rawDb()
  54. {
  55. return $this->response->json(Db::select('SELECT id, randomNumber FROM World WHERE id = ?', [random_int(1, 10000)]));
  56. }
  57. /**
  58. * @GetMapping(path="/queries/[{queries}]")
  59. */
  60. public function queries($queries = 1)
  61. {
  62. $queries = $this->clamp($queries);
  63. $rows = [];
  64. while ($queries--) {
  65. $rows[] = World::find(random_int(1, 10000));
  66. }
  67. return $this->response->json($rows);
  68. }
  69. /**
  70. * @GetMapping(path="/raw-queries/[{queries}]")
  71. */
  72. public function rawQueries($queries = 1)
  73. {
  74. $queries = $this->clamp($queries);
  75. $rows = [];
  76. while ($queries--) {
  77. $rows[] = Db::selectOne('SELECT id, randomNumber FROM World WHERE id = ?', [random_int(1, 10000)]);
  78. }
  79. return $this->response->json($rows);
  80. }
  81. /**
  82. * @GetMapping(path="/fortunes")
  83. */
  84. public function fortunes()
  85. {
  86. $rows = Fortune::all();
  87. $insert = new Fortune();
  88. $insert->id = 0;
  89. $insert->message = 'Additional fortune added at request time.';
  90. $rows->add($insert);
  91. $rows = $rows->sortBy('message');
  92. return $this->render->render('fortunes', ['rows' => $rows]);
  93. }
  94. /**
  95. * @GetMapping(path="/micro-fortunes")
  96. */
  97. public function microFortunes()
  98. {
  99. $rows = Db::select('SELECT id, message FROM Fortune');
  100. $fortune = [];
  101. foreach ($rows ?? [] as $row) {
  102. $fortune[$row->id] = $row->message;
  103. }
  104. $fortune[0] = 'Additional fortune added at request time.';
  105. asort($fortune);
  106. $html = '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>';
  107. foreach ($fortune as $id => $message) {
  108. $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  109. $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
  110. }
  111. $html .= '</table></body></html>';
  112. return $this->response->withAddedHeader('content-type', 'text/html; charset=utf-8')->withBody(new SwooleStream($html));
  113. }
  114. /**
  115. * @GetMapping(path="/updates/[{queries}]")
  116. */
  117. public function updates($queries = 1)
  118. {
  119. $queries = $this->clamp($queries);
  120. $rows = [];
  121. while ($queries--) {
  122. $row = World::find(random_int(1, 10000));
  123. $row->randomNumber = random_int(1, 10000);
  124. $row->save();
  125. $rows[] = $row;
  126. }
  127. return $this->response->json($rows);
  128. }
  129. /**
  130. * @GetMapping(path="/raw-updates/[{queries}]")
  131. */
  132. public function rawUpdates($queries = 1)
  133. {
  134. $queries = $this->clamp($queries);
  135. $rows = [];
  136. while ($queries--) {
  137. $row = Db::selectOne('SELECT id, randomNumber FROM World WHERE id = ?', [$id = random_int(1, 10000)]);
  138. $rand = random_int(1, 10000);
  139. $row->randomNumber = $rand;
  140. Db::update('UPDATE World SET randomNumber = ? WHERE id = ?', [$rand, $id]);
  141. $rows[] = $row;
  142. }
  143. return $this->response->json($rows);
  144. }
  145. /**
  146. * @GetMapping(path="/plaintext")
  147. */
  148. public function plaintext()
  149. {
  150. return $this->response->raw('Hello, World!');
  151. }
  152. private function clamp($value): int
  153. {
  154. if (! is_numeric($value) || $value < 1) {
  155. return 1;
  156. }
  157. if ($value > 500) {
  158. return 500;
  159. }
  160. return (int)$value;
  161. }
  162. }