SiteController.php 4.6 KB

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