Hello.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Vanilla\Controller;
  3. use Pimf\Controller\Base, Pimf\View, Pimf\Registry;
  4. class Hello extends Base
  5. {
  6. /**
  7. * A index action - this is a framework restriction!
  8. */
  9. public function indexAction()
  10. {
  11. $this->jsonAction();
  12. }
  13. /**
  14. * Test 1: JSON serialization
  15. */
  16. public function jsonAction()
  17. {
  18. $this->response->asJSON()->send(array("message" => "Hello World!"));
  19. }
  20. /**
  21. * Test 2: Multiple database queries
  22. */
  23. public function queriesAction()
  24. {
  25. $queries = max(1, min($this->request->fromGet()->get('queries', 1), 500));
  26. $worlds = array();
  27. for ($i = 0; $i < (int)$queries; ++$i) {
  28. $worlds[] = Registry::get('em')->world->find(mt_rand(1, 10000));
  29. }
  30. $this->response->asJSON()->send($worlds);
  31. }
  32. /**
  33. * Test 3: Single database query
  34. */
  35. public function dbAction()
  36. {
  37. $worlds = Registry::get('em')->world->find(1);
  38. $this->response->asJSON()->send($worlds);
  39. }
  40. /**
  41. * Test 4: Fortunes
  42. */
  43. public function fortunesAction()
  44. {
  45. $templates = array();
  46. $fortunes = Registry::get('em')->fortune->getAll();
  47. $fortunes[] = 'Additional fortune added at request time.';
  48. asort($fortunes);
  49. foreach ($fortunes as $i => $fortune) {
  50. $templates[$i] = '<tr><td>'.$i.'</td><td>'.$fortune.'</td></tr>';
  51. }
  52. $this->response->asHTML()->send(
  53. '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'.implode('', $templates).'</table></body></html>'
  54. );
  55. }
  56. /**
  57. * Test 5: Database updates
  58. */
  59. public function updatesAction()
  60. {
  61. $queries = max(1, min($this->request->fromGet()->get('queries', 1), 500));
  62. $worlds = array();
  63. /* @var $em \Pimf\EntityManager */
  64. $em = Registry::get('em');
  65. $em->beginTransaction();
  66. for ($i = 0; $i < (int)$queries; ++$i) {
  67. $worlds[] = $em->world->find(mt_rand(1, 10000));
  68. }
  69. foreach ($worlds as $i => $row) {
  70. $row[$i]['randomNumber'] = rand(1, 10000);
  71. $em->world->update($row);
  72. }
  73. $em->commitTransaction();
  74. $this->response->asJSON()->send($worlds);
  75. }
  76. /**
  77. * Test 6: Plaintext
  78. */
  79. public function plaintextAction()
  80. {
  81. $this->response->asTEXT()->send('Hello World!');
  82. }
  83. }