SiteController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\helpers\Html;
  5. use yii\web\Controller;
  6. class SiteController extends Controller
  7. {
  8. private function resJson($data) {
  9. header('Content-type: application/json');
  10. echo json_encode($data);
  11. }
  12. public function actionJson() {
  13. return $this->resJson(array('message'=>'Hello, World!'));
  14. }
  15. public function actionDb($queries = 1) {
  16. // Set up for Test
  17. // $cmd = Yii::$app->db->createCommand('insert into World (randomNumber) values (:v)');
  18. // for($i = 1; $i <=10000 ; $i ++ ) {
  19. // $cmd->bindValue(':v',mt_rand(1, 10000))->execute();
  20. // }
  21. $statement = Yii::$app->db->createCommand('select id,randomNumber from World where id = :id');
  22. if ($queries == 1) {
  23. $arr = $statement->bindValue(':id',mt_rand(1, 10000))->queryOne();
  24. } else {
  25. if ($queries > 500) $queries = 500;
  26. elseif ($queries <= 0 ) $queries = 1;
  27. // Create an array with the response string.
  28. $arr = array();
  29. // For each query, store the result set values in the response array
  30. while (0 < $queries--) {
  31. // Store result in array.
  32. $arr[] = $statement->bindValue(':id',mt_rand(1, 10000))->queryOne();
  33. }
  34. }
  35. return $this->resJson($arr);
  36. }
  37. public function actionFortunes() {
  38. // Test Data
  39. // $arr = [
  40. // 11=>'<script>alert("This should not be displayed in a browser alert box");</script>',
  41. // 4=>'A bad random number generator: 1, 1, 1, 1, 1, 4.33e+67, 1, 1, 1',
  42. // 5=>'A computer program does what you tell it to do, not what you want it to do.',
  43. // 2=>'A computer scientist is someone who fixes things that aren\'t broken.',
  44. // 8=>'A list is only as strong as its weakest link. — Donald Knuth',
  45. // //0=>'Additional fortune added at request time.',
  46. // //0=>'Additional fortune added at request time.',
  47. // 3=>'After enough decimal places, nobody gives a damn.',
  48. // 7=>'Any program that runs right is obsolete.',
  49. // 10=>'Computers make very fast, very accurate mistakes.',
  50. // 6=>'Emacs is a nice operating system, but I prefer UNIX. — Tom Christaensen',
  51. // 9=>'Feature: A bug with seniority.',
  52. // 1=>'fortune: No such file or directory',
  53. // 12=>'フレームワークのベンチマーク'
  54. // ];
  55. // foreach($arr as $k=>$v) {
  56. // Yii::$app->db->createCommand('insert into Fortune (id,message) values (:id,:message)',[':id'=>$k,':message'=>$v])->execute();
  57. // }
  58. $arr = Yii::$app->db->createCommand('select id, message from Fortune')->queryAll();
  59. $arr[] = ['id'=>0,'message'=>'Additional fortune added at request time.'];
  60. asort($arr);
  61. header("Content-Type: text/html; charset=utf-8");
  62. echo <<<EOM
  63. <!DOCTYPE html>
  64. <html>
  65. <head>
  66. <title>Fortunes</title>
  67. </head>
  68. <body>
  69. <table>
  70. <tr>
  71. <th>id</th>
  72. <th>message</th>
  73. </tr>
  74. EOM;
  75. foreach ( $arr as $val ) {
  76. echo '<tr>';
  77. echo '<td>'.$val['id'].'</td>';
  78. echo '<td>'.Html::encode($val['message']).'</td>';
  79. echo '</tr>';
  80. }
  81. echo <<<EOM
  82. </table>
  83. </body>
  84. </html>
  85. EOM;
  86. }
  87. public function actionUpdates($queries = 1) {
  88. if ($queries > 500) $queries = 500;
  89. elseif ($queries <= 0 ) $queries = 1;
  90. $selectCommand = Yii::$app->db->createCommand('select randomNumber from World where id = :id');
  91. $updateCommand = Yii::$app->db->createCommand('update World set randomNumber = :num where id = :id');
  92. $arr = [];
  93. while (0 < $queries--) {
  94. // Store result in array.
  95. $id = mt_rand(1,10000);
  96. $randomNumber = mt_rand(1, 1000);
  97. $selectCommand->bindParam(':id',$id)->queryScalar();
  98. $updateCommand->bindValues([':id'=>$id,':num'=>$randomNumber])->execute();
  99. $arr[] = array('id' => $id, 'randomNumber' => $randomNumber);
  100. }
  101. return $this->resJson($arr);
  102. }
  103. public function actionPlaintext() {
  104. header("Content-Type: text/plain;");
  105. echo 'Hello, World!';
  106. }
  107. }