TestService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\services;
  3. use App\models\Fortune;
  4. use App\models\World;
  5. use SwFwLess\components\http\Response;
  6. use SwFwLess\services\BaseService;
  7. class TestService extends BaseService
  8. {
  9. public function json()
  10. {
  11. return ['message' => 'Hello, World!'];
  12. }
  13. public function db()
  14. {
  15. $world = World::select()->cols(['*'])->where('`id` = :id')
  16. ->bindValue(':id', random_int(1, 10000))
  17. ->first();
  18. return $world ? $world->toArray() : [];
  19. }
  20. public function queries($queries = 1)
  21. {
  22. $queries = $this->clamp($queries);
  23. $rows = [];
  24. while ($queries--) {
  25. $row = World::select()->cols(['*'])->where('`id` = :id')
  26. ->bindValue(':id', random_int(1, 10000))
  27. ->first();
  28. $rows[] = $row ? $row->toArray() : [];
  29. }
  30. return $rows;
  31. }
  32. public function fortunes()
  33. {
  34. $rows = Fortune::select()->cols(['*'])->get();
  35. $insert = new Fortune();
  36. $insert->id = 0;
  37. $insert->message = 'Additional fortune added at request time.';
  38. $rows[] = $insert;
  39. usort($rows, function ($left, $right) {
  40. return strcmp($left->message, $right->message);
  41. });
  42. return Response::output($this->renderFortunes($rows), 200, ['Content-Type' => 'text/html;charset=utf-8']);
  43. }
  44. private function renderFortunes($fortunes)
  45. {
  46. $html = <<<EOF
  47. <!DOCTYPE html>
  48. <html>
  49. <head><title>Fortunes</title></head>
  50. <body>
  51. <table>
  52. <tr><th>id</th><th>message</th></tr>
  53. %s
  54. </table>
  55. </body>
  56. </html>
  57. EOF;
  58. $fortuneRows = '';
  59. foreach ($fortunes as $fortune) {
  60. $fortuneRows .= ' <tr><td>' . htmlspecialchars($fortune->id) .
  61. '</td><td>' . htmlspecialchars($fortune->message) . '</td></tr>' . PHP_EOL;
  62. }
  63. return sprintf($html, $fortuneRows);
  64. }
  65. public function updates($queries = 1)
  66. {
  67. $queries = $this->clamp($queries);
  68. $rows = [];
  69. while ($queries--) {
  70. $row = World::select()->cols(['*'])->where('`id` = :id')
  71. ->bindValue(':id', random_int(1, 10000))
  72. ->first();
  73. if ($row) {
  74. $row->randomNumber = random_int(1, 10000);
  75. $row->save();
  76. $rows[] = $row->toArray();
  77. } else {
  78. $rows[] = [];
  79. }
  80. }
  81. return $rows;
  82. }
  83. public function plaintext()
  84. {
  85. return Response::output('Hello, World!', 200, ['Content-Type' => 'text/plain']);
  86. }
  87. private function clamp($value): int
  88. {
  89. if (! is_numeric($value) || $value < 1) {
  90. return 1;
  91. }
  92. if ($value > 500) {
  93. return 500;
  94. }
  95. return (int)$value;
  96. }
  97. }