PhptalRenderer::__sleep()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Agavi\Renderer;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// |                                                                           |
8
// | For the full copyright and license information, please view the LICENSE   |
9
// | file that was distributed with this source code. You can also view the    |
10
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
11
// |   vi: set noexpandtab:                                                    |
12
// |   Local Variables:                                                        |
13
// |   indent-tabs-mode: t                                                     |
14
// |   End:                                                                    |
15
// +---------------------------------------------------------------------------+
16
use Agavi\Config\Config;
17
use Agavi\Util\Toolkit;
18
use Agavi\View\TemplateLayer;
19
20
/**
21
 * A renderer produces the output as defined by a View
22
 *
23
 * @package    agavi
24
 * @subpackage renderer
25
 *
26
 * @author     David Zülke <[email protected]>
27
 * @author     Benjamin Muskalla <[email protected]>
28
 * @copyright  Authors
29
 * @copyright  The Agavi Project
30
 *
31
 * @since      0.11.0
32
 *
33
 * @version    $Id$
34
 */
35
class PhptalRenderer extends Renderer
36
{
37
    /**
38
     * @constant   string The directory inside the cache dir where templates will
39
     *                    be stored in compiled form.
40
     */
41
    const COMPILE_DIR = 'templates';
42
    
43
    /**
44
     * @constant   string The subdirectory inside the compile dir where templates
45
     *                    will be stored in compiled form.
46
     */
47
    const COMPILE_SUBDIR = 'phptal';
48
    
49
    /**
50
     * @var        string A string with the default template file extension,
51
     *                    including the dot.
52
     */
53
    protected $defaultExtension = '.tal';
54
55
    /**
56
     * @var        PHPTAL PHPTAL template engine.
57
     */
58
    protected $phptal = null;
59
60
    /**
61
     * Pre-serialization callback.
62
     *
63
     * Excludes the PHPTAL instance to prevent excessive serialization load.
64
     *
65
     * @author     David Zülke <[email protected]>
66
     * @since      0.11.0
67
     */
68
    public function __sleep()
69
    {
70
        $keys = parent::__sleep();
71
        unset($keys[array_search('phptal', $keys)]);
72
        return $keys;
73
    }
74
    
75
    /**
76
     * Create an instance of PHPTAL and initialize it correctly.
77
     *
78
     * @return     PHPTAL The PHPTAL instance.
79
     *
80
     * @author     David Zülke <[email protected]>
81
     * @since      1.0.2
82
     */
83
    protected function createEngineInstance()
84
    {
85
        $phptalPhpCodeDestination = Config::get('core.cache_dir') . DIRECTORY_SEPARATOR . PhptalRenderer::COMPILE_DIR . DIRECTORY_SEPARATOR . PhptalRenderer::COMPILE_SUBDIR . DIRECTORY_SEPARATOR;
86
        
87
        // we keep this for < 1.2
88
        if (!defined('PHPTAL_PHP_CODE_DESTINATION')) {
89
            define('PHPTAL_PHP_CODE_DESTINATION', $phptalPhpCodeDestination);
90
        }
91
        
92
        Toolkit::mkdir($phptalPhpCodeDestination, fileperms(Config::get('core.cache_dir')), true);
93
        
94
        if (!class_exists('PHPTAL')) {
95
            require('PHPTAL.php');
96
        }
97
        
98
        $phptal = new PHPTAL();
99
        
100
        if (version_compare(PHPTAL_VERSION, '1.2', 'ge')) {
101
            $phptal->setPhpCodeDestination($phptalPhpCodeDestination);
102
        } else {
103
            trigger_error('Support for PHPTAL versions older than 1.2 is deprecated and will be removed in Agavi 1.2.', E_USER_DEPRECATED);
104
        }
105
        
106
        if ($this->hasParameter('encoding')) {
107
            $phptal->setEncoding($this->getParameter('encoding'));
108
        }
109
        
110
        return $phptal;
111
    }
112
113
    /**
114
     * Retrieve the PHPTAL instance
115
     *
116
     * @return     PHPTAL A PHPTAL instance.
117
     *
118
     * @author     David Zülke <[email protected]>
119
     * @author     Benjamin Muskalla <[email protected]>
120
     * @since      0.11.0
121
     */
122
    protected function getEngine()
123
    {
124
        if ($this->phptal) {
125
            return $this->phptal;
126
        }
127
        
128
        $this->phptal = $this->createEngineInstance();
129
        
130
        return $this->phptal;
131
    }
132
133
    /**
134
     * Render the presentation and return the result.
135
     *
136
     * @param      TemplateLayer $layer The template layer to render.
137
     * @param      array         $attributes The template variables.
138
     * @param      array         $slots The slots.
139
     * @param      array         $moreAssigns Associative array of additional assigns.
140
     *
141
     * @return     string A rendered result.
142
     *
143
     * @author     David Zülke <[email protected]>
144
     * @author     Benjamin Muskalla <[email protected]>
145
     * @since      0.11.0
146
     */
147
    public function render(TemplateLayer $layer, array &$attributes = array(), array &$slots = array(), array &$moreAssigns = array())
148
    {
149
        $engine = $this->getEngine();
150
        
151 View Code Duplication
        if ($this->extractVars) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
            foreach ($attributes as $key => $value) {
153
                $engine->set($key, $value);
154
            }
155
        } else {
156
            $engine->set($this->varName, $attributes);
157
        }
158
        
159
        $engine->set($this->slotsVarName, $slots);
160
        
161
        foreach ($this->assigns as $key => $getter) {
162
            $engine->set($key, $this->context->$getter());
163
        }
164
        
165
        $finalMoreAssigns =& self::buildMoreAssigns($moreAssigns, $this->moreAssignNames);
166
        foreach ($finalMoreAssigns as $key => &$value) {
167
            $engine->set($key, $value);
168
        }
169
        
170
        $engine->setTemplate($layer->getResourceStreamIdentifier());
171
        
172
        return $engine->execute();
173
    }
174
}
175