| [ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * This file is part of the symfony package. 5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 6 * (c) 2004-2006 Sean Kerr. 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 /** 13 * 14 * @package symfony 15 * @subpackage view 16 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 17 * @author Sean Kerr <skerr@mojavi.org> 18 * @version SVN: $Id: sfPHPView.class.php 3308 2007-01-20 05:48:11Z fabien $ 19 */ 20 class sfPHPView extends sfView 21 { 22 /** 23 * Executes any presentation logic for this view. 24 */ 25 public function execute() 26 { 27 } 28 29 /** 30 * Returns variables that will be accessible to the template. 31 * 32 * @return array Attributes from the template 33 */ 34 protected function getGlobalVars() 35 { 36 $context = $this->getContext(); 37 38 $shortcuts = array( 39 'sf_context' => $context, 40 'sf_params' => $context->getRequest()->getParameterHolder(), 41 'sf_request' => $context->getRequest(), 42 'sf_user' => $context->getUser(), 43 'sf_view' => $this, 44 ); 45 46 if (sfConfig::get('sf_use_flash')) 47 { 48 $sf_flash = new sfParameterHolder(); 49 $sf_flash->add($context->getUser()->getAttributeHolder()->getAll('symfony/flash')); 50 $shortcuts['sf_flash'] = $sf_flash; 51 } 52 53 return $shortcuts; 54 } 55 56 /** 57 * Load core and standard helpers to be use in the template. 58 */ 59 protected function loadCoreAndStandardHelpers() 60 { 61 static $coreHelpersLoaded = 0; 62 63 if ($coreHelpersLoaded) 64 { 65 return; 66 } 67 68 $coreHelpersLoaded = 1; 69 $core_helpers = array('Helper', 'Url', 'Asset', 'Tag', 'Escaping'); 70 $standard_helpers = sfConfig::get('sf_standard_helpers'); 71 72 $helpers = array_unique(array_merge($core_helpers, $standard_helpers)); 73 sfLoader::loadHelpers($helpers); 74 } 75 76 /** 77 * Renders the presentation. 78 * 79 * @param string Filename 80 * 81 * @return string File content 82 */ 83 protected function renderFile($_sfFile) 84 { 85 if (sfConfig::get('sf_logging_enabled')) 86 { 87 $this->getContext()->getLogger()->info('{sfView} render "'.$_sfFile.'"'); 88 } 89 90 $this->loadCoreAndStandardHelpers(); 91 92 $_escaping = $this->getEscaping(); 93 if ($_escaping === false || $_escaping === 'bc') 94 { 95 extract($this->attributeHolder->getAll()); 96 } 97 98 if ($_escaping !== false) 99 { 100 $sf_data = sfOutputEscaper::escape($this->getEscapingMethod(), $this->attributeHolder->getAll()); 101 102 if ($_escaping === 'both') 103 { 104 foreach ($sf_data as $_key => $_value) 105 { 106 ${$_key} = $_value; 107 } 108 } 109 } 110 111 // render 112 ob_start(); 113 ob_implicit_flush(0); 114 require($_sfFile); 115 116 return ob_get_clean(); 117 } 118 119 /** 120 * Retrieves the template engine associated with this view. 121 * 122 * Note: This will return null because PHP itself has no engine reference. 123 * 124 * @return null 125 */ 126 public function getEngine() 127 { 128 return null; 129 } 130 131 /** 132 * Configures template. 133 * 134 * @return void 135 */ 136 public function configure() 137 { 138 // store our current view 139 $actionStackEntry = $this->getContext()->getActionStack()->getLastEntry(); 140 if (!$actionStackEntry->getViewInstance()) 141 { 142 $actionStackEntry->setViewInstance($this); 143 } 144 145 // require our configuration 146 $viewConfigFile = $this->moduleName.'/'.sfConfig::get('sf_app_module_config_dir_name').'/view.yml'; 147 require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$viewConfigFile)); 148 149 // set template directory 150 if (!$this->directory) 151 { 152 $this->setDirectory(sfLoader::getTemplateDir($this->moduleName, $this->getTemplate())); 153 } 154 } 155 156 /** 157 * Loop through all template slots and fill them in with the results of 158 * presentation data. 159 * 160 * @param string A chunk of decorator content 161 * 162 * @return string A decorated template 163 */ 164 protected function decorate($content) 165 { 166 $template = $this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate(); 167 168 if (sfConfig::get('sf_logging_enabled')) 169 { 170 $this->getContext()->getLogger()->info('{sfView} decorate content with "'.$template.'"'); 171 } 172 173 // set the decorator content as an attribute 174 $this->attributeHolder->set('sf_content', $content); 175 176 // for backwards compatibility with old layouts; remove at 0.8.0? 177 $this->attributeHolder->set('content', $content); 178 179 // render the decorator template and return the result 180 $retval = $this->renderFile($template); 181 182 return $retval; 183 } 184 185 /** 186 * Renders the presentation. 187 * 188 * When the controller render mode is sfView::RENDER_CLIENT, this method will 189 * render the presentation directly to the client and null will be returned. 190 * 191 * @return string A string representing the rendered presentation, if 192 * the controller render mode is sfView::RENDER_VAR, otherwise null 193 */ 194 public function render($templateVars = null) 195 { 196 $context = $this->getContext(); 197 198 // get the render mode 199 $mode = $context->getController()->getRenderMode(); 200 201 if ($mode == sfView::RENDER_NONE) 202 { 203 return null; 204 } 205 206 $retval = null; 207 $response = $context->getResponse(); 208 if (sfConfig::get('sf_cache')) 209 { 210 $key = $response->getParameterHolder()->remove('current_key', 'symfony/cache/current'); 211 $cache = $response->getParameter($key, null, 'symfony/cache'); 212 if ($cache !== null) 213 { 214 $cache = unserialize($cache); 215 $retval = $cache['content']; 216 $vars = $cache['vars']; 217 $response->mergeProperties($cache['response']); 218 } 219 } 220 221 // decorator 222 $layout = $response->getParameter($this->moduleName.'_'.$this->actionName.'_layout', null, 'symfony/action/view'); 223 if (false === $layout) 224 { 225 $this->setDecorator(false); 226 } 227 else if (null !== $layout) 228 { 229 $this->setDecoratorTemplate($layout.$this->getExtension()); 230 } 231 232 // template variables 233 if ($templateVars === null) 234 { 235 $actionInstance = $context->getActionStack()->getLastEntry()->getActionInstance(); 236 $templateVars = $actionInstance->getVarHolder()->getAll(); 237 } 238 239 // assigns some variables to the template 240 $this->attributeHolder->add($this->getGlobalVars()); 241 $this->attributeHolder->add($retval !== null ? $vars : $templateVars); 242 243 // render template if no cache 244 if ($retval === null) 245 { 246 // execute pre-render check 247 $this->preRenderCheck(); 248 249 // render template file 250 $template = $this->getDirectory().'/'.$this->getTemplate(); 251 $retval = $this->renderFile($template); 252 253 if (sfConfig::get('sf_cache') && $key !== null) 254 { 255 $cache = array( 256 'content' => $retval, 257 'vars' => $templateVars, 258 'view_name' => $this->viewName, 259 'response' => $context->getResponse(), 260 ); 261 $response->setParameter($key, serialize($cache), 'symfony/cache'); 262 263 if (sfConfig::get('sf_web_debug')) 264 { 265 $retval = sfWebDebug::getInstance()->decorateContentWithDebug($key, $retval, true); 266 } 267 } 268 } 269 270 // now render decorator template, if one exists 271 if ($this->isDecorator()) 272 { 273 $retval = $this->decorate($retval); 274 } 275 276 // render to client 277 if ($mode == sfView::RENDER_CLIENT) 278 { 279 $context->getResponse()->setContent($retval); 280 } 281 282 return $retval; 283 } 284 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |