Browse Source

Change to the templates

Skamander 12 years ago
parent
commit
4525613628

+ 12 - 0
php-symfony2/app/Resources/views/base.html.php

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta charset="UTF-8" />
+        <title><?php $view['slots']->output('title') ?></title>
+        <?php $view['slots']->output('stylesheets') ?>
+    </head>
+    <body>
+        <?php $view['slots']->output('body') ?>
+        <?php $view['slots']->output('javascripts') ?>
+    </body>
+</html>

+ 1 - 1
php-symfony2/app/config/config_prod.yml

@@ -14,7 +14,7 @@ framework:
 doctrine:
     orm:
         metadata_cache_driver: apc
-#        result_cache_driver: apc
+        #result_cache_driver: apc
         query_cache_driver: apc
 
 monolog:

+ 4 - 0
php-symfony2/app/config/routing.yml

@@ -14,6 +14,10 @@ _fortunes:
     pattern:  /fortunes
     defaults: { _controller: SkamanderBenchmarkBundle:Bench:fortunes }
 
+_fortunesPhp:
+    pattern:  /fortunes-php
+    defaults: { _controller: SkamanderBenchmarkBundle:Bench:fortunesPhp }
+
 _fortunesRaw:
     pattern:  /fortunes-raw
     defaults: { _controller: SkamanderBenchmarkBundle:Bench:fortunesRaw }

+ 7 - 1
php-symfony2/benchmark_config

@@ -10,13 +10,19 @@
       "port": 8080,
       "sort": 51
     },
+    "php-templates": {
+      "setup_file": "setup",
+      "fortune_url": "/fortunes-php",
+      "port": 8080,
+      "sort": 83
+    },
     "raw": {
       "setup_file": "setup",
       "db_url": "/db-raw",
       "query_url": "/db-raw?queries=",
       "fortune_url": "/fortunes-raw",
       "port": 8080,
-      "sort": 83
+      "sort": 91
     }
   }]
 }

+ 32 - 1
php-symfony2/src/Skamander/BenchmarkBundle/Controller/BenchController.php

@@ -5,6 +5,7 @@ namespace Skamander\BenchmarkBundle\Controller;
 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 use Symfony\Component\HttpFoundation\JsonResponse;
 use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
 use Skamander\BenchmarkBundle\Entity\Fortune;
 
 class BenchController extends Controller
@@ -67,7 +68,7 @@ class BenchController extends Controller
         ]);
     }
 
-    public function fortunesRawAction()
+    public function fortunesPhpAction()
     {
         $repo = $this->getDoctrine()
             ->getRepository('SkamanderBenchmarkBundle:Fortune');
@@ -87,4 +88,34 @@ class BenchController extends Controller
             'fortunes' => $fortunes
         ]);
     }
+
+    public function fortunesRawAction()
+    {
+        $repo = $this->getDoctrine()
+            ->getRepository('SkamanderBenchmarkBundle:Fortune');
+        $fortunes = $repo->findAll();
+
+        $runtimeFortune = new Fortune();
+        $runtimeFortune->setId(0)
+            ->setMessage('Additional fortune added at request time.');
+
+        $fortunes[] = $runtimeFortune;
+
+        usort($fortunes, function($left, $right) {
+            return strcmp($left->message, $right->message);
+        });
+
+        // This is not the symfony way to work with templates! It's implemented to show users
+        // who don't want to use template engines (like twig), or template sugar (like the slots etc.
+        // from symfony 2), because in their opinion already built-in php constructs like foreach +
+        // if else + include etc. are enough, that the performance impact should be neglectable, and
+        // that the advantages outweigh the disadvantages (performance).
+        $title = 'Fortunes';
+
+        ob_start();
+        include __DIR__ . '/../Resources/views/Bench/raw/content.php';
+        $response = ob_get_clean();
+
+        return new Response($response);
+    }
 }

+ 7 - 14
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/Bench/fortunes.html.php

@@ -1,25 +1,18 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-    <meta charset="utf-8">
-    <title>Benchmark</title>
-</head>
-<body>
+<?php $view->extend('SkamanderBenchmarkBundle::layout.html.php') ?>
 
+<?php $view['slots']->set('title', 'Fortunes') ?>
+
+<?php $view['slots']->start('content') ?>
 <table>
     <tr>
         <th>id</th>
         <th>message</th>
     </tr>
-
     <?php foreach($fortunes as $fortune): ?>
         <tr>
-            <td><?php echo $fortune->id; ?></td>
-            <td><?php echo htmlspecialchars($fortune->message, ENT_QUOTES, "UTF-8", false); ?></td>
+            <td><?php echo $view->escape($fortune->id); ?></td>
+            <td><?php echo $view->escape($fortune->message); ?></td>
         </tr>
     <?php endforeach; ?>
-
 </table>
-
-</body>
-</html>
+<?php $view['slots']->stop() ?>

+ 7 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/Bench/raw/content.php

@@ -0,0 +1,7 @@
+<?php include 'template_header.php'; ?>
+
+<div class="block">
+    <?php include 'fortunes.php'; ?>
+</div>
+
+<?php include 'template_footer.php'; ?>

+ 14 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/Bench/raw/fortunes.php

@@ -0,0 +1,14 @@
+<table>
+    <tr>
+        <th>id</th>
+        <th>message</th>
+    </tr>
+
+    <?php foreach($fortunes as $fortune): ?>
+        <tr>
+            <td><?php echo $fortune->id; ?></td>
+            <td><?php echo htmlspecialchars($fortune->message, ENT_QUOTES, "UTF-8", false); ?></td>
+        </tr>
+    <?php endforeach; ?>
+
+</table>

+ 2 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/Bench/raw/template_footer.php

@@ -0,0 +1,2 @@
+</body>
+</html>

+ 7 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/Bench/raw/template_header.php

@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <meta charset="UTF-8" />
+    <title><?php echo $title ?></title>
+</head>
+<body>

+ 9 - 0
php-symfony2/src/Skamander/BenchmarkBundle/Resources/views/layout.html.php

@@ -0,0 +1,9 @@
+<?php $view->extend('::base.html.php') ?>
+
+<?php $view['slots']->set('title', 'Benchmark Bundle') ?>
+
+<?php $view['slots']->start('body') ?>
+    <div class="block">
+        <?php $view['slots']->output('content') ?>
+    </div>
+<?php $view['slots']->stop() ?>