Development

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

You must first sign up to be able to contribute.

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

Revision 9945, 4.8 kB (checked in by fabien, 6 years ago)

fixed 404 page when raised by a non existant route

  • 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     sfLoader::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     extract($this->attributeHolder->toArray());
73
74     // render
75     ob_start();
76     ob_implicit_flush(0);
77     require($_sfFile);
78
79     return ob_get_clean();
80   }
81
82   /**
83    * Retrieves the template engine associated with this view.
84    *
85    * Note: This will return null because PHP itself has no engine reference.
86    *
87    * @return null
88    */
89   public function getEngine()
90   {
91     return null;
92   }
93
94   /**
95    * Configures template.
96    *
97    * @return void
98    */
99   public function configure()
100   {
101     // store our current view
102     $this->context->set('view_instance', $this);
103
104     // require our configuration
105     require($this->context->getConfigCache()->checkConfig('modules/'.$this->moduleName.'/config/view.yml'));
106
107     // set template directory
108     if (!$this->directory)
109     {
110       $this->setDirectory($this->context->getConfiguration()->getTemplateDir($this->moduleName, $this->getTemplate()));
111     }
112   }
113
114   /**
115    * Loop through all template slots and fill them in with the results of presentation data.
116    *
117    * @param  string $content  A chunk of decorator content
118    *
119    * @return string A decorated template
120    */
121   protected function decorate($content)
122   {
123     if (sfConfig::get('sf_logging_enabled'))
124     {
125       $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Decorate content with "%s/%s"', $this->getDecoratorDirectory(), $this->getDecoratorTemplate()))));
126     }
127
128     // set the decorator content as an attribute
129     $attributeHolder = $this->attributeHolder;
130
131     $this->attributeHolder = $this->initializeAttributeHolder(array('sf_content' => new sfOutputEscaperSafe($content)));
132     $this->attributeHolder->set('sf_type', 'layout');
133
134     // render the decorator template and return the result
135     $ret = $this->renderFile($this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate());
136
137     $this->attributeHolder = $attributeHolder;
138
139     return $ret;
140   }
141
142   /**
143    * Renders the presentation.
144    *
145    * @return string A string representing the rendered presentation
146    */
147   public function render()
148   {
149     $content = null;
150     if (sfConfig::get('sf_cache'))
151     {
152       $viewCache = $this->context->getViewCacheManager();
153       $uri = $this->context->getRouting()->getCurrentInternalUri();
154
155       if (!is_null($uri))
156       {
157         list($content, $decoratorTemplate) = $viewCache->getActionCache($uri);
158         if (!is_null($content))
159         {
160           $this->setDecoratorTemplate($decoratorTemplate);
161         }
162       }
163     }
164
165     // render template if no cache
166     if (is_null($content))
167     {
168       // execute pre-render check
169       $this->preRenderCheck();
170
171       $this->attributeHolder->set('sf_type', 'action');
172
173       // render template file
174       $content = $this->renderFile($this->getDirectory().'/'.$this->getTemplate());
175
176       if (sfConfig::get('sf_cache') && !is_null($uri))
177       {
178         $content = $viewCache->setActionCache($uri, $content, $this->isDecorator() ? $this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate() : false);
179       }
180     }
181
182     // now render decorator template, if one exists
183     if ($this->isDecorator())
184     {
185       $content = $this->decorate($content);
186     }
187
188     return $content;
189   }
190 }
191
Note: See TracBrowser for help on using the browser.