TestController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /** @package HELLO WORLD::Controller */
  3. /** import supporting libraries */
  4. require_once("verysimple/Phreeze/Controller.php");
  5. /**
  6. *
  7. * @package HELLO WORLD::Controller
  8. * @author ClassBuilder
  9. * @version 1.0
  10. */
  11. class TestController extends Controller
  12. {
  13. /**
  14. * Not used but necessary to implement Controller
  15. * @see Controller::Init()
  16. */
  17. protected function Init()
  18. {
  19. }
  20. /**
  21. * Test route that outputs a simple JSON object
  22. */
  23. public function JSON()
  24. {
  25. $arr = array(
  26. "message" => "Hello, World!"
  27. );
  28. $this->RenderJSON($arr);
  29. }
  30. /**
  31. * Test route that connects to the database and outputs
  32. * the number of rows specified in the querystring argument "queries"
  33. */
  34. public function DB()
  35. {
  36. require_once("Model/World.php");
  37. // Read number of queries to run from URL parameter
  38. $query_count = RequestUtil::Get('queries',1);
  39. $arr = array();
  40. for ($i = 0; $i < $query_count; $i++) {
  41. $id = mt_rand(1, 10000);
  42. $world = $this->Phreezer->Get("World",$id);
  43. // convert the Phreezable object into a simple structure for output
  44. $arr[] = array('id'=>$world->Id,'randomNumber'=>$world->Randomnumber);
  45. }
  46. $this->RenderJSON($arr);
  47. }
  48. /**
  49. * Output the Fortunes test template
  50. */
  51. public function Fortunes()
  52. {
  53. require_once("Model/Fortune.php");
  54. require_once("verysimple/Phreeze/PHPRenderEngine.php");
  55. // charset must be set to UTF8 to support multi-byte chars
  56. $this->Phreezer->DataAdapter->ConnectionSetting->Charset = "utf8";
  57. // obtain fortunes without using 'order by'
  58. $fortunes = $this->Phreezer->Query('Fortune')->ToObjectArray();
  59. // dynamically add a new, non-persisted Fortune object
  60. $newFortune = new Fortune($this->Phreezer);
  61. $newFortune->Id = 0;
  62. $newFortune->Message = 'Additional fortune added at request time.';
  63. $fortunes[] = $newFortune;
  64. // sort (will use Fortune->ToString)
  65. Phreezer::Sort($fortunes);
  66. // Render using a template
  67. $this->RenderEngine = new PHPRenderEngine('templates');
  68. $this->Assign('fortunes',$fortunes);
  69. $this->Render('TestFortunes.php');
  70. }
  71. /**
  72. * Test for performing updates
  73. */
  74. public function Updates()
  75. {
  76. require_once("Model/World.php");
  77. // Read number of queries to run from URL parameter
  78. $query_count = RequestUtil::Get('queries',1);
  79. $arr = array();
  80. for ($i = 0; $i < $query_count; $i++) {
  81. $id = mt_rand(1, 10000);
  82. $world = $this->Phreezer->Get("World",$id);
  83. // update the random number and persist the record
  84. $world->Randomnumber = mt_rand(1, 10000);
  85. $world->Save();
  86. // convert the Phreezable object into a simple structure for output
  87. $arr[] = array('id'=>$world->Id,'randomNumber'=>$world->Randomnumber);
  88. }
  89. $this->RenderJSON($arr);
  90. }
  91. /**
  92. * Test for outputting plaintext
  93. */
  94. public function PlainText()
  95. {
  96. header('Content-type: text/plain');
  97. echo 'Hello, World!';
  98. }
  99. }