PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/object/task/SmartyRendererTask.class.php

https://bitbucket.org/stk2k/charcoalphp2.1
PHP | 272 lines | 156 code | 49 blank | 67 comment | 12 complexity | 2077d324de823b90e011482a7e2d569c MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty renderer task
  4. *
  5. * PHP version 5
  6. *
  7. * @package objects.tasks
  8. * @author CharcoalPHP Development Team
  9. * @copyright 2008 stk2k, sazysoft
  10. */
  11. /** @noinspection PhpIncludeInspection */
  12. require_once( 'Smarty/Smarty.class.php' );
  13. class Charcoal_SmartyRendererTask extends Charcoal_Task implements Charcoal_ITask
  14. {
  15. const TAG = 'smarty_renderer_task';
  16. private $template_files;
  17. private $smarty;
  18. /**
  19. * Constructor
  20. */
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->template_files = array();
  25. $this->smarty = new Smarty();
  26. log_debug( "smarty", "smarty=" . spl_object_hash($this->smarty), self::TAG );
  27. }
  28. /**
  29. * Initialize instance
  30. *
  31. * @param array $config configuration data
  32. */
  33. public function configure( $config )
  34. {
  35. parent::configure( $config );
  36. $config = new Charcoal_ConfigPropertySet( $this->getSandbox()->getEnvironment(), $config );
  37. $this->smarty->caching = 0; //$config->getBoolean( 'caching' )->unbox();
  38. $this->smarty->compile_check = ub( $config->getBoolean( 'compile_check', FALSE ) );
  39. $this->smarty->template_dir = us( $config->getString( 'template_dir', '', TRUE ) );
  40. $this->smarty->compile_dir = us( $config->getString( 'compile_dir', '', TRUE ) );
  41. $this->smarty->config_dir = us( $config->getString( 'config_dir', '', TRUE ) );
  42. $this->smarty->cache_dir = us( $config->getString( 'cache_dir', '', TRUE ) );
  43. $this->smarty->left_delimiter = us( $config->getString( 'left_delimiter', '{', FALSE ) );
  44. $this->smarty->right_delimiter = us( $config->getString( 'right_delimiter', '}', FALSE ) );
  45. // $this->smarty->default_modifiers = $config->getArray( 'default_modifiers', array() )->unbox();
  46. $plugins_dir = uv( $config->getArray( 'plugins_dir', array(), TRUE ) );
  47. // add default plugins_dir: Smarty/Smarty/plugins
  48. $reflector = new ReflectionClass($this->smarty);
  49. $plugins_dir[] = dirname($reflector->getFileName()) . '/plugins';
  50. $this->smarty->plugins_dir = $plugins_dir;
  51. log_debug( "smarty", "smarty->plugins_dir=" . print_r($this->smarty->plugins_dir, true), self::TAG );
  52. log_debug( "smarty", "smarty=" . spl_object_hash($this->smarty), self::TAG );
  53. $smarty_options = array(
  54. 'caching' => $this->smarty->caching,
  55. 'compile_check' => $this->smarty->compile_check,
  56. 'template_dir' => $this->smarty->template_dir,
  57. 'compile_dir' => $this->smarty->compile_dir,
  58. 'config_dir' => $this->smarty->config_dir,
  59. 'cache_dir' => $this->smarty->cache_dir,
  60. 'default_modifiers' => $this->smarty->default_modifiers,
  61. 'plugins_dir' => $this->smarty->plugins_dir,
  62. 'left_delimiter' => $this->smarty->left_delimiter,
  63. 'right_delimiter' => $this->smarty->right_delimiter,
  64. );
  65. foreach( $smarty_options as $key => $value ){
  66. log_debug( 'system, debug, smarty', "smarty option: [$key]=" . Charcoal_System::toString($value) );
  67. }
  68. }
  69. /**
  70. * Render layout
  71. *
  72. * Also set below values:
  73. *
  74. * $charcoal.profile.XXX --- defined values in profile.ini
  75. * $charcoal.cookie.XXX --- defined values in server cookies
  76. * $charcoal.session.XXX --- defined values in server session
  77. * $charcoal.request.id --- request ID(sha1 hash)
  78. * $charcoal.request.path --- request object path(i.e. @:login:form)
  79. * $charcoal._smarty --- Smarty object
  80. *
  81. * @param Charcoal_IEventContext $context event context
  82. */
  83. protected function renderLayout( $context )
  84. {
  85. $charcoal = array();
  86. /** @var Charcoal_RenderLayoutEvent $event */
  87. $event = $context->getEvent();
  88. /** @var Charcoal_HttpResponse $response */
  89. $response = $context->getResponse();
  90. /** @var Charcoal_Session $session */
  91. $session = $context->getSession();
  92. // retrieve layout
  93. $layout = $event->getLayout();
  94. // Page information
  95. $page_info = $layout->getAttribute( s('page_info') );
  96. log_debug( "smarty", "page_info=" . print_r($page_info,true), self::TAG );
  97. // Profile information
  98. $profile_config = $this->getSandbox()->getProfile()->getAll();
  99. if ( $profile_config && is_array($profile_config) ){
  100. foreach( $profile_config as $key => $value ){
  101. $charcoal['profile'][$key] = $value;
  102. }
  103. }
  104. // Cookie information
  105. if ( $response instanceof Charcoal_HttpResponse ){
  106. $cookies = $response->getCookies();
  107. if ( $cookies && is_array($cookies) ){
  108. foreach( $cookies as $key => $value ){
  109. $charcoal['cookie'][$key] = $value;
  110. }
  111. }
  112. }
  113. $smarty = $this->smarty;
  114. // Assign variables
  115. if ( $page_info && is_array($page_info) ){
  116. foreach( $page_info as $key => $value ){
  117. $smarty->assign( $key, $value );
  118. }
  119. }
  120. // Session data
  121. $charcoal['session'] = $session;
  122. // Request ID and reauest path
  123. $charcoal['request']['id'] = $this->getSandbox()->getEnvironment()->get( '%REQUEST_ID%' );
  124. $charcoal['request']['path'] = $this->getSandbox()->getEnvironment()->get( '%REQUEST_PATH%' );
  125. // Assign all
  126. $smarty->assign( 'charcoal', $charcoal );
  127. // Assign all layout values
  128. $layout_values = $event->getValues();
  129. if ( !$layout_values ){
  130. // If layout values are not set, response values will be used instead.
  131. $layout_values = $response->getAll();
  132. }
  133. foreach( $layout_values as $key => $value ){
  134. $smarty->assign( $key, $value );
  135. }
  136. $smarty->assign( '_smarty', $smarty );
  137. // render template
  138. $template = $layout->getAttribute( s('layout') );
  139. // set smarty error_reporting flags
  140. $this->smarty->error_reporting = E_ALL & ~E_STRICT & ~E_WARNING & ~E_NOTICE & ~(8192 /*= E_DEPRECATED */);
  141. // compile and output template
  142. log_debug( "smarty", "template=$template", self::TAG );
  143. $html = $smarty->fetch( $template );
  144. log_debug( "smarty", "html=$html", self::TAG );
  145. // output to rendering target
  146. $render_target = $event->getRenderTarget();
  147. if ( $render_target ){
  148. $render_target->render( $html );
  149. log_debug( "smarty", "Rendered by render target: $render_target", self::TAG );
  150. }
  151. else{
  152. echo $html;
  153. log_debug( "smarty", "Output by echo.", self::TAG );
  154. }
  155. }
  156. /**
  157. * Process events
  158. *
  159. * @param Charcoal_IEventContext $context event context
  160. *
  161. * @return boolean|Charcoal_Boolean
  162. */
  163. public function processEvent( $context )
  164. {
  165. log_debug( "smarty", "smarty=" . spl_object_hash($this->smarty), self::TAG );
  166. /** @var Charcoal_RenderLayoutEvent $event */
  167. $event = $context->getEvent();
  168. /** @var Charcoal_HttpResponse $response */
  169. $response = $context->getResponse();
  170. // retrieve layout
  171. $layout = $event->getLayout();
  172. log_debug( "smarty", "Rendering by smarty. Layout:" . print_r($layout,true), self::TAG );
  173. log_debug( "smarty", "caching=" . print_r($this->smarty->caching, true), self::TAG );
  174. log_debug( "smarty", "template_dir=" . print_r($this->smarty->template_dir, true), self::TAG );
  175. log_debug( "smarty", "compile_dir=" . print_r($this->smarty->compile_dir, true), self::TAG );
  176. log_debug( "smarty", "config_dir=" . print_r($this->smarty->config_dir, true), self::TAG );
  177. log_debug( "smarty", "cache_dir=" . print_r($this->smarty->cache_dir, true), self::TAG );
  178. log_debug( "smarty", "plugins_dir=" . print_r($this->smarty->plugins_dir, true), self::TAG );
  179. // page redirection
  180. if ( $layout instanceof Charcoal_IRedirectLayout ){
  181. $url = $layout->makeRedirectURL();
  182. $response->redirect( s($url) );
  183. log_debug( "system, debug, smarty, redirect", "redirected to URL: $url", self::TAG );
  184. return b(TRUE);
  185. }
  186. elseif ( $event instanceof Charcoal_URLRedirectEvent ){
  187. /** @var Charcoal_URLRedirectEvent $event */
  188. $url = $event->getURL();
  189. $response->redirect( s($url) );
  190. log_debug( "system, debug, smarty, redirect", "redirected to URL: $url", self::TAG );
  191. return b(TRUE);
  192. }
  193. // render layout
  194. if ( $event instanceof Charcoal_RenderLayoutEvent )
  195. {
  196. set_error_handler( array($this,"onUnhandledError") );
  197. try{
  198. $this->renderLayout( $context );
  199. }
  200. catch ( Exception $ex )
  201. {
  202. _catch( $ex );
  203. _throw( new Charcoal_SmartyRendererTaskException( "rendering failed", $ex ) );
  204. }
  205. restore_error_handler();
  206. return b(TRUE);
  207. }
  208. return b(FALSE);
  209. }
  210. /*
  211. * smarty error handler
  212. */
  213. public static function onUnhandledError( $errno, $errstr, $errfile, $errline )
  214. {
  215. $flags_handled = error_reporting() ;
  216. if ( Charcoal_System::isBitSet( $errno, $flags_handled, Charcoal_System::BITTEST_MODE_ANY ) )
  217. {
  218. $errno_disp = Charcoal_System::phpErrorString( $errno );
  219. echo "smarty error [errno]$errno($errno_disp) [errstr]$errstr [errfile]$errfile [errline]$errline" . eol();
  220. }
  221. return TRUE; // Otherwise, ignore all errors
  222. }
  223. }