Index.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\controller;
  3. use Cyber\Response;
  4. class Index {
  5. public function json()
  6. {
  7. return Response::json(['message' => 'Hello, World!']);
  8. }
  9. public function plaintext()
  10. {
  11. return Response::text('Hello, World!');
  12. }
  13. public function db()
  14. {
  15. $prepare = app()->dbWorld;
  16. $prepare->execute([mt_rand(1, 10000)]);
  17. $data = $prepare->fetch();
  18. return Response::json($data);
  19. }
  20. public function fortunes()
  21. {
  22. $fortune = app()->dbFortune;
  23. $fortune->execute();
  24. $arr = $fortune->fetchAll(\PDO::FETCH_KEY_PAIR);
  25. $arr[0] = 'Additional fortune added at request time.';
  26. \asort($arr);
  27. $html = '';
  28. foreach ($arr as $id => $message) {
  29. $message = \htmlspecialchars($message, \ENT_QUOTES, 'UTF-8');
  30. $html .= "<tr><td>$id</td><td>$message</td></tr>";
  31. }
  32. return Response::html("<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>");
  33. }
  34. public function queries($q=1)
  35. {
  36. $statement = app()->dbWorld;
  37. $query_count = max(min(intval($q), 500), 1);
  38. $arr = [];
  39. while ($query_count--) {
  40. $statement->execute([mt_rand(1, 10000)]);
  41. $arr[] = $statement->fetch();
  42. }
  43. return Response::json($arr);
  44. }
  45. public function updates($q=1)
  46. {
  47. static $updates = [];
  48. $random = app()->dbWorld;
  49. $count = max(min(intval($q), 500), 1);
  50. $worlds = $keys = $values = [];
  51. for ($i = 0; $i < $count; ++ $i) {
  52. $values[] = $keys[] = $id = mt_rand(1, 10000);
  53. $random->execute([$id]);
  54. $row = $random->fetch();
  55. $values[] = $row['randomNumber'] = mt_rand(1, 10000);
  56. $worlds[] = $row;
  57. }
  58. if (!isset($updates[$count])) {
  59. $sql = 'UPDATE World SET randomNumber = CASE id' . str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $count) . 'END WHERE id IN (' . str_repeat('?::INTEGER,', $count - 1) . '?::INTEGER)';
  60. $updates[$count] = app()->db->prepare($sql);
  61. }
  62. $updates[$count]->execute([...$values, ...$keys]);
  63. return Response::json($worlds);
  64. }
  65. }