TestController.php 3.3 KB

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