sfPHPView.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * A view that uses PHP as the templating engine.
  12. *
  13. * @package symfony
  14. * @subpackage view
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @author Sean Kerr <sean@code-box.org>
  17. * @version SVN: $Id: sfPHPView.class.php 11783 2008-09-25 16:21:27Z fabien $
  18. */
  19. class sfPHPView extends sfView
  20. {
  21. /**
  22. * Executes any presentation logic for this view.
  23. */
  24. public function execute()
  25. {
  26. }
  27. /**
  28. * Loads core and standard helpers to be use in the template.
  29. */
  30. protected function loadCoreAndStandardHelpers()
  31. {
  32. static $coreHelpersLoaded = 0;
  33. if ($coreHelpersLoaded)
  34. {
  35. return;
  36. }
  37. $coreHelpersLoaded = 1;
  38. $helpers = array_unique(array_merge(array('Helper', 'Url', 'Asset', 'Tag', 'Escaping'), sfConfig::get('sf_standard_helpers')));
  39. // remove default Form helper if compat_10 is false
  40. if (!sfConfig::get('sf_compat_10') && false !== $i = array_search('Form', $helpers))
  41. {
  42. unset($helpers[$i]);
  43. }
  44. $this->context->getConfiguration()->loadHelpers($helpers);
  45. }
  46. /**
  47. * Renders the presentation.
  48. *
  49. * @param string $_sfFile Filename
  50. *
  51. * @return string File content
  52. */
  53. protected function renderFile($_sfFile)
  54. {
  55. if (sfConfig::get('sf_logging_enabled'))
  56. {
  57. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Render "%s"', $_sfFile))));
  58. }
  59. $this->loadCoreAndStandardHelpers();
  60. // EXTR_REFS can't be used (see #3595 and #3151)
  61. extract($this->attributeHolder->toArray());
  62. // render
  63. ob_start();
  64. ob_implicit_flush(0);
  65. require($_sfFile);
  66. return ob_get_clean();
  67. }
  68. /**
  69. * Retrieves the template engine associated with this view.
  70. *
  71. * Note: This will return null because PHP itself has no engine reference.
  72. *
  73. * @return null
  74. */
  75. public function getEngine()
  76. {
  77. return null;
  78. }
  79. /**
  80. * Configures template.
  81. *
  82. * @return void
  83. */
  84. public function configure()
  85. {
  86. // store our current view
  87. $this->context->set('view_instance', $this);
  88. // require our configuration
  89. require($this->context->getConfigCache()->checkConfig('modules/'.$this->moduleName.'/config/view.yml'));
  90. // set template directory
  91. if (!$this->directory)
  92. {
  93. $this->setDirectory($this->context->getConfiguration()->getTemplateDir($this->moduleName, $this->getTemplate()));
  94. }
  95. }
  96. /**
  97. * Loop through all template slots and fill them in with the results of presentation data.
  98. *
  99. * @param string $content A chunk of decorator content
  100. *
  101. * @return string A decorated template
  102. */
  103. protected function decorate($content)
  104. {
  105. if (sfConfig::get('sf_logging_enabled'))
  106. {
  107. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Decorate content with "%s/%s"', $this->getDecoratorDirectory(), $this->getDecoratorTemplate()))));
  108. }
  109. // set the decorator content as an attribute
  110. $attributeHolder = $this->attributeHolder;
  111. $this->attributeHolder = $this->initializeAttributeHolder(array('sf_content' => new sfOutputEscaperSafe($content)));
  112. $this->attributeHolder->set('sf_type', 'layout');
  113. // render the decorator template and return the result
  114. $ret = $this->renderFile($this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate());
  115. $this->attributeHolder = $attributeHolder;
  116. return $ret;
  117. }
  118. /**
  119. * Renders the presentation.
  120. *
  121. * @return string A string representing the rendered presentation
  122. */
  123. public function render()
  124. {
  125. $content = null;
  126. if (sfConfig::get('sf_cache'))
  127. {
  128. $viewCache = $this->context->getViewCacheManager();
  129. $uri = $this->context->getRouting()->getCurrentInternalUri();
  130. if (!is_null($uri))
  131. {
  132. list($content, $decoratorTemplate) = $viewCache->getActionCache($uri);
  133. if (!is_null($content))
  134. {
  135. $this->setDecoratorTemplate($decoratorTemplate);
  136. }
  137. }
  138. }
  139. // render template if no cache
  140. if (is_null($content))
  141. {
  142. // execute pre-render check
  143. $this->preRenderCheck();
  144. $this->attributeHolder->set('sf_type', 'action');
  145. // render template file
  146. $content = $this->renderFile($this->getDirectory().'/'.$this->getTemplate());
  147. if (sfConfig::get('sf_cache') && !is_null($uri))
  148. {
  149. $content = $viewCache->setActionCache($uri, $content, $this->isDecorator() ? $this->getDecoratorDirectory().'/'.$this->getDecoratorTemplate() : false);
  150. }
  151. }
  152. // now render decorator template, if one exists
  153. if ($this->isDecorator())
  154. {
  155. $content = $this->decorate($content);
  156. }
  157. return $content;
  158. }
  159. }