IndexController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. namespace ImiApp\ApiServer\Controller;
  3. use Imi\Db\Db;
  4. use Imi\RequestContext;
  5. use ImiApp\Model\World;
  6. use ImiApp\Model\Fortune;
  7. use Imi\Redis\RedisManager;
  8. use Imi\Util\Stream\MemoryStream;
  9. use Imi\Controller\HttpController;
  10. use Imi\Server\View\Annotation\View;
  11. use Imi\Server\Route\Annotation\Route;
  12. use Imi\Server\Route\Annotation\Action;
  13. use Imi\Server\Route\Annotation\Controller;
  14. /**
  15. * @Controller("/")
  16. */
  17. class IndexController extends HttpController
  18. {
  19. /**
  20. * @Action
  21. *
  22. * @return void
  23. */
  24. public function json()
  25. {
  26. return ['message' => 'Hello, World!'];
  27. }
  28. /**
  29. * @Action
  30. * @View(renderType="html")
  31. *
  32. * @return void
  33. */
  34. public function plaintext()
  35. {
  36. return RequestContext::get('response')->withHeader('Content-Type', 'text/plain; charset=utf-8')->write('Hello, World!');
  37. }
  38. /**
  39. * @Action
  40. *
  41. * @return void
  42. */
  43. public function dbModel()
  44. {
  45. return World::find(mt_rand(1, 10000));
  46. }
  47. /**
  48. * @Action
  49. *
  50. * @return void
  51. */
  52. public function dbQueryBuilder()
  53. {
  54. return Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', mt_rand(1, 10000))->select()->get();
  55. }
  56. /**
  57. * @Action
  58. *
  59. * @return void
  60. */
  61. public function dbRaw()
  62. {
  63. $db = Db::getInstance();
  64. $stmt = $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?');
  65. $stmt->execute([mt_rand(1, 10000)]);
  66. return $stmt->fetch();
  67. }
  68. /**
  69. * @Action
  70. *
  71. * @return void
  72. */
  73. public function queryModel($queries)
  74. {
  75. if($queries > 1)
  76. {
  77. $queryCount = min($queries, 500);
  78. }
  79. else
  80. {
  81. $queryCount = 1;
  82. }
  83. $list = [];
  84. while ($queryCount--)
  85. {
  86. $list[] = World::find(mt_rand(1, 10000));
  87. }
  88. return $list;
  89. }
  90. /**
  91. * @Action
  92. *
  93. * @return void
  94. */
  95. public function queryQueryBuilder($queries)
  96. {
  97. if($queries > 1)
  98. {
  99. $queryCount = min($queries, 500);
  100. }
  101. else
  102. {
  103. $queryCount = 1;
  104. }
  105. $list = [];
  106. while ($queryCount--)
  107. {
  108. $list[] = Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', mt_rand(1, 10000))->select()->get();
  109. }
  110. return $list;
  111. }
  112. /**
  113. * @Action
  114. *
  115. * @return void
  116. */
  117. public function queryRaw($queries)
  118. {
  119. if($queries > 1)
  120. {
  121. $queryCount = min($queries, 500);
  122. }
  123. else
  124. {
  125. $queryCount = 1;
  126. }
  127. $list = [];
  128. $db = Db::getInstance();
  129. $stmt = $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?');
  130. while ($queryCount--)
  131. {
  132. $stmt->execute([mt_rand(1, 10000)]);
  133. $list[] = $stmt->fetch();
  134. }
  135. return $list;
  136. }
  137. /**
  138. * @Action
  139. * @View(renderType="html")
  140. *
  141. * @return void
  142. */
  143. public function fortunes()
  144. {
  145. RequestContext::use(function(&$context){
  146. $context['response'] = $context['response']->withHeader('Content-Type', 'text/html; charset=UTF-8');
  147. });
  148. $list = Fortune::select();
  149. $rows = [];
  150. foreach($list as $item)
  151. {
  152. $rows[$item->id] = $item->message;
  153. }
  154. $rows[0] = 'Additional fortune added at request time.';
  155. asort($rows);
  156. return [
  157. 'rows' => $rows,
  158. ];
  159. }
  160. /**
  161. * @Action
  162. * @View(renderType="html")
  163. *
  164. * @return void
  165. */
  166. public function fortunesRaw()
  167. {
  168. $rows = [];
  169. foreach(Db::getInstance()->query('SELECT id, message FROM Fortune')->fetchAll() as $item)
  170. {
  171. $rows[$item['id']] = $item['message'];
  172. }
  173. $rows[0] = 'Additional fortune added at request time.';
  174. asort($rows);
  175. $html = '';
  176. foreach ($rows as $id => $message)
  177. {
  178. $message = \htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
  179. $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
  180. }
  181. return RequestContext::get('response')->withHeader('Content-Type', 'text/html; charset=UTF-8')
  182. ->withBody(new MemoryStream("<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>{$html}</table></body></html>"));
  183. }
  184. /**
  185. * @Action
  186. *
  187. * @return void
  188. */
  189. public function updateModel($queries)
  190. {
  191. if($queries > 1)
  192. {
  193. $queryCount = min($queries, 500);
  194. }
  195. else
  196. {
  197. $queryCount = 1;
  198. }
  199. $list = [];
  200. while ($queryCount--)
  201. {
  202. $list[] = $row = World::find(mt_rand(1, 10000));
  203. $row->randomNumber = mt_rand(1, 10000);
  204. $row->update();
  205. }
  206. return $list;
  207. }
  208. /**
  209. * @Action
  210. *
  211. * @return void
  212. */
  213. public function updateQueryBuilder($queries)
  214. {
  215. if($queries > 1)
  216. {
  217. $queryCount = min($queries, 500);
  218. }
  219. else
  220. {
  221. $queryCount = 1;
  222. }
  223. $list = [];
  224. while ($queryCount--)
  225. {
  226. $id = mt_rand(1, 10000);
  227. $row = Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', $id)->select()->get();
  228. $row['randomNumber'] = mt_rand(1, 10000);
  229. Db::query()->from('World')->where('id', '=', $row['id'])->update([
  230. 'randomNumber' => $row['randomNumber'],
  231. ]);
  232. $list[] = $row;
  233. }
  234. return $list;
  235. }
  236. /**
  237. * @Action
  238. *
  239. * @return void
  240. */
  241. public function updateRaw($queries)
  242. {
  243. if($queries > 1)
  244. {
  245. $queryCount = min($queries, 500);
  246. }
  247. else
  248. {
  249. $queryCount = 1;
  250. }
  251. $list = [];
  252. $db = Db::getInstance();
  253. $stmtSelect = $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?');
  254. $stmtUpdate = $db->prepare('UPDATE World SET randomNumber = :randomNumber WHERE id = :id');
  255. while ($queryCount--)
  256. {
  257. $id = mt_rand(1, 10000);
  258. $stmtSelect->execute([$id]);
  259. $row = $stmtSelect->fetch();
  260. $row['randomNumber'] = mt_rand(1, 10000);
  261. $stmtUpdate->execute([
  262. 'id' => $row['id'],
  263. 'randomNumber' => $row['randomNumber'],
  264. ]);
  265. $list[] = $row;
  266. }
  267. return $list;
  268. }
  269. /**
  270. * @Action
  271. * @Route("cached-worlds")
  272. *
  273. * @return void
  274. */
  275. public function cachedWorlds($count)
  276. {
  277. if($count > 1)
  278. {
  279. $queryCount = min($count, 500);
  280. }
  281. else
  282. {
  283. $queryCount = 1;
  284. }
  285. $ids = [];
  286. while ($queryCount--)
  287. {
  288. $ids[] = 'world:' . mt_rand(1, 10000);
  289. }
  290. return RedisManager::getInstance()->mget($ids);
  291. }
  292. }