BenchController.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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($queries)
  18. {
  19. $worlds = $arr = array();
  20. $world = null;
  21. if ($queries < 1 || is_null($queries)) {
  22. $queries = 1;
  23. } elseif ($queries > 500) {
  24. $queries = 500;
  25. }
  26. for ($i = 0; $i < $queries; ++$i) {
  27. $world = World::find(mt_rand(1, 10000));
  28. $arr['id'] = (int) $world->id;
  29. $arr['randomNumber'] = (int) $world->randomnumber;
  30. $worlds[] = $arr;
  31. }
  32. header('Content-type: application/json');
  33. echo json_encode($worlds);
  34. }
  35. public function fortunesAction()
  36. {
  37. $fortuneCollection = array();
  38. $fortuneCollection = Fortune::all();
  39. $fortunes = $fortuneCollection->asArray();
  40. $runtimeFortune = new Fortune();
  41. $runtimeFortune->id = 0;
  42. $runtimeFortune->message = 'Additional fortune added at request time.';
  43. $fortunes[] = $runtimeFortune;
  44. usort($fortunes, function($left, $right) {
  45. if ($left->message === $right->message) {
  46. return 0;
  47. } else if ($left->message > $right->message) {
  48. return 1;
  49. } else {
  50. return -1;
  51. }
  52. });
  53. $this->render('fortunes', array(
  54. 'fortunes' => $fortunes
  55. ));
  56. }
  57. }