index-micro.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. try {
  3. $app = new Phalcon\Mvc\Micro();
  4. // Setting up the database connection
  5. $app['db'] = function() {
  6. return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
  7. 'host' => 'tfb-database',
  8. 'dbname' => 'hello_world',
  9. 'username' => 'benchmarkdbuser',
  10. 'password' => 'benchmarkdbpass',
  11. 'options' => [
  12. PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
  13. PDO::ATTR_PERSISTENT => TRUE,
  14. ]
  15. ));
  16. };
  17. // Setting up the view component (seems to be required even when not used)
  18. $app['view'] = function() {
  19. $view = new \Phalcon\Mvc\View();
  20. $view->setViewsDir(__DIR__ . '/../app/views/');
  21. $view->registerEngines(array(
  22. ".volt" => function($view) {
  23. $volt = new \Phalcon\Mvc\View\Engine\Volt($view);
  24. $volt->setOptions(array(
  25. "path" => __DIR__ . "/../app/compiled-templates/",
  26. "extension" => ".c",
  27. "separator" => '_',
  28. ));
  29. return $volt;
  30. }
  31. ));
  32. return $view;
  33. };
  34. $app->map('/plaintext', function() {
  35. header("Content-Type: text/plain; charset=UTF-8");
  36. echo "Hello, World!";
  37. });
  38. $app->map('/json', function() {
  39. header("Content-Type: application/json");
  40. echo json_encode(array('message' => 'Hello, World!'));
  41. });
  42. //
  43. $app->map('/db', function() use ($app) {
  44. header("Content-Type: application/json");
  45. $db = $app['db'];
  46. $world = $db->fetchOne('SELECT * FROM world WHERE id = ' . mt_rand(1, 10000), Phalcon\Db\Enum::FETCH_ASSOC);
  47. echo json_encode($world);
  48. });
  49. // queries
  50. $app->map('/queries', function() use ($app) {
  51. header("Content-Type: application/json");
  52. $db = $app['db'];
  53. $queries = $app->request->getQuery('queries', null, 1);
  54. $queries = is_numeric($queries) ? min(max(intval($queries), 1), 500) : 1;
  55. $worlds = array();
  56. for ($i = 0; $i < $queries; ++$i) {
  57. $worlds[] = $db->fetchOne('SELECT * FROM world WHERE id = ' . mt_rand(1, 10000), Phalcon\Db\Enum::FETCH_ASSOC);
  58. }
  59. echo json_encode($worlds);
  60. });
  61. // /fortunes
  62. $app->map('/fortunes', function() use ($app) {
  63. $fortunes = $app['db']->query('SELECT * FROM fortune')->fetchAll();
  64. $fortunes[] = array(
  65. 'id' => 0,
  66. 'message' => 'Additional fortune added at request time.'
  67. );
  68. usort($fortunes, function($left, $right) {
  69. return $left['message'] <=> $right['message'];
  70. });
  71. header("Content-Type: text/html; charset=utf-8");
  72. echo $app['view']->getRender('bench', 'fortunes', array(
  73. 'fortunes' => $fortunes
  74. ));
  75. });
  76. $url = $_REQUEST['_url'] ?? '/';
  77. $app->handle($url);
  78. } catch(\Phalcon\Exception $e) {
  79. echo "PhalconException: ", $e->getMessage();
  80. }