Browse Source

Implemented the fortune testcase

Skamander 12 years ago
parent
commit
a551ebab9f

+ 9 - 0
php-phalcon/README.md

@@ -13,6 +13,11 @@ Uses the built-in ORM of Phalcon PHP
 
 
 * [DB test controller](app/controllers/BenchController.php)
 * [DB test controller](app/controllers/BenchController.php)
 
 
+### Template Test
+Uses Phalcons template engine 'Volt'
+
+* [Template test controller](app/controllers/BenchController.php)
+
 
 
 ## Infrastructure Software Versions
 ## Infrastructure Software Versions
 The tests were run with:
 The tests were run with:
@@ -34,3 +39,7 @@ http://localhost/db
 ### Variable Query Test
 ### Variable Query Test
     
     
 http://localhost/db?queries=2
 http://localhost/db?queries=2
+
+### Templating Test
+
+http://localhost/fortunes

+ 0 - 0
php-phalcon/app/compiled-templates/.keepit


+ 5 - 0
php-phalcon/app/config/routes.php

@@ -13,4 +13,9 @@ $router->add('/db', array(
     'action' => 'db',
     'action' => 'db',
 ));
 ));
 
 
+$router->add('/fortunes', array(
+    'controller' => 'bench',
+    'action' => 'fortunes',
+));
+
 return $router;
 return $router;

+ 35 - 0
php-phalcon/app/controllers/BenchController.php

@@ -4,6 +4,7 @@ class BenchController extends \Phalcon\Mvc\Controller
 {
 {
     public function initialize()
     public function initialize()
     {
     {
+        // views must be renderd explicitly. safes processing time when not needed.
         $this->view->disable();
         $this->view->disable();
     }
     }
 
 
@@ -24,12 +25,46 @@ class BenchController extends \Phalcon\Mvc\Controller
         return $this->sendContentAsJson($worlds);
         return $this->sendContentAsJson($worlds);
     }
     }
 
 
+    public function fortunesAction() {
+        // since the resultset is immutable get an array instead
+        // so we can add the new fortune
+        $fortunes = Fortunes::find()->toArray();
+        $fortunes[] = array(
+            'id' => 0,
+            'message' => 'Additional fortune added at request time.'
+        );
+
+        usort($fortunes, function($left, $right) {
+            if ($left['message'] === $right['message']) {
+                return 0;
+            } else if ($left['message'] > $right['message']) {
+                return 1;
+            } else {
+                return -1;
+            }
+        });
+
+        return $this->sendContentAsText(
+            $this->view->getRender('bench', 'fortunes', array(
+                'fortunes' => $fortunes
+            ))
+        );
+    }
+
     private function getQueryOrDefault($query, $default) {
     private function getQueryOrDefault($query, $default) {
         return $this->request->getQuery($query) !== null
         return $this->request->getQuery($query) !== null
             ? $this->request->getQuery($query)
             ? $this->request->getQuery($query)
             : $default;
             : $default;
     }
     }
 
 
+    private function sendContentAsText($content) {
+        $response = new Phalcon\Http\Response();
+        $response->setStatusCode(200, "OK");
+        $response->setHeader("Content-Type", "text/html; charset=utf-8");
+        $response->setContent($content);
+        return $response;
+    }
+
     private function sendContentAsJson($content) {
     private function sendContentAsJson($content) {
         $response = new Phalcon\Http\Response();
         $response = new Phalcon\Http\Response();
         $response->setStatusCode(200, "OK");
         $response->setStatusCode(200, "OK");

+ 13 - 0
php-phalcon/app/models/Fortunes.php

@@ -0,0 +1,13 @@
+<?php
+
+
+class Fortunes extends \Phalcon\Mvc\Model
+{
+    public $id;
+
+    public $message;
+
+    public function getSource() {
+        return "Fortune";
+    }
+}

+ 20 - 0
php-phalcon/app/views/bench/fortunes.volt

@@ -0,0 +1,20 @@
+{% extends "templates/base.volt" %}
+
+{% block title %} Fortunes {% endblock %}
+
+{% block content %}
+    <table>
+        <tr>
+            <th>id</th>
+            <th>message</th>
+        </tr>
+
+        {% for fortune in fortunes %}
+            <tr>
+                <td>{{ fortune['id'] }}</td>
+                <td>{{ fortune['message'] | e }}</td>
+            </tr>
+        {% endfor %}
+
+    </table>
+{% endblock %}

+ 10 - 0
php-phalcon/app/views/templates/base.volt

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>{% block title %}{% endblock %}</title>
+    <meta charset="utf-8">
+</head>
+<body>
+{% block content %}{% endblock %}
+</body>
+</html>

+ 1 - 0
php-phalcon/benchmark_config

@@ -6,6 +6,7 @@
       "json_url": "/json",
       "json_url": "/json",
       "db_url": "/db",
       "db_url": "/db",
       "query_url": "/db?queries=",
       "query_url": "/db?queries=",
+      "fortune_url": "/fortunes",
       "port": 8080,
       "port": 8080,
       "sort": 72
       "sort": 72
     }
     }

+ 22 - 4
php-phalcon/public/index.php

@@ -19,20 +19,38 @@ try {
         return include($config->application->routes);
         return include($config->application->routes);
     });
     });
 
 
+    //Register Volt as a service
+    $di->set('voltService', function($view, $di) {
+        $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
+        $volt->setOptions(array(
+            "compiledPath" => "../app/compiled-templates/",
+            "compiledExtension" => ".compiled"
+        ));
+
+        return $volt;
+    });
+
     // Setting up the view component (seems to be required even when not used)
     // Setting up the view component (seems to be required even when not used)
     $di->set('view', function() use ($config) {
     $di->set('view', function() use ($config) {
         $view = new \Phalcon\Mvc\View();
         $view = new \Phalcon\Mvc\View();
         $view->setViewsDir($config->application->viewsDir);
         $view->setViewsDir($config->application->viewsDir);
+        $view->registerEngines(array(
+            ".volt" => 'voltService'
+        ));
+
         return $view;
         return $view;
     });
     });
 
 
     // Setting up the database connection
     // Setting up the database connection
     $di->set('db', function() use ($config) {
     $di->set('db', function() use ($config) {
         return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
         return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
-            "host"     => $config->database->host,
-            "username" => $config->database->username,
-            "password" => $config->database->password,
-            "dbname"   => $config->database->name
+            'host'     => $config->database->host,
+            'username' => $config->database->username,
+            'password' => $config->database->password,
+            'dbname'   => $config->database->name,
+            'options'  => array(
+                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
+            )
         ));
         ));
     });
     });