Raw.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. use Yaf\Controller_Abstract as AbstractController;
  3. class RawController extends AbstractController
  4. {
  5. public function plaintextAction ()
  6. {
  7. header('Content-Type: text/plain');
  8. die("Hello, World!");
  9. }
  10. public function jsonAction ()
  11. {
  12. header('Content-type: application/json');
  13. die(json_encode(array('message' => 'Hello, World!')));
  14. }
  15. public function dbAction ()
  16. {
  17. $dbh = DatabaseManager::getInstance()->getConnection();
  18. $query_count = (int) $this->getRequest()->get('queries', 1);
  19. if (0 >= $query_count) {
  20. $query_count = 1;
  21. } elseif (500 < $query_count) {
  22. $query_count = 500;
  23. }
  24. $arr = array();
  25. $id = mt_rand(1, 10000);
  26. $statement = $dbh->prepare('SELECT `randomNumber` FROM `World` WHERE `id` = :id');
  27. $statement->bindParam(':id', $id, \PDO::PARAM_INT);
  28. while (0 < $query_count--) {
  29. $statement->execute();
  30. $arr[] = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
  31. $id = mt_rand(1, 10000);
  32. }
  33. if (count($arr) == 1) {
  34. $arr = $arr[0];
  35. }
  36. header('Content-type: application/json');
  37. die(json_encode($arr));
  38. }
  39. public function updatesAction ()
  40. {
  41. $dbh = DatabaseManager::getInstance()->getConnection();
  42. $query_count = (int) $this->getRequest()->get('queries', 1);
  43. if (0 >= $query_count) {
  44. $query_count = 1;
  45. } elseif (500 < $query_count) {
  46. $query_count = 500;
  47. }
  48. $arr = array();
  49. $id = mt_rand(1, 10000);
  50. $random_number = mt_rand(1, 10000);
  51. $statement = $dbh->prepare('UPDATE `World` SET `randomNumber` = :random_number WHERE `id` = :id');
  52. $statement->bindParam(':id', $id, \PDO::PARAM_INT);
  53. $statement->bindParam(':random_number', $random_number, \PDO::PARAM_INT);
  54. while (0 < $query_count--) {
  55. $statement->execute();
  56. $arr[] = array('id' => $id, 'randomNumber' => $random_number);
  57. $random_number = mt_rand(1, 10000);
  58. $id = mt_rand(1, 10000);
  59. }
  60. header('Content-type: application/json');
  61. die(json_encode($arr));
  62. }
  63. public function fortunesAction ()
  64. {
  65. $view = $this->getView();
  66. $dbh = DatabaseManager::getInstance()->getConnection();
  67. $statement = $dbh->query('SELECT `id`, `message` FROM `Fortune`');
  68. $arr = $statement->fetchAll(\PDO::FETCH_KEY_PAIR);
  69. $arr[0] = 'Additional fortune added at request time.';
  70. asort($arr);
  71. $view->rows = $arr;
  72. header('Content-Type: text/html; charset=utf-8');
  73. }
  74. }