BenchController.php 1.8 KB

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