123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- <?php
- namespace ImiApp\ApiServer\Controller;
- use Imi\Db\Db;
- use Imi\RequestContext;
- use ImiApp\Model\World;
- use ImiApp\Model\Fortune;
- use Imi\Redis\RedisManager;
- use Imi\Util\Stream\MemoryStream;
- use Imi\Controller\HttpController;
- use Imi\Server\View\Annotation\View;
- use Imi\Server\Route\Annotation\Route;
- use Imi\Server\Route\Annotation\Action;
- use Imi\Server\Route\Annotation\Controller;
- /**
- * @Controller("/")
- */
- class IndexController extends HttpController
- {
- /**
- * @Action
- *
- * @return void
- */
- public function json()
- {
- return ['message' => 'Hello, World!'];
- }
- /**
- * @Action
- * @View(renderType="html")
- *
- * @return void
- */
- public function plaintext()
- {
- return RequestContext::get('response')->withHeader('Content-Type', 'text/plain; charset=utf-8')->write('Hello, World!');
- }
- /**
- * @Action
- *
- * @return void
- */
- public function dbModel()
- {
- return World::find(mt_rand(1, 10000));
- }
- /**
- * @Action
- *
- * @return void
- */
- public function dbQueryBuilder()
- {
- return Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', mt_rand(1, 10000))->select()->get();
- }
- /**
- * @Action
- *
- * @return void
- */
- public function dbRaw()
- {
- $db = Db::getInstance();
- $stmt = $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?');
- $stmt->execute([mt_rand(1, 10000)]);
- return $stmt->fetch();
- }
- /**
- * @Action
- *
- * @return void
- */
- public function queryModel($queries)
- {
- if($queries > 1)
- {
- $queryCount = min($queries, 500);
- }
- else
- {
- $queryCount = 1;
- }
- $list = [];
- while ($queryCount--)
- {
- $list[] = World::find(mt_rand(1, 10000));
- }
- return $list;
- }
- /**
- * @Action
- *
- * @return void
- */
- public function queryQueryBuilder($queries)
- {
- if($queries > 1)
- {
- $queryCount = min($queries, 500);
- }
- else
- {
- $queryCount = 1;
- }
- $list = [];
- while ($queryCount--)
- {
- $list[] = Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', mt_rand(1, 10000))->select()->get();
- }
- return $list;
- }
- /**
- * @Action
- *
- * @return void
- */
- public function queryRaw($queries)
- {
- if($queries > 1)
- {
- $queryCount = min($queries, 500);
- }
- else
- {
- $queryCount = 1;
- }
- $list = [];
- $db = Db::getInstance();
- $stmt = $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?');
- while ($queryCount--)
- {
- $stmt->execute([mt_rand(1, 10000)]);
- $list[] = $stmt->fetch();
- }
- return $list;
- }
- /**
- * @Action
- * @View(renderType="html")
- *
- * @return void
- */
- public function fortunes()
- {
- RequestContext::use(function(&$context){
- $context['response'] = $context['response']->withHeader('Content-Type', 'text/html; charset=UTF-8');
- });
- $list = Fortune::select();
- $rows = [];
- foreach($list as $item)
- {
- $rows[$item->id] = $item->message;
- }
- $rows[0] = 'Additional fortune added at request time.';
- asort($rows);
- return [
- 'rows' => $rows,
- ];
- }
- /**
- * @Action
- * @View(renderType="html")
- *
- * @return void
- */
- public function fortunesRaw()
- {
- $rows = [];
- foreach(Db::getInstance()->query('SELECT id, message FROM Fortune')->fetchAll() as $item)
- {
- $rows[$item['id']] = $item['message'];
- }
- $rows[0] = 'Additional fortune added at request time.';
- asort($rows);
- $html = '';
- foreach ($rows as $id => $message)
- {
- $message = \htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
- $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
- }
- return RequestContext::get('response')->withHeader('Content-Type', 'text/html; charset=UTF-8')
- ->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>"));
- }
- /**
- * @Action
- *
- * @return void
- */
- public function updateModel($queries)
- {
- if($queries > 1)
- {
- $queryCount = min($queries, 500);
- }
- else
- {
- $queryCount = 1;
- }
- $list = [];
- while ($queryCount--)
- {
- $list[] = $row = World::find(mt_rand(1, 10000));
- $row->randomNumber = mt_rand(1, 10000);
- $row->update();
- }
- return $list;
- }
- /**
- * @Action
- *
- * @return void
- */
- public function updateQueryBuilder($queries)
- {
- if($queries > 1)
- {
- $queryCount = min($queries, 500);
- }
- else
- {
- $queryCount = 1;
- }
- $list = [];
- while ($queryCount--)
- {
- $id = mt_rand(1, 10000);
- $row = Db::query()->from('World')->field('id', 'randomNumber')->where('id', '=', $id)->select()->get();
- $row['randomNumber'] = mt_rand(1, 10000);
- Db::query()->from('World')->where('id', '=', $row['id'])->update([
- 'randomNumber' => $row['randomNumber'],
- ]);
- $list[] = $row;
- }
- return $list;
- }
- /**
- * @Action
- *
- * @return void
- */
- public function updateRaw($queries)
- {
- if($queries > 1)
- {
- $queryCount = min($queries, 500);
- }
- else
- {
- $queryCount = 1;
- }
- $list = [];
- $db = Db::getInstance();
- $stmtSelect = $db->prepare('SELECT id, randomNumber FROM World WHERE id = ?');
- $stmtUpdate = $db->prepare('UPDATE World SET randomNumber = :randomNumber WHERE id = :id');
- while ($queryCount--)
- {
- $id = mt_rand(1, 10000);
- $stmtSelect->execute([$id]);
- $row = $stmtSelect->fetch();
- $row['randomNumber'] = mt_rand(1, 10000);
- $stmtUpdate->execute([
- 'id' => $row['id'],
- 'randomNumber' => $row['randomNumber'],
- ]);
- $list[] = $row;
- }
- return $list;
- }
- /**
- * @Action
- * @Route("cached-worlds")
- *
- * @return void
- */
- public function cachedWorlds($count)
- {
- if($count > 1)
- {
- $queryCount = min($count, 500);
- }
- else
- {
- $queryCount = 1;
- }
- $ids = [];
- while ($queryCount--)
- {
- $ids[] = 'world:' . mt_rand(1, 10000);
- }
- return RedisManager::getInstance()->mget($ids);
- }
- }
|