Browse Source

improve code to be more natural with framework guidelines

kenjikobe 12 years ago
parent
commit
43e8403f76

+ 3 - 4
php-phalcon/app/config/config.php

@@ -9,10 +9,9 @@ return new \Phalcon\Config(array(
         'name'     => 'hello_world',
     ),
     'application' => array(
-        'controllersDir' => __DIR__ . '/../../app/controllers/',
-        'modelsDir'      => __DIR__ . '/../../app/models/',
-        'viewsDir'       => __DIR__ . '/../../app/views/',
-        'routes'         => __DIR__ . '/../../app/config/routes.php',
+        'controllersDir' => APP_PATH . '/app/controllers/',
+        'modelsDir'      => APP_PATH . '/app/models/',
+        'viewsDir'       => APP_PATH . '/app/views/',
         'baseUri'        => '/',
     )
 ));

+ 1 - 1
php-phalcon/app/config/routes.php

@@ -1,6 +1,6 @@
 <?php
 
-$router = new Phalcon\Mvc\Router();
+$router = new Phalcon\Mvc\Router(false);
 
 $router->add('/json', array(
     'controller' => 'bench',

+ 21 - 26
php-phalcon/app/controllers/BenchController.php

@@ -1,11 +1,14 @@
 <?php
 
+use Phalcon\Mvc\View,
+    Phalcon\Mvc\Model\Resultset;
+
 class BenchController extends \Phalcon\Mvc\Controller
 {
     public function initialize()
     {
         // views must be renderd explicitly. safes processing time when not needed.
-        $this->view->disable();
+        $this->view->setRenderLevel(View::LEVEL_LAYOUT);
     }
 
     public function jsonAction() {
@@ -15,7 +18,9 @@ class BenchController extends \Phalcon\Mvc\Controller
     }
 
     public function dbAction() {
-        $queries = $this->getQueryOrDefault('queries', 1);
+
+        $queries = $this->request->getQuery('queries', null, 1);
+
         $worlds = array();
 
         for ($i = 0; $i < $queries; ++$i) {
@@ -26,50 +31,40 @@ class BenchController extends \Phalcon\Mvc\Controller
     }
 
     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']) {
+            $l = $left['message'];
+            $r = $right['message'];
+            if ($l === $r) {
                 return 0;
-            } else if ($left['message'] > $right['message']) {
-                return 1;
             } else {
-                return -1;
+                if ($l > $r) {
+                    return 1;
+                } else {
+                    return -1;
+                }
             }
         });
 
-        return $this->sendContentAsText(
-            $this->view->getRender('bench', 'fortunes', array(
-                'fortunes' => $fortunes
-            ))
-        );
-    }
-
-    private function getQueryOrDefault($query, $default) {
-        return $this->request->getQuery($query) !== null
-            ? $this->request->getQuery($query)
-            : $default;
-    }
+        $this->response->setStatusCode(200, "OK");
+        $this->response->setHeader("Content-Type", "text/html; charset=utf-8");
 
-    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;
+        $this->view->fortunes = $fortunes;
     }
 
     private function sendContentAsJson($content) {
-        $response = new Phalcon\Http\Response();
+        $response = new Phalcon\Http\Response(json_encode($content));
         $response->setStatusCode(200, "OK");
         $response->setHeader("Content-Type", "application/json");
-        $response->setContent(json_encode($content));
         return $response;
     }
 }

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

@@ -1,6 +1,5 @@
 <?php
 
-
 class Fortunes extends \Phalcon\Mvc\Model
 {
     public $id;

+ 0 - 1
php-phalcon/app/models/Worlds.php

@@ -1,6 +1,5 @@
 <?php
 
-
 class Worlds extends \Phalcon\Mvc\Model
 {
     public $id;

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

@@ -1,20 +1 @@
-{% 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 %}
+<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>

+ 1 - 0
php-phalcon/app/views/layouts/bench.volt

@@ -0,0 +1 @@
+<!DOCTYPE html><html><head><title>Fortunes</title><meta charset="utf-8"></head><body>{{ content() }}</body></html>

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

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

+ 37 - 19
php-phalcon/public/index.php

@@ -1,8 +1,11 @@
 <?php
 
+define('APP_PATH', realpath('..'));
+
 try {
+
     // Load the config
-    $config = include(__DIR__."/../app/config/config.php");
+    $config = include APP_PATH . "/app/config/config.php";
 
     // Register an autoloader
     $loader = new \Phalcon\Loader();
@@ -15,27 +18,39 @@ try {
     $di = new Phalcon\DI\FactoryDefault();
 
     // Setting up the router
-    $di->set('router', function() use ($config) {
-        return include($config->application->routes);
-    });
+    $di->set('router', require APP_PATH . '/app/config/routes.php');
 
-    //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;
+    //MetaData
+    $di->set('modelsMetadata', function(){
+        if (function_exists('apc_store')) {
+            return new Phalcon\Mvc\Model\MetaData\Apc();
+        } else {
+            return new Phalcon\Mvc\Model\MetaData\Files(array(
+                'metaDataDir' => APP_PATH . "/app/compiled-templates/"
+            ));
+        }
     });
 
     // Setting up the view component (seems to be required even when not used)
     $di->set('view', function() use ($config) {
+
         $view = new \Phalcon\Mvc\View();
+
         $view->setViewsDir($config->application->viewsDir);
+
         $view->registerEngines(array(
-            ".volt" => 'voltService'
+            ".volt" => function($view, $di) {
+
+                $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
+
+                $volt->setOptions(array(
+                    "compiledPath" => APP_PATH . "/app/compiled-templates/",
+                    "compiledExtension" => ".compiled",
+                    "compiledSeparator" => '_',
+                ));
+
+                return $volt;
+            }
         ));
 
         return $view;
@@ -43,11 +58,14 @@ try {
 
     // Setting up the database connection
     $di->set('db', function() use ($config) {
+
+        $database = $config->database;
+
         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' => $database->host,
+            'username' => $database->username,
+            'password' => $database->password,
+            'dbname' => $database->name,
             'options'  => array(
                 PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
             )
@@ -61,4 +79,4 @@ try {
 
 } catch(\Phalcon\Exception $e) {
     echo "PhalconException: ", $e->getMessage();
-}
+}