| 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 renderer produces the output as defined by a View |
|---|
| 18 | * |
|---|
| 19 | * @package agavi |
|---|
| 20 | * @subpackage renderer |
|---|
| 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 | */ |
|---|
| 30 | abstract class AgaviRenderer extends AgaviParameterHolder |
|---|
| 31 | { |
|---|
| 32 | /** |
|---|
| 33 | * @var AgaviContext An AgaviContext instance. |
|---|
| 34 | */ |
|---|
| 35 | protected $context = null; |
|---|
| 36 | |
|---|
| 37 | /** |
|---|
| 38 | * @var string A string with the default template file extension, |
|---|
| 39 | * including the dot. |
|---|
| 40 | */ |
|---|
| 41 | protected $defaultExtension = ''; |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * @var string The name of the array that contains the template vars. |
|---|
| 45 | */ |
|---|
| 46 | protected $varName = 'template'; |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * @var string The name of the array that contains the slots output. |
|---|
| 50 | */ |
|---|
| 51 | protected $slotsVarName = 'slots'; |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * @var bool Whether or not the template vars should be extracted. |
|---|
| 55 | */ |
|---|
| 56 | protected $extractVars = false; |
|---|
| 57 | |
|---|
| 58 | /** |
|---|
| 59 | * @var array An array of objects to be exported for use in templates. |
|---|
| 60 | */ |
|---|
| 61 | protected $assigns = array(); |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * @var array An array of names for the "more" assigns. |
|---|
| 65 | */ |
|---|
| 66 | protected $moreAssignNames = array(); |
|---|
| 67 | |
|---|
| 68 | /** |
|---|
| 69 | * Pre-serialization callback. |
|---|
| 70 | * |
|---|
| 71 | * Will set the name of the context and exclude the instance from serializing. |
|---|
| 72 | * |
|---|
| 73 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 74 | * @since 0.11.0 |
|---|
| 75 | */ |
|---|
| 76 | public function __sleep() |
|---|
| 77 | { |
|---|
| 78 | $this->contextName = $this->context->getName(); |
|---|
| 79 | $arr = get_object_vars($this); |
|---|
| 80 | unset($arr['context']); |
|---|
| 81 | return array_keys($arr); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** |
|---|
| 85 | * Post-unserialization callback. |
|---|
| 86 | * |
|---|
| 87 | * Will restore the context based on the names set by __sleep. |
|---|
| 88 | * |
|---|
| 89 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 90 | * @since 0.11.0 |
|---|
| 91 | */ |
|---|
| 92 | public function __wakeup() |
|---|
| 93 | { |
|---|
| 94 | $this->context = AgaviContext::getInstance($this->contextName); |
|---|
| 95 | unset($this->contextName); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Initialize this Renderer. |
|---|
| 100 | * |
|---|
| 101 | * @param AgaviContext The current application context. |
|---|
| 102 | * @param array An associative array of initialization parameters. |
|---|
| 103 | * |
|---|
| 104 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 105 | * @since 0.11.0 |
|---|
| 106 | */ |
|---|
| 107 | public function initialize(AgaviContext $context, array $parameters = array()) |
|---|
| 108 | { |
|---|
| 109 | $this->context = $context; |
|---|
| 110 | |
|---|
| 111 | $this->setParameters($parameters); |
|---|
| 112 | |
|---|
| 113 | $this->varName = $this->getParameter('var_name', $this->varName); |
|---|
| 114 | $this->slotsVarName = $this->getParameter('slots_var_name', $this->slotsVarName); |
|---|
| 115 | $this->extractVars = $this->getParameter('extract_vars', $this->extractVars); |
|---|
| 116 | |
|---|
| 117 | $this->defaultExtension = $this->getParameter('default_extension', $this->defaultExtension); |
|---|
| 118 | |
|---|
| 119 | if(!$this->extractVars && $this->varName == $this->slotsVarName) { |
|---|
| 120 | throw new AgaviException('Template and Slots container variable names cannot be identical.'); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | foreach($this->getParameter('assigns', array()) as $item => $var) { |
|---|
| 124 | $getter = 'get' . str_replace('_', '', $item); |
|---|
| 125 | if(method_exists($this->context, $getter)) { |
|---|
| 126 | $this->assigns[$var] = $getter; |
|---|
| 127 | } else { |
|---|
| 128 | $this->moreAssignNames[$item] = $var; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * Retrieve the current application context. |
|---|
| 135 | * |
|---|
| 136 | * @return AgaviContext The current AgaviContext instance. |
|---|
| 137 | * |
|---|
| 138 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 139 | * @since 0.11.0 |
|---|
| 140 | */ |
|---|
| 141 | public final function getContext() |
|---|
| 142 | { |
|---|
| 143 | return $this->context; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * Get the template file extension |
|---|
| 148 | * |
|---|
| 149 | * @return string The extension, including a leading dot. |
|---|
| 150 | * |
|---|
| 151 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 152 | * @since 0.11.0 |
|---|
| 153 | */ |
|---|
| 154 | public function getDefaultExtension() |
|---|
| 155 | { |
|---|
| 156 | return $this->defaultExtension; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | /** |
|---|
| 160 | * Render the presentation and return the result. |
|---|
| 161 | * |
|---|
| 162 | * @param AgaviTemplateLayer The template layer to render. |
|---|
| 163 | * @param array The template variables. |
|---|
| 164 | * @param array The slots. |
|---|
| 165 | * @param array Associative array of additional assigns. |
|---|
| 166 | * |
|---|
| 167 | * @return string A rendered result. |
|---|
| 168 | * |
|---|
| 169 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 170 | * @since 0.11.0 |
|---|
| 171 | */ |
|---|
| 172 | abstract public function render(AgaviTemplateLayer $layer, array &$attributes = array(), array &$slots = array(), array &$moreAssigns = array()); |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | ?> |
|---|