PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/tasks/SmartyRendererTask.class.php

https://bitbucket.org/stk2k/charcoalphp2.1
PHP | 207 lines | 122 code | 42 blank | 43 comment | 12 complexity | 3fb7519cebad8a9a90f3876ac503e53b MD5 | raw file
  1. <?php
  2. /**
  3. * Smarty???????
  4. *
  5. * PHP version 5
  6. *
  7. * @package renderers
  8. * @author CharcoalPHP Development Team
  9. * @copyright 2008 - 2012 CharcoalPHP Development Team
  10. */
  11. require_once( 'Smarty/Smarty.class.php' );
  12. class Charcoal_SmartyRendererTask extends Charcoal_Task implements Charcoal_ITask
  13. {
  14. var $_template_files;
  15. var $_smarty;
  16. /*
  17. * ???????
  18. */
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->_template_files = array();
  23. $this->_smarty = new Smarty();
  24. log_debug( "smarty", "SMARTY_DIR: " . SMARTY_DIR );
  25. }
  26. /*
  27. * Initialize instance
  28. *
  29. * @param Charcoal_Config $config configuration of component
  30. */
  31. public function configure( Charcoal_Config $config )
  32. {
  33. parent::configure( $config );
  34. $this->_smarty->caching = 0; //$config->getBoolean( s('caching') )->getValue();
  35. $this->_smarty->compile_check = $config->getBoolean( s('compile_check'), b(FALSE) )->getValue();
  36. $this->_smarty->template_dir = $config->getString( s('template_dir'), s('') )->getValue();
  37. $this->_smarty->compile_dir = $config->getString( s('compile_dir'), s('') )->getValue();
  38. $this->_smarty->config_dir = $config->getString( s('config_dir'), s('') )->getValue();
  39. $this->_smarty->cache_dir = $config->getString( s('cache_dir'), s('') )->getValue();
  40. $this->_smarty->_default_modifiers = $config->getArray( s('default_modifiers'), v(array()) )->getValue();
  41. $plugins_dir = $config->getArray( s('plugins_dir'), v(array()) );
  42. if ( $plugins_dir->isEmpty() ){
  43. $this->_smarty->plugins_dir = 'plugins';
  44. }
  45. else{
  46. $this->_smarty->plugins_dir = $plugins_dir->getValue();
  47. }
  48. $left_delimiter = $config->getString( s('left_delimiter'), s('{') );
  49. if ( !$left_delimiter->isEmpty() ){
  50. $this->_smarty->left_delimiter = $left_delimiter->getValue();
  51. }
  52. $right_delimiter = $config->getString( s('right_delimiter'), s('}') );
  53. if ( !$right_delimiter->isEmpty() ){
  54. $this->_smarty->right_delimiter = $right_delimiter->getValue();
  55. }
  56. }
  57. /**
  58. * Process events
  59. *
  60. * @param Charcoal_IEventContext $context event context
  61. */
  62. public function processEvent( Charcoal_IEventContext $context )
  63. {
  64. $event = $context->getEvent();
  65. $response = $context->getResponse();
  66. $sequence = $context->getSequence();
  67. // output response headers
  68. $response->flushHeaders();
  69. // ????????
  70. $layout = $event->getLayout();
  71. log_info( "system,smarty", "SmartyRenderer???????????????layout:" . print_r($layout,true) );
  72. log_info( "smarty", "caching=" . $this->_smarty->caching );
  73. log_info( "smarty", "template_dir=" . $this->_smarty->template_dir );
  74. log_info( "smarty", "compile_dir=" . $this->_smarty->compile_dir );
  75. log_info( "smarty", "config_dir=" . $this->_smarty->config_dir );
  76. log_info( "smarty", "cache_dir=" . $this->_smarty->cache_dir );
  77. try{
  78. $charcoal = array();
  79. // ??????
  80. if ( $layout instanceof Charcoal_IRedirectLayout ){
  81. // ??????????
  82. $url = $layout->makeRedirectURL();
  83. // ??????
  84. $response->redirect( s($url) );
  85. log_info( "system,smarty", "???????????????????????:$url" );
  86. }
  87. else if ( $event instanceof Charcoal_URLRedirectEvent ){
  88. // ??????????
  89. $url = $event->getURL();
  90. // ??????
  91. $response->redirect( s($url) );
  92. log_info( "system,smarty", "???????????????????????:$url" );
  93. }
  94. else{
  95. // Charcoal_EncodingConverter???
  96. $conv = Charcoal_EncodingConverter::fromString( s('PHP'), s('HTML') );
  97. // ?????????
  98. $charcoal['version']['major'] = Charcoal_Framework::getMajorVersion();
  99. $charcoal['version']['minor'] = Charcoal_Framework::getMinorVersion();
  100. $charcoal['version']['revision'] = Charcoal_Framework::getRevision();
  101. $charcoal['version']['build'] = Charcoal_Framework::getBuildNumber();
  102. // ?????
  103. $page_info = $layout->getAttribute( s('page_info') );
  104. log_info( "smarty", "page_info:" . print_r($page_info,true) );
  105. // ????????
  106. $profile_config = Charcoal_Profile::getConfig();
  107. if ( $profile_config && is_array($profile_config) ){
  108. foreach( $profile_config as $key => $value ){
  109. $charcoal['profile'][$key] = $value;
  110. }
  111. }
  112. // ??????
  113. $cookies = $response->getCookies();
  114. if ( $cookies && is_array($cookies) ){
  115. foreach( $cookies as $key => $value ){
  116. $charcoal['cookie'][$key] = $value;
  117. }
  118. }
  119. // Smarty?????????
  120. $smarty = $this->_smarty;
  121. // ?????????Smarty?assign??
  122. if ( $page_info && is_array($page_info) ){
  123. foreach( $page_info as $key => $value ){
  124. $smarty->assign( $key, $value );
  125. log_info( "smarty", "page_info [$key]=" . print_r($value,true) );
  126. }
  127. }
  128. // ???????????????????????
  129. $charcoal['sequence'] = $sequence;
  130. // ?????ID???????????
  131. $charcoal['request']['id'] = Charcoal_Framework::getRequestID();
  132. $charcoal['request']['path'] = Charcoal_Framework::getRequestPath();
  133. // ????????????????????????
  134. $charcoal['response'] = $response;
  135. // ????????assign??
  136. $smarty->assign( 'charcoal', $charcoal );
  137. // ????????????Smarty?assign??
  138. $keys = $response->getKeys();
  139. foreach( $keys as $key ){
  140. $value = $response->get( s($key) );
  141. $smarty->assign( $key, $value );
  142. log_info( "smarty", "response [$key]=" . print_r($value,true) );
  143. }
  144. // ???????????????????????
  145. log_info( "smarty", "response:" . print_r($response,true) );
  146. $smarty->register_object( 'response', $response );
  147. // ????????????
  148. $template = $layout->getAttribute( s('layout') );
  149. log_info( "smarty", "template=$template" );
  150. $html = $smarty->fetch( $template );
  151. log_info( "smarty", "html=$html" );
  152. echo $html;
  153. log_info( "system,smarty", "?????????????????????????????:$template" );
  154. }
  155. }
  156. catch ( Exception $ex )
  157. {
  158. _catch( $ex );
  159. _throw( new Charcoal_SmartyRendererTaskException( s("rendering failed"), $ex ) );
  160. }
  161. log_info( "system,smarty", "SmartyRenderer????????????????" );
  162. return b(TRUE);
  163. }
  164. }
  165. return __FILE__;