Renderer::__sleep()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 7
loc 7
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\Core\Context;
17
use Agavi\Exception\AgaviException;
18
use Agavi\Util\ParameterHolder;
19
use Agavi\View\TemplateLayer;
20
21
/**
22
 * A renderer produces the output as defined by a View
23
 *
24
 * @package    agavi
25
 * @subpackage renderer
26
 *
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
abstract class Renderer extends ParameterHolder
36
{
37
    /**
38
     * @var        Context An Context instance.
39
     */
40
    protected $context = null;
41
42
    /**
43
     * @var        string The context name.
44
     */
45
    protected $contextName = null;
46
    /**
47
     * @var        string A string with the default template file extension,
48
     *                    including the dot.
49
     */
50
    protected $defaultExtension = '';
51
    
52
    /**
53
     * @var        string The name of the array that contains the template vars.
54
     */
55
    protected $varName = 'template';
56
    
57
    /**
58
     * @var        string The name of the array that contains the slots output.
59
     */
60
    protected $slotsVarName = 'slots';
61
    
62
    /**
63
     * @var        bool Whether or not the template vars should be extracted.
64
     */
65
    protected $extractVars = false;
66
    
67
    /**
68
     * @var        array An array of objects to be exported for use in templates.
69
     */
70
    protected $assigns = array();
71
    
72
    /**
73
     * @var        array An array of names for the "more" assigns.
74
     */
75
    protected $moreAssignNames = array();
76
    
77
    /**
78
     * Pre-serialization callback.
79
     *
80
     * Will set the name of the context and exclude the instance from serializing.
81
     *
82
     * @author     David Zülke <[email protected]>
83
     * @since      0.11.0
84
     */
85 View Code Duplication
    public function __sleep()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
86
    {
87
        $this->contextName = $this->context->getName();
88
        $arr = get_object_vars($this);
89
        unset($arr['context']);
90
        return array_keys($arr);
91
    }
92
    
93
    /**
94
     * Post-unserialization callback.
95
     *
96
     * Will restore the context based on the names set by __sleep.
97
     *
98
     * @author     David Zülke <[email protected]>
99
     * @since      0.11.0
100
     */
101
    public function __wakeup()
102
    {
103
        $this->context = Context::getInstance($this->contextName);
104
        unset($this->contextName);
105
    }
106
    
107
    /**
108
     * Initialize this Renderer.
109
     *
110
     * @param      Context $context The current application context.
111
     * @param      array   $parameters An associative array of initialization parameters.
112
     *
113
     * @author     David Zülke <[email protected]>
114
     * @since      0.11.0
115
     */
116
    public function initialize(Context $context, array $parameters = array())
117
    {
118
        $this->context = $context;
119
        
120
        $this->setParameters($parameters);
121
        
122
        $this->varName = $this->getParameter('var_name', $this->varName);
123
        $this->slotsVarName = $this->getParameter('slots_var_name', $this->slotsVarName);
124
        $this->extractVars = $this->getParameter('extract_vars', $this->extractVars);
125
        
126
        $this->defaultExtension = $this->getParameter('default_extension', $this->defaultExtension);
127
        
128
        if (!$this->extractVars && $this->varName == $this->slotsVarName) {
129
            throw new AgaviException('Template and Slots container variable names cannot be identical.');
130
        }
131
        
132
        foreach ($this->getParameter('assigns', array()) as $item => $var) {
133
            $getter = 'get' . str_replace('_', '', $item);
134
            if (is_callable(array($this->context, $getter))) {
135
                if ($var === null) {
136
                    // the name is null, which means this one should not be assigned
137
                    // we do this in here, not for the moreAssignNames, since those are checked later in the renderer
138
                    continue;
139
                }
140
                $this->assigns[$var] = $getter;
141
            } else {
142
                $this->moreAssignNames[$item] = $var;
143
            }
144
        }
145
    }
146
    
147
    /**
148
     * Retrieve the current application context.
149
     *
150
     * @return     Context The current Context instance.
151
     *
152
     * @author     David Zülke <[email protected]>
153
     * @since      0.11.0
154
     */
155
    final public function getContext()
156
    {
157
        return $this->context;
158
    }
159
    
160
    /**
161
     * Get the template file extension
162
     *
163
     * @return     string The extension, including a leading dot.
164
     *
165
     * @author     David Zülke <[email protected]>
166
     * @since      0.11.0
167
     */
168
    public function getDefaultExtension()
169
    {
170
        return $this->defaultExtension;
171
    }
172
    
173
    /**
174
     * Build an array of "more" assigns.
175
     *
176
     * @param      array $moreAssigns The values to be assigned.
177
     * @param      array $moreAssignNames Assigns name map.
178
     *
179
     * @return     array The data.
180
     *
181
     * @author     David Zülke <[email protected]>
182
     * @since      1.0.0
183
     */
184
    protected static function &buildMoreAssigns(&$moreAssigns, $moreAssignNames)
185
    {
186
        $retval = array();
187
        
188
        foreach ($moreAssigns as $name => &$value) {
189
            if (isset($moreAssignNames[$name])) {
190
                $name = $moreAssignNames[$name];
191
            } elseif (array_key_exists($name, $moreAssignNames)) {
192
                // the name is null, which means this one should not be assigned
193
                continue;
194
            }
195
            $retval[$name] =& $value;
196
        }
197
        
198
        return $retval;
199
    }
200
    
201
    /**
202
     * Render the presentation and return the result.
203
     *
204
     * @param      TemplateLayer $layer The template layer to render.
205
     * @param      array         $attributes The template variables.
206
     * @param      array         $slots The slots.
207
     * @param      array         $moreAssigns Associative array of additional assigns.
208
     *
209
     * @return     string A rendered result.
210
     *
211
     * @author     David Zülke <[email protected]>
212
     * @since      0.11.0
213
     */
214
    abstract public function render(TemplateLayer $layer, array &$attributes = array(), array &$slots = array(), array &$moreAssigns = array());
215
}
216