Using Twig with Phalcon PHP and kill it!!

Using Twig with Phalcon PHP.

Twig is a modern template engine for PHP.
Now we can Using Twig with Phalcon.
How to do:
1.Install Twing in library as git submodule.

1
git submodule add git@github.com:fabpot/Twig.git app/library/Twig

2.Install phalcon incubator in library as git submodule.

1
git submodule add git@github.com:phalcon/incubator.git app/library/incubator

3.Edit your app/config/loader.php file and add a twig,incubator to autoloader.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
 
$loader = new \Phalcon\Loader();
 
/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir
    )
);
 
$loader->registerNamespaces(array(
    'Phalcon' => __DIR__ . '/../../app/library/incubator/Library/Phalcon/',
 
));
 
$loader->register();
 
require __DIR__ . '/../../app/library/Twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();

4.Edit your app/config/services.php file and add a twig service to DI

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * Setting up the view component
 */
$di->set('twigService', function($view, $di) use ($config) {
    $options = array(
        'debug' => true,
        'charset' => 'UTF-8',
        'base_template_class' => 'Twig_Template',
        'strict_variables' => false,
        'autoescape' => false,
        $config->application->cacheDir,
        'auto_reload' => null,
        'optimizations' => -1,
    );
    $twig = new \Phalcon\Mvc\View\Engine\Twig($view, $di, $options);
    return $twig;
}, true);
 
$di->set('view', function () use ($config) {
    $view = new \Phalcon\Mvc\View();
    $view->setViewsDir($config->application->viewsDir);
    $view->registerEngines(array(
        ".twig" => 'twigService'
    ));
    return $view;
}, true);

5. add New Twig templete Files app/views/index.twig.

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html>
	<head>
		<title>Phalcon PHP Framework</title>
	</head>
	<body>
		<h1>Congratulations!</h1>
                <p>You're now flying with Phalcon. Great things are about to happen!</p>
	</body>
</html>

It work fine.
螢幕快照 2014-04-17 13.31.41

kill Phalcon!!

But it there good for Phalcon?

before:
螢幕快照 2014-04-16 13.23.04
after:
螢幕快照 2014-04-17 13.34.03

we can see it is slow down!

1
2
  before : Total Incl. Wall Time (microsec): 1,339 microsecs
  after  : Total Incl. Wall Time (microsec): 20,496 microsecs

and it very different.
before:
phalcon_callgraph.php
after:
twig_phalcon
And i take off facebook XHPROF from index.php files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
////Start enable facebook XHPROF
//xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
 
error_reporting(E_ALL);
 
try {
 
    /**
     * Read the configuration
     */
    $config = include __DIR__ . "/../app/config/config.php";
 
    /**
     * Read auto-loader
     */
    include __DIR__ . "/../app/config/loader.php";
 
    /**
     * Read services
     */
    include __DIR__ . "/../app/config/services.php";
 
    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application($di);
 
    echo $application->handle()->getContent();
 
} catch (\Exception $e) {
    echo $e->getMessage();
}
 
////end of facebook XHPROF
//$xhprof_data = xhprof_disable();
//$XHPROF_ROOT = __DIR__ . " /../../www/";
//include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
//include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
//
//$xhprof_runs = new XHProfRuns_Default();
//$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
//
//echo "http://localhost/xhprof_html/index.php?run={$run_id}&source=xhprof_testing\n";

Using apache AB tools benchmark it.
before:
螢幕快照 2014-04-16 0.02.58
after:
螢幕快照 2014-04-17 13.54.27
it only “Requests per second: 311.85 [#/sec] (mean)" we lose 1000% speed.
Phalcon it is die.

I put all testing file on github https://github.com/SDpower/phalcon-pj1 .

2 thoughts on “Using Twig with Phalcon PHP and kill it!!

  1. Did you install and enabled the Twig C Extension or have you just used the Twig composer package? I see you have also disabled the optimization extension.

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *