EzctemplateRenderer::createEngineInstance()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
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     Felix Weis <[email protected]>
27
 * @author     David Zülke <[email protected]>
28
 * @copyright  Authors
29
 * @copyright  The Agavi Project
30
 *
31
 * @since      0.11.0
32
 *
33
 * @version    $Id$
34
 */
35
class EzctemplateRenderer extends Renderer implements ReusableRendererInterface
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 = 'ezctemplate';
48
49
    /**
50
     * @constant   string The directory inside the cache dir where cached content
51
     *                    will be stored.
52
     */
53
    const CACHE_DIR = 'content';
54
55
    /**
56
     * @var        ezcTemplate The template engine instance.
57
     */
58
    protected $ezcTemplate = null;
59
60
    /**
61
     * @var        string A string with the default template file extension,
62
     *                    including the dot.
63
     */
64
    protected $defaultExtension = '.ezt';
65
66
    /**
67
     * Pre-serialization callback.
68
     *
69
     * Excludes the ezcTemplate instance to prevent excessive serialization load.
70
     *
71
     * @author     Felix Weis <[email protected]>
72
     * @since      0.11.0
73
     */
74
    public function __sleep()
75
    {
76
        $keys = parent::__sleep();
77
        unset($keys[array_search('ezctemplate', $keys)]);
78
        return $keys;
79
    }
80
    
81
    /**
82
     * Create an instance of ezcTemplate and initialize it correctly.
83
     * Returns an instance of EzctemplateTemplate by default.
84
     *
85
     * @return     ezcTemplate The ezcTemplate instance.
86
     *
87
     * @author     David Zülke <[email protected]>
88
     * @since      1.0.2
89
     */
90
    protected function createEngineInstance()
91
    {
92
        $cls = $this->getParameter('template_class', 'EzctemplateTemplate');
93
        
94
        $ezcTemplate = new $cls();
95
        if ($ezcTemplate instanceof AgaviIEzctemplateTemplate) {
0 ignored issues
show
Bug introduced by
The class Agavi\Renderer\AgaviIEzctemplateTemplate does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
96
            $ezcTemplate->setContext($this->context);
97
        }
98
        
99
        return $ezcTemplate;
100
    }
101
102
    /**
103
     * Grab a cleaned up ezctemplate instance.
104
     *
105
     * @return     ezcTemplate A ezcTemplate instance.
106
     *
107
     * @author     Felix Weis <[email protected]>
108
     * @since      0.11.0
109
     */
110
    protected function getEngine()
111
    {
112
        // ezcTemplate already initialized, only clear the assigns and retun the engine
113
        if ($this->ezcTemplate) {
114
            $this->ezcTemplate->send = new ezcTemplateVariableCollection();
115
            return $this->ezcTemplate;
116
        }
117
118
        $this->ezcTemplate = $this->createEngineInstance();
119
        // initialize ezcTemplate
120
        
121
        $parentMode = fileperms(Config::get('core.cache_dir'));
122
123
        $compileDir = Config::get('core.cache_dir') . DIRECTORY_SEPARATOR . self::COMPILE_DIR . DIRECTORY_SEPARATOR . self::COMPILE_SUBDIR;
124
        Toolkit::mkdir($compileDir, $parentMode, true);
125
126
        // templatePath unnessesary because Agavi will always supply the absolute ressource path
127
        $config = new ezcTemplateConfiguration();
128
        $config->templatePath = "";
129
        $config->compilePath = $compileDir;
130
131
        // set the ezcTemplateOutputContext (standard is ezcTemplateXhtmlContext)
132
        if ($this->hasParameter('context')) {
133
            $contextClass = $this->getParameter('context');
134
            $config->context = new $contextClass();
135
        }
136
137
        // add some usefull Agavi Functions/Blocks as Extension
138
        $config->addExtension('EzctemplateCustomBlocks');
139
        $config->addExtension('EzctemplateCustomFunctions');
140
        
141
        foreach ($this->getParameter('extensions', array()) as $extension) {
142
            $config->addExtension($extension);
143
        }
144
        
145
        $this->ezcTemplate->configuration = $config;
146
147
        return $this->ezcTemplate;
148
    }
149
150
    /**
151
     * Render the presentation and return the result.
152
     *
153
     * @param      TemplateLayer $layer The template layer to render.
154
     * @param      array         $attributes The template variables.
155
     * @param      array         $slots The slots.
156
     * @param      array         $moreAssigns Associative array of additional assigns.
157
     *
158
     * @return     string A rendered result.
159
     *
160
     * @author     Felix Weis <[email protected]>
161
     * @since      0.11.0
162
     */
163
    public function render(TemplateLayer $layer, array &$attributes = array(), array &$slots = array(), array &$moreAssigns = array())
164
    {
165
        $engine = $this->getEngine();
166
167
        if ($this->extractVars) {
168
            foreach ($attributes as $name => &$value) {
169
                $engine->send->{$name} = $value;
170
            }
171
        } else {
172
            $engine->send->{$this->varName} = $attributes;
173
        }
174
175
        $key = $this->slotsVarName;
176
        $engine->send->{$key} = $slots;
177
178
        foreach ($this->assigns as $key => $getter) {
179
            $engine->send->{$key} = $this->context->$getter();
180
        }
181
182
        $finalMoreAssigns =& self::buildMoreAssigns($moreAssigns, $this->moreAssignNames);
183
        foreach ($finalMoreAssigns as $key => &$value) {
184
            $engine->send->{$key} = $value;
185
        }
186
187
        return $engine->process($layer->getResourceStreamIdentifier());
188
    }
189
}
190