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. return $this->sendContentAsJson($this->getRandomWorld());
  20. }
  21. public function queriesAction()
  22. {
  23. $queries = min(500, max(1, $this->filter->sanitize($this->request->getQuery('queries', null, 1), "int")));
  24. $worlds = array();
  25. for ($i = 0; $i < $queries; ++$i) {
  26. $worlds[] = $this->getRandomWorld();
  27. }
  28. return $this->sendContentAsJson($worlds);
  29. }
  30. public function fortunesAction()
  31. {
  32. $fortunes = $this->getFortunesArray();
  33. $fortunes[] = $this->buildFortune();
  34. $this->response->setHeader("Content-Type", "text/html; charset=utf-8");
  35. $this->view->fortunes = $this->sortFortunes($fortunes);
  36. }
  37. public function updateAction()
  38. {
  39. $queries = $this->request->getQuery('queries', null, 1);
  40. $queries = max(1, min(500, $queries));
  41. $worlds = array();
  42. for ($i = 0; $i < $queries; ++$i) {
  43. $world = $this->getRandomWorld();
  44. $world->randomNumber = mt_rand(1, 10000);
  45. $world->save();
  46. $worlds[] = $world;
  47. }
  48. return $this->sendContentAsJson($worlds);
  49. }
  50. public function plaintextAction()
  51. {
  52. $this->view->disable();
  53. $this->response->setStatusCode(200, "OK");
  54. $this->response->setContentType('text/plain', 'UTF-8');
  55. $this->response->setContent("Hello, World!");
  56. $this->response->send();
  57. }
  58. protected function getRandomWorld()
  59. {
  60. return Worlds::findFirst(mt_rand(1, 10000));
  61. }
  62. protected function getFortunesArray()
  63. {
  64. // since the resultset is immutable get an array instead
  65. // so we can add the new fortune
  66. return Fortunes::find()->toArray();
  67. }
  68. protected function buildFortune()
  69. {
  70. return array(
  71. 'id' => 0,
  72. 'message' => 'Additional fortune added at request time.'
  73. );
  74. }
  75. protected function sortFortunes($fortunes)
  76. {
  77. usort($fortunes,
  78. function($left, $right) {
  79. $l = $left['message'];
  80. $r = $right['message'];
  81. if ($l === $r) {
  82. return 0;
  83. } else {
  84. if ($l > $r) {
  85. return 1;
  86. } else {
  87. return -1;
  88. }
  89. }
  90. });
  91. return $fortunes;
  92. }
  93. private function sendContentAsJson($content)
  94. {
  95. $response = new Phalcon\Http\Response(json_encode($content));
  96. $response->setHeader("Content-Type", "application/json");
  97. return $response;
  98. }
  99. }