Development

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

You must first sign up to be able to contribute.

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

Revision 24511, 5.0 kB (checked in by FabianLange, 5 years ago)

[1.2, 1.3, 1.4] correctly closing output buffering in case of exceptions while requiring a file in sfPHPView (fixes #7596)

  • 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  * A view that uses PHP as the templating engine.
14  *
15  * @package    symfony
16  * @subpackage view
17  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
18  * @author     Sean Kerr <sean@code-box.org>
19  * @version    SVN: $Id$
20  */
21 class sfPHPView extends sfView
22 {
23   /**
24    * Executes any presentation logic for this view.
25    */
26   public function execute()
27   {
28   }
29
30   /**
31    * Loads core and standard helpers to be use in the template.
32    */
33   protected function loadCoreAndStandardHelpers()
34   {
35     static $coreHelpersLoaded = 0;
36
37     if ($coreHelpersLoaded)
38     {
39       return;
40     }
41
42     $coreHelpersLoaded = 1;
43
44     $helpers = array_unique(array_merge(array('Helper', 'Url', 'Asset', 'Tag', 'Escaping'), sfConfig::get('sf_standard_helpers')));
45
46     // remove default Form helper if compat_10 is false
47     if (!sfConfig::get('sf_compat_10') && false !== $i = array_search('Form', $helpers))
48     {
49       unset($helpers[$i]);
50     }
51
52     $this->context->getConfiguration()->loadHelpers($helpers);
53   }
54
55   /**
56    * Renders the presentation.
57    *
58    * @param  string $_sfFile  Filename
59    *
60    * @return string File content
61    */
62   protected function renderFile($_sfFile)
63   {
64     if (sfConfig::get('sf_logging_enabled'))
65     {
66       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Render "%s"', $_sfFile))));
67     }
68
69     $this->loadCoreAndStandardHelpers();
70
71     // EXTR_REFS can't be used (see #3595 and #3151)
72     $vars = $this->attributeHolder->toArray();
73     extract($vars);
74
75     // render
76     ob_start();
77     ob_implicit_flush(0);
78
79     try
80     {
81       require($_sfFile);
82     }
83     catch (Exception $e)
84     {
85       // need to end output buffering before throwing the exception #7596
86       ob_end_clean();
87       throw $e;
88     }
89
90     return ob_get_clean();
91   }
92
93   /**
94    * Retrieves the template engine associated with this view.
95    *
96    * Note: This will return null because PHP itself has no engine reference.
97    *
98    * @return null
99    */
100   public function getEngine()
101   {
102     return null;
103   }
104
105   /**
106    * Configures template.
107    *
108    * @return void
109    */
110   public function configure()
111   {
112     // store our current view
113     $this->context->set('view_instance', $this);
114
115     // require our configuration
116     require($this->context->getConfigCache()->checkConfig('modules/'.$this->moduleName.'/config/view.yml'));
117
118     // set template directory
119     if (!$this->directory)
120     {
121       $this->setDirectory($this->context->getConfiguration()->getTemplateDir($this->moduleName, $this->getTemplate()));
122     }
123   }
124
125   /**
126    * Loop through all template slots and fill them in with the results of presentation data.
127    *
128    * @param  string $content  A chunk of decorator content
129    *
130    * @return string A decorated template
131    */
132   protected function decorate($content)
133   {
134     if (sfConfig::get('sf_logging_enabled'))
135     {
136       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Decorate content with "%s/%s"', $this->getDecoratorDirectory(), $this->getDecoratorTemplate()))));
137     }
138
139     // set the decorator content as an attribute
140     $attributeHolder = $this->attributeHolder;
141
142     $this->attributeHolder = $this->initializeAttributeHolder(array('sf_content' => new sfOutputEscaperSafe($content)));
143     $this->attributeHolder->set('sf_type', 'layout');
144
145     // render the decorator template and return the result
146     $ret = $this->renderFile($this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate());
147
148     $this->attributeHolder = $attributeHolder;
149
150     return $ret;
151   }
152
153   /**
154    * Renders the presentation.
155    *
156    * @return string A string representing the rendered presentation
157    */
158   public function render()
159   {
160     $content = null;
161     if (sfConfig::get('sf_cache'))
162     {
163       $viewCache = $this->context->getViewCacheManager();
164       $uri = $this->context->getRouting()->getCurrentInternalUri();
165
166       if (!is_null($uri))
167       {
168         list($content, $decoratorTemplate) = $viewCache->getActionCache($uri);
169         if (!is_null($content))
170         {
171           $this->setDecoratorTemplate($decoratorTemplate);
172         }
173       }
174     }
175
176     // render template if no cache
177     if (is_null($content))
178     {
179       // execute pre-render check
180       $this->preRenderCheck();
181
182       $this->attributeHolder->set('sf_type', 'action');
183
184       // render template file
185       $content = $this->renderFile($this->getDirectory().'/'.$this->getTemplate());
186
187       if (sfConfig::get('sf_cache') && !is_null($uri))
188       {
189         $content = $viewCache->setActionCache($uri, $content, $this->isDecorator() ? $this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate() : false);
190       }
191     }
192
193     // now render decorator template, if one exists
194     if ($this->isDecorator())
195     {
196       $content = $this->decorate($content);
197     }
198
199     return $content;
200   }
201 }
202
Note: See TracBrowser for help on using the browser.