Development

/branches/1.0/lib/view/sfPHPView.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/view/sfPHPView.class.php

Revision 20606, 7.3 kB (checked in by fabien, 5 years ago)

[1.0, 1.2, 1.3] fixed a Strict Standards warning under PHP 5.3 (closes #6933)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
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 <sean@code-box.org>
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 <sean@code-box.org>
18  * @version    SVN: $Id$
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       $vars = $this->attributeHolder->getAll();
96       extract($vars);
97     }
98
99     if ($_escaping !== false)
100     {
101       $sf_data = sfOutputEscaper::escape($this->getEscapingMethod(), $this->attributeHolder->getAll());
102
103       if ($_escaping === 'both')
104       {
105         foreach ($sf_data as $_key => $_value)
106         {
107           ${$_key} = $_value;
108         }
109       }
110     }
111
112     // render
113     ob_start();
114     ob_implicit_flush(0);
115     require($_sfFile);
116
117     return ob_get_clean();
118   }
119
120   /**
121    * Retrieves the template engine associated with this view.
122    *
123    * Note: This will return null because PHP itself has no engine reference.
124    *
125    * @return null
126    */
127   public function getEngine()
128   {
129     return null;
130   }
131
132   /**
133    * Configures template.
134    *
135    * @return void
136    */
137   public function configure()
138   {
139     // store our current view
140     $actionStackEntry = $this->getContext()->getActionStack()->getLastEntry();
141     if (!$actionStackEntry->getViewInstance())
142     {
143       $actionStackEntry->setViewInstance($this);
144     }
145
146     // require our configuration
147     $viewConfigFile = $this->moduleName.'/'.sfConfig::get('sf_app_module_config_dir_name').'/view.yml';
148     require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$viewConfigFile));
149
150     // set template directory
151     if (!$this->directory)
152     {
153       $this->setDirectory(sfLoader::getTemplateDir($this->moduleName, $this->getTemplate()));
154     }
155   }
156
157   /**
158    * Loop through all template slots and fill them in with the results of
159    * presentation data.
160    *
161    * @param string A chunk of decorator content
162    *
163    * @return string A decorated template
164    */
165   protected function decorate($content)
166   {
167     $template = $this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate();
168
169     if (sfConfig::get('sf_logging_enabled'))
170     {
171       $this->getContext()->getLogger()->info('{sfView} decorate content with "'.$template.'"');
172     }
173
174     // set the decorator content as an attribute
175     $this->attributeHolder->set('sf_content', $content);
176
177     // for backwards compatibility with old layouts; remove at 0.8.0?
178     $this->attributeHolder->set('content', $content);
179
180     // render the decorator template and return the result
181     $retval = $this->renderFile($template);
182
183     return $retval;
184   }
185
186   /**
187    * Renders the presentation.
188    *
189    * When the controller render mode is sfView::RENDER_CLIENT, this method will
190    * render the presentation directly to the client and null will be returned.
191    *
192    * @return string A string representing the rendered presentation, if
193    *                the controller render mode is sfView::RENDER_VAR, otherwise null
194    */
195   public function render($templateVars = null)
196   {
197     $context = $this->getContext();
198
199     // get the render mode
200     $mode = $context->getController()->getRenderMode();
201
202     if ($mode == sfView::RENDER_NONE)
203     {
204       return null;
205     }
206
207     $retval = null;
208     $response = $context->getResponse();
209     if (sfConfig::get('sf_cache'))
210     {
211       $key   = $response->getParameterHolder()->remove('current_key', 'symfony/cache/current');
212       $cache = $response->getParameter($key, null, 'symfony/cache');
213       if ($cache !== null)
214       {
215         $cache  = unserialize($cache);
216         $retval = $cache['content'];
217         $vars   = $cache['vars'];
218         $response->mergeProperties($cache['response']);
219       }
220     }
221
222     // decorator
223     $layout = $response->getParameter($this->moduleName.'_'.$this->actionName.'_layout', null, 'symfony/action/view');
224     if (false === $layout)
225     {
226       $this->setDecorator(false);
227     }
228     else if (null !== $layout)
229     {
230       $this->setDecoratorTemplate($layout.$this->getExtension());
231     }
232
233     // template variables
234     if ($templateVars === null)
235     {
236       $actionInstance   = $context->getActionStack()->getLastEntry()->getActionInstance();
237       $templateVars     = $actionInstance->getVarHolder()->getAll();
238     }
239
240     // assigns some variables to the template
241     $this->attributeHolder->add($this->getGlobalVars());
242     $this->attributeHolder->add($retval !== null ? $vars : $templateVars);
243
244     // render template if no cache
245     if ($retval === null)
246     {
247       // execute pre-render check
248       $this->preRenderCheck();
249
250       // render template file
251       $template = $this->getDirectory().'/'.$this->getTemplate();
252       $retval = $this->renderFile($template);
253
254       if (sfConfig::get('sf_cache') && $key !== null)
255       {
256         $cache = array(
257           'content'   => $retval,
258           'vars'      => $templateVars,
259           'view_name' => $this->viewName,
260           'response'  => $context->getResponse(),
261         );
262         $response->setParameter($key, serialize($cache), 'symfony/cache');
263
264         if (sfConfig::get('sf_web_debug'))
265         {
266           $retval = sfWebDebug::getInstance()->decorateContentWithDebug($key, $retval, true);
267         }
268       }
269     }
270
271     // now render decorator template, if one exists
272     if ($this->isDecorator())
273     {
274       $retval = $this->decorate($retval);
275     }
276
277     // render to client
278     if ($mode == sfView::RENDER_CLIENT)
279     {
280       $context->getResponse()->setContent($retval);
281     }
282
283     return $retval;
284   }
285 }
286
Note: See TracBrowser for help on using the browser.