BenchController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace Apps\Controllers;
  3. use Cygnite\Mvc\Controller\AbstractBaseController;
  4. use Apps\Models\Fortune;
  5. use Apps\Models\World;
  6. class BenchController extends AbstractBaseController
  7. {
  8. protected $templateEngine = false;
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function indexAction()
  14. {
  15. echo 'Hello World!';
  16. }
  17. public function dbAction()
  18. {
  19. $worlds = $arr = array();
  20. $world = null;
  21. $queries = 1;
  22. $worlds = $this->getWorldsInfo($queries);
  23. $worlds = $worlds[0];
  24. header('Content-type: application/json');
  25. echo json_encode($worlds);
  26. }
  27. private function getWorldsInfo($queries)
  28. {
  29. $world = null;
  30. $worlds = $arr = array();
  31. for ($i = 0; $i < $queries; ++$i) {
  32. $world = World::find(mt_rand(1, 10000));
  33. $arr['id'] = (int) $world->id;
  34. $arr['randomNumber'] = (int) $world->randomnumber;
  35. $worlds[] = $arr;
  36. }
  37. return $worlds;
  38. }
  39. public function queriesAction($queries = 1)
  40. {
  41. $queries = intval($queries);
  42. if ($queries < 1 ) {
  43. $queries = 1;
  44. } elseif ($queries > 500) {
  45. $queries = 500;
  46. }
  47. $worlds = array();
  48. $worlds = $this->getWorldsInfo($queries);
  49. header('Content-type: application/json');
  50. echo json_encode($worlds);
  51. }
  52. public function fortunesAction()
  53. {
  54. $fortuneCollection = array();
  55. $fortuneCollection = Fortune::all();
  56. $fortunes = $fortuneCollection->asArray();
  57. $runtimeFortune = new Fortune();
  58. $runtimeFortune->id = 0;
  59. $runtimeFortune->message = 'Additional fortune added at request time.';
  60. $fortunes[] = $runtimeFortune;
  61. usort($fortunes, function($left, $right) {
  62. if ($left->message === $right->message) {
  63. return 0;
  64. } else if ($left->message > $right->message) {
  65. return 1;
  66. } else {
  67. return -1;
  68. }
  69. });
  70. header('Content-Type: text/html; charset=utf-8');
  71. $this->render('fortunes', array(
  72. 'fortunes' => $fortunes
  73. ));
  74. }
  75. }