TestController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. $id = mt_rand(1, 10000);
  38. $world = $this->Phreezer->Get("World",$id);
  39. $this->RenderJSON($world,'',true);
  40. }
  41. /**
  42. * Test route that connects to the database and outputs
  43. * the number of rows specified in the querystring argument "queries"
  44. */
  45. public function Query()
  46. {
  47. require_once("Model/World.php");
  48. // Read number of queries to run from URL parameter
  49. $query_count = RequestUtil::Get('queries',1);
  50. $query_count = is_numeric($query_count) ? min(max($query_count, 1), 500) : 1;
  51. // make sure the query count paramter is in range
  52. if (!is_numeric($query_count)) {
  53. $query_count = 1;
  54. }
  55. else {
  56. $query_count = max(1,min($query_count,500));
  57. }
  58. $arr = array();
  59. for ($i = 0; $i < $query_count; $i++) {
  60. $id = mt_rand(1, 10000);
  61. // convert the Phreezable object into a simple structure for output
  62. $arr[] = $this->Phreezer->Get("World",$id)->ToObject();
  63. }
  64. $this->RenderJSON($arr);
  65. }
  66. /**
  67. * Output the Fortunes test template
  68. */
  69. public function Fortunes()
  70. {
  71. require_once("Model/Fortune.php");
  72. require_once("verysimple/Phreeze/PHPRenderEngine.php");
  73. // charset must be set to UTF8 to support multi-byte chars
  74. $this->Phreezer->DataAdapter->ConnectionSetting->Charset = "utf8";
  75. // obtain fortunes without using 'order by'
  76. $fortunes = $this->Phreezer->Query('Fortune')->ToObjectArray();
  77. // dynamically add a new, non-persisted Fortune object
  78. $newFortune = new Fortune($this->Phreezer);
  79. $newFortune->Id = 0;
  80. $newFortune->Message = 'Additional fortune added at request time.';
  81. $fortunes[] = $newFortune;
  82. // sort (will use Fortune->ToString)
  83. Phreezer::Sort($fortunes);
  84. // Render using a template
  85. $this->RenderEngine = new PHPRenderEngine('templates');
  86. $this->Assign('fortunes',$fortunes);
  87. $this->Render('TestFortunes.php');
  88. }
  89. /**
  90. * Test for performing updates
  91. */
  92. public function Updates()
  93. {
  94. require_once("Model/World.php");
  95. // Read number of queries to run from URL parameter
  96. $query_count = RequestUtil::Get('queries',1);
  97. $query_count = is_numeric($query_count) ? min(max($query_count, 1), 500) : 1;
  98. $arr = array();
  99. for ($i = 0; $i < $query_count; $i++) {
  100. $id = mt_rand(1, 10000);
  101. $world = $this->Phreezer->Get("World",$id);
  102. // update the random number and persist the record
  103. $world->Randomnumber = mt_rand(1, 10000);
  104. $world->Save();
  105. // convert the Phreezable object into a simple structure for output
  106. $arr[] = array('id'=>$world->Id,'randomNumber'=>$world->Randomnumber);
  107. }
  108. $this->RenderJSON($arr);
  109. }
  110. /**
  111. * Test for outputting plaintext
  112. */
  113. public function PlainText()
  114. {
  115. header('Content-type: text/plain');
  116. echo 'Hello, World!';
  117. }
  118. }