index.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. 'dsn' => 'host=localhost;dbname=hello_world;charset=utf8',
  8. 'username' => 'benchmarkdbuser',
  9. 'password' => 'benchmarkdbpass',
  10. 'persistent' => true
  11. ));
  12. };
  13. // Setting up the view component (seems to be required even when not used)
  14. $app['view'] = function() {
  15. $view = new \Phalcon\Mvc\View();
  16. $view->setViewsDir(__DIR__ . '/../views/');
  17. $view->registerEngines(array(
  18. ".volt" => function($view, $di) {
  19. $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
  20. $volt->setOptions(array(
  21. "compiledPath" => __DIR__ . "/../compiled-templates/",
  22. "compiledExtension" => ".c",
  23. "compiledSeparator" => '_',
  24. ));
  25. return $volt;
  26. }
  27. ));
  28. return $view;
  29. };
  30. $app->map('/json', function() {
  31. header("Content-Type: application/json");
  32. echo json_encode(array('message' => 'Hello, World!'));
  33. });
  34. //
  35. $app->map('/db', function() use ($app) {
  36. $db = $app['db'];
  37. $queries = $app->request->getQuery('queries', null, 1);
  38. $worlds = array();
  39. for ($i = 0; $i < $queries; ++$i) {
  40. $worlds[] = $db->fetchOne('SELECT * FROM world WHERE id = ' . mt_rand(1, 10000), Phalcon\Db::FETCH_ASSOC);
  41. }
  42. if ($queries == 1) {
  43. $worlds = $worlds[0];
  44. }
  45. echo json_encode($worlds);
  46. });
  47. // /fortunes
  48. $app->map('/fortunes', function() use ($app) {
  49. $fortunes = $app['db']->query('SELECT * FROM fortune')->fetchAll();
  50. $fortunes[] = array(
  51. 'id' => 0,
  52. 'message' => 'Additional fortune added at request time.'
  53. );
  54. usort($fortunes, function($left, $right) {
  55. $l = $left['message'];
  56. $r = $right['message'];
  57. if ($l === $r) {
  58. return 0;
  59. } else {
  60. if ($l > $r) {
  61. return 1;
  62. } else {
  63. return -1;
  64. }
  65. }
  66. });
  67. header("Content-Type: text/html; charset=utf-8");
  68. echo $app['view']->getRender('bench', 'fortunes', array(
  69. 'fortunes' => $fortunes
  70. ));
  71. });
  72. $app->handle();
  73. } catch(\Phalcon\Exception $e) {
  74. echo "PhalconException: ", $e->getMessage();
  75. }