index.php 2.7 KB

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