BenchController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. use Phalcon\Mvc\View,
  3. Phalcon\Mvc\Model\Resultset;
  4. class BenchController extends \Phalcon\Mvc\Controller
  5. {
  6. public function initialize()
  7. {
  8. // views must be renderd explicitly. safes processing time when not needed.
  9. $this->view->setRenderLevel(View::LEVEL_LAYOUT);
  10. }
  11. public function jsonAction()
  12. {
  13. return $this->sendContentAsJson(array(
  14. 'message' => 'Hello, World!'
  15. ));
  16. }
  17. public function dbAction()
  18. {
  19. $queries = $this->request->getQuery('queries', null, 1);
  20. $worlds = array();
  21. for ($i = 0; $i < $queries; ++$i) {
  22. $worlds[] = $this->getRandomWorld();
  23. }
  24. if (count($worlds) == 1) {
  25. return $this->sendContentAsJson($worlds[0]);
  26. }
  27. else {
  28. return $this->sendContentAsJson($worlds);
  29. }
  30. }
  31. public function fortunesAction()
  32. {
  33. $fortunes = $this->getFortunesArray();
  34. $fortunes[] = $this->buildFortune();
  35. $this->response->setHeader("Content-Type", "text/html; charset=utf-8");
  36. $this->view->fortunes = $this->sortFortunes($fortunes);
  37. }
  38. public function updateAction()
  39. {
  40. $queries = $this->request->getQuery('queries', null, 1);
  41. $queries = max(1, min(500, $queries));
  42. $worlds = array();
  43. for ($i = 0; $i < $queries; ++$i) {
  44. $world = $this->getRandomWorld();
  45. $world->randomNumber = mt_rand(1, 10000);
  46. $world->save();
  47. $worlds[] = $world;
  48. }
  49. return $this->sendContentAsJson($worlds);
  50. }
  51. public function plaintextAction()
  52. {
  53. $this->view->disable();
  54. $this->response->setStatusCode(200, "OK");
  55. $this->response->setContentType('text/plain', 'UTF-8');
  56. $this->response->setContent("Hello, World!");
  57. $this->response->send();
  58. }
  59. protected function getRandomWorld()
  60. {
  61. return Worlds::findFirst(mt_rand(1, 10000));
  62. }
  63. protected function getFortunesArray()
  64. {
  65. // since the resultset is immutable get an array instead
  66. // so we can add the new fortune
  67. return Fortunes::find()->toArray();
  68. }
  69. protected function buildFortune()
  70. {
  71. return array(
  72. 'id' => 0,
  73. 'message' => 'Additional fortune added at request time.'
  74. );
  75. }
  76. protected function sortFortunes($fortunes)
  77. {
  78. usort($fortunes,
  79. function($left, $right) {
  80. $l = $left['message'];
  81. $r = $right['message'];
  82. if ($l === $r) {
  83. return 0;
  84. } else {
  85. if ($l > $r) {
  86. return 1;
  87. } else {
  88. return -1;
  89. }
  90. }
  91. });
  92. return $fortunes;
  93. }
  94. private function sendContentAsJson($content)
  95. {
  96. $response = new Phalcon\Http\Response(json_encode($content));
  97. $response->setHeader("Content-Type", "application/json");
  98. return $response;
  99. }
  100. }