root/branches/0.11/src/view/AgaviTemplateLayer.class.php

Revision 2258, 7.6 KB (checked in by david, 11 months ago)

bumped and fixed copyright years, closes #664

  • Property svn:keywords set to Id
Line 
1<?php
2
3// +---------------------------------------------------------------------------+
4// | This file is part of the Agavi package.                                   |
5// | Copyright (c) 2005-2008 the Agavi Project.                                |
6// |                                                                           |
7// | For the full copyright and license information, please view the LICENSE   |
8// | file that was distributed with this source code. You can also view the    |
9// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
10// |   vi: set noexpandtab:                                                    |
11// |   Local Variables:                                                        |
12// |   indent-tabs-mode: t                                                     |
13// |   End:                                                                    |
14// +---------------------------------------------------------------------------+
15
16/**
17 * A template layer wraps information necessary to render a template.
18 *
19 * @package    agavi
20 * @subpackage view
21 *
22 * @author     David Zülke <dz@bitxtender.com>
23 * @copyright  Authors
24 * @copyright  The Agavi Project
25 *
26 * @since      0.11.0
27 *
28 * @version    $Id$
29 */
30abstract class AgaviTemplateLayer extends AgaviParameterHolder
31{
32  /**
33   * @var        AgaviContext The current Context.
34   */
35  protected $context = null;
36 
37  /**
38   * @var        AgaviRenderer The Renderer instance to be used for this layer.
39   */
40  protected $renderer = null;
41 
42  /**
43   * @var        array An associative array of execution containers for slots.
44   */
45  protected $slots = array();
46 
47  /**
48   * Constructor.
49   *
50   * @param      array Initial parameters.
51   *
52   * @author     David Zülke <dz@bitxtender.com>
53   * @since      0.11.0
54   */
55  public function __construct(array $parameters = array())
56  {
57    parent::__construct(array_merge($parameters, array(
58      'module' => null,
59      'template' => null,
60    )));
61  }
62 
63  /**
64   * Convenience overload for accessing parameters using a method.
65   *
66   * @param      string The method name.
67   * @param      array  The method arguments.
68   *
69   * @author     David Zülke <dz@bitxtender.com>
70   * @since      0.11.0
71   */
72  public function __call($name, array $args)
73  {
74    $matches = array();
75    if(preg_match('/^(has|get|set|remove)(.+)$/', $name, $matches)) {
76      $method = $matches[1] . 'Parameter';
77      // transform "FooBarBaz" (from "setTemplateDir" etc) to "foo_bar_baz"
78      $parameter = strtolower(preg_replace('/((?<!\A)[A-Z])/u', '_$1', $matches[2]));
79      return call_user_func_array(array($this, $method), array_merge(array($parameter), $args));
80    }
81  }
82 
83  /**
84   * Pre-serialization callback.
85   *
86   * Will set the name of the context and exclude the instance from serializing.
87   *
88   * @author     David Zülke <dz@bitxtender.com>
89   * @since      0.11.0
90   */
91  public function __sleep()
92  {
93    $this->contextName = $this->context->getName();
94    $arr = get_object_vars($this);
95    unset($arr['context']);
96    return array_keys($arr);
97  }
98 
99  /**
100   * Post-unserialization callback.
101   *
102   * Will restore the context based on the names set by __sleep.
103   *
104   * @author     David Zülke <dz@bitxtender.com>
105   * @since      0.11.0
106   */
107  public function __wakeup()
108  {
109    $this->context = AgaviContext::getInstance($this->contextName);
110    unset($this->contextName);
111  }
112 
113  /**
114   * Object cloning callback.
115   *
116   * Will clone each individual slot (which are execution containers).
117   *
118   * @author     David Zülke <dz@bitxtender.com>
119   * @since      0.11.0
120   */
121  public function __clone()
122  {
123    foreach($this->slots as &$slot) {
124      $slot = clone $slot;
125    }
126  }
127 
128  /**
129   * A convenience function that renders all slots and then the main template.
130   * Useful in your custom models to render an email, for example.
131   *
132   * @param      AgaviRenderer An optional renderer instance that will be used
133   *                           instead of the one set on the layer.
134   *
135   * @return     string The rendered result.
136   *
137   * @author     David Zülke <dz@bitxtender.com>
138   * @since      0.11.0
139   */
140  public function execute(AgaviRenderer $renderer = null, array &$attributes = array(), array &$moreAssigns = array())
141  {
142    $output = array();
143   
144    foreach($this->getSlots() as $slotName => $slotContainer) {
145      $slotResponse = $slotContainer->execute();
146      $output[$slotName] = $slotResponse->getContent();
147    }
148   
149    if($renderer === null) {
150      $renderer = $this->getRenderer();
151    }
152   
153    if(!($renderer instanceof AgaviRenderer)) {
154      throw new AgaviException('No renderer has been set or given.');
155    }
156   
157    return $renderer->render($this, $attributes, $output, $moreAssigns);
158  }
159 
160  /**
161   * Initialize the layer.
162   *
163   * @param      AgaviContext The current Context instance.
164   * @param      array        An array of initialization parameters.
165   *
166   * @author     David Zülke <dz@bitxtender.com>
167   * @since      0.11.0
168   */
169  public function initialize(AgaviContext $context, array $parameters = array())
170  {
171    $this->context = $context;
172   
173    $this->setParameters($parameters);
174  }
175 
176  /**
177   * Set a renderer instance to use for this layer.
178   *
179   * @param      AgaviRenderer A renderer instance.
180   *
181   * @author     David Zülke <dz@bitxtender.com>
182   * @since      0.11.0
183   */
184  public function setRenderer(AgaviRenderer $renderer)
185  {
186    $this->renderer = $renderer;
187  }
188 
189  /**
190   * Get the renderer instance used for this layer.
191   *
192   * @return     AgaviRenderer A renderer instance.
193   *
194   * @author     David Zülke <dz@bitxtender.com>
195   * @since      0.11.0
196   */
197  public function getRenderer()
198  {
199    return $this->renderer;
200  }
201 
202  /**
203   * Set a slot that is rendered along with and available inside this layer.
204   *
205   * @param      string                  The name of the slot.
206   * @param      AgaviExecutionContainer The slot's execution container.
207   *
208   * @author     David Zülke <dz@bitxtender.com>
209   * @since      0.11.0
210   */
211  public function setSlot($name, AgaviExecutionContainer $c)
212  {
213    $this->slots[$name] = $c;
214  }
215 
216  /**
217   * Get the execution container for a slot.
218   *
219   * @param      string The name of the slot.
220   *
221   * @return     AgaviExecutionContainer The slot's execution container.
222   *
223   * @author     David Zülke <dz@bitxtender.com>
224   * @since      0.11.0
225   */
226  public function getSlot($name)
227  {
228    if(isset($this->slots[$name])) {
229      return $this->slots[$name];
230    }
231  }
232 
233  /**
234   * Get all slots.
235   *
236   * @return     array An associative array of slot names and exec containers.
237   *
238   * @author     David Zülke <dz@bitxtender.com>
239   * @since      0.11.0
240   */
241  public function getSlots()
242  {
243    return $this->slots;
244  }
245 
246  /**
247   * Check whether or not a slot has been set.
248   *
249   * @param      string The name of the slot.
250   *
251   * @return     bool True if the slot exists, false otherwise.
252   *
253   * @author     David Zülke <dz@bitxtender.com>
254   * @since      0.11.0
255   */
256  public function hasSlot($name)
257  {
258    return isset($this->slots[$name]);
259  }
260 
261  /**
262   * Check if any slots have been set.
263   *
264   * @return     bool true if any slots are defined, falseotherwise.
265   *
266   * @author     David Zülke <dz@bitxtender.com>
267   * @since      0.11.0
268   */
269  public function hasSlots()
270  {
271    return (count($this->slots) > 0);
272  }
273 
274  /**
275   * Remove a slot.
276   *
277   * @param      string The name of the slot.
278   *
279   * @author     David Zülke <dz@bitxtender.com>
280   * @since      0.11.0
281   */
282  public function removeSlot($name)
283  {
284    if(isset($this->slots[$name])) {
285      unset($this->slots[$name]);
286    }
287  }
288 
289  /**
290   * Get the full, resolved stream location name to the template resource.
291   *
292   * @return     string A PHP stream resource identifier.
293   *
294   * @throws     AgaviException If the template could not be found.
295   *
296   * @author     David Zülke <dz@bitxtender.com>
297   * @since      0.11.0
298   */
299  abstract public function getResourceStreamIdentifier();
300}
301
302?>
Note: See TracBrowser for help on using the browser.