123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace app\controller;
- use support\Request;
- use support\bootstrap\Date;
- use support\bootstrap\db\Raw as Db;
- use support\Response;
- use PDO;
- class Index
- {
- public function plaintext($request)
- {
- return new Response(200, [
- 'Content-Type' => 'text/plain',
- 'Date' => Date::$date
- ], 'Hello, World!');
- }
- public function json($request)
- {
- return new Response(200, [
- 'Content-Type' => 'application/json',
- 'Date' => Date::$date
- ], json_encode(['message' => 'Hello, World!']));
- }
- public function db($request)
- {
- $statement = Db::$random;
- $statement->execute([\mt_rand(1, 10000)]);
- return new Response(200, [
- 'Content-Type' => 'application/json',
- 'Date' => Date::$date
- ], json_encode($statement->fetch()));
- }
- public function fortunes($request)
- {
- $fortune = Db::$fortune;
- $fortune->execute();
- $arr = $fortune->fetchAll(\PDO::FETCH_KEY_PAIR);
- $arr[0] = 'Additional fortune added at request time.';
- \asort($arr);
- $html = '';
- foreach ($arr as $id => $message) {
- $message = \htmlspecialchars($message, \ENT_QUOTES, 'UTF-8');
- $html .= "<tr><td>$id</td><td>$message</td></tr>";
- }
- return new Response(200, [
- 'Date' => Date::$date
- ], "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>"
- );
- }
- public function queries($request, $q = 1)
- {
- $statement = Db::$random;
- $query_count = 1;
- if ((int) $q > 1) {
- $query_count = \min($q, 500);
- }
- $arr = [];
- while ($query_count--) {
- $statement->execute([\mt_rand(1, 10000)]);
- $arr[] = $statement->fetch();
- }
- return new Response(200, [
- 'Content-Type' => 'application/json',
- 'Date' => Date::$date
- ], json_encode($arr));
- }
- public function updates($request, $q = 1)
- {
- $random = Db::$random;
- $query_count = 1;
- if ((int) $q > 1) {
- $query_count = \min($q, 500);
- }
- $worlds = [];
- while ($query_count--) {
- $random->execute([\mt_rand(1, 10000)]);
- $world = $random->fetch();
- $world['randomNumber'] = \mt_rand(1, 10000);
- $worlds[] = $world;
- }
- Db::update($worlds);
- return new Response(200, [
- 'Content-Type' => 'application/json',
- 'Date' => Date::$date
- ], \json_encode($worlds));
- }
- }
|