BenchController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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->setContentType('text/plain');
  54. $this->response->setContent("Hello, World!");
  55. $this->response->send();
  56. }
  57. protected function getRandomWorld()
  58. {
  59. return Worlds::findFirst(mt_rand(1, 10000));
  60. }
  61. protected function getFortunesArray()
  62. {
  63. // since the resultset is immutable get an array instead
  64. // so we can add the new fortune
  65. return Fortunes::find()->toArray();
  66. }
  67. protected function buildFortune()
  68. {
  69. return array(
  70. 'id' => 0,
  71. 'message' => 'Additional fortune added at request time.'
  72. );
  73. }
  74. protected function sortFortunes($fortunes)
  75. {
  76. usort($fortunes,
  77. function($left, $right) {
  78. $l = $left['message'];
  79. $r = $right['message'];
  80. if ($l === $r) {
  81. return 0;
  82. } else {
  83. if ($l > $r) {
  84. return 1;
  85. } else {
  86. return -1;
  87. }
  88. }
  89. });
  90. return $fortunes;
  91. }
  92. private function sendContentAsJson($content)
  93. {
  94. $response = new Phalcon\Http\Response(json_encode($content));
  95. $response->setHeader("Content-Type", "application/json");
  96. return $response;
  97. }
  98. }