PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/include/AMP/Display/Template.php

https://github.com/radicalsuz/amp
PHP | 294 lines | 166 code | 41 blank | 87 comment | 23 complexity | 36a0cc05c4e3bb0148b4fb5c9035d527 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-2.0
  1. <?php
  2. class AMP_Display_Template {
  3. var $_template;
  4. var $_properties;
  5. var $_output;
  6. //var $_allowed_methods = array( );
  7. //var $_confirmed_methods = array( );
  8. //var $_methods_output= array( );
  9. //var $_method_args = array( );
  10. var $_helpers = array( );
  11. var $_tokens_active;
  12. var $_path_default = 'AMP/Display/';
  13. var $_path_current = 'AMP/Display/';
  14. var $_extension = 'thtml';
  15. var $_displays_partial = array( );
  16. var $_headings = array( );
  17. function AMP_Display_Template( $template_path = null ) {
  18. $this->__construct( $template_path );
  19. }
  20. function __construct( $template_path = null ) {
  21. if ( isset( $template_path )) $this->set_path_template( $template_path );
  22. }
  23. function set_path_template( $path ){
  24. $this->_template = $this->_load_template_file( $path );
  25. $this->_path_current = substr( $path, 0, strlen( $path ) - strlen( basename( $path )));
  26. $dotspot = strrpos( basename( $path ), ".");
  27. if ( $dotspot ) $this->_extension = substr( basename( $path ), $dotspot + 1 );
  28. preg_match_all( "/%([\w\s]+)(%|$)/", $this->_template, $token_results, PREG_PATTERN_ORDER );
  29. $this->_tokens_active = $token_results[1];
  30. }
  31. function _load_template_file( $request_path ) {
  32. $path_verified = false;
  33. $path_exists = file_exists_incpath( $request_path );
  34. if ( $path_exists ){
  35. $path_verified = $path_exists;
  36. } else {
  37. $test_path = $this->_path_default . basename( $request_path );
  38. if ( $path_exists = file_exists_incpath( $test_path )) {
  39. $path_verified = $path_exists;
  40. }
  41. }
  42. if ( file_exists( $path_verified )) return file_get_contents( $path_verified );
  43. /*
  44. $paths = explode(PATH_SEPARATOR, get_include_path());
  45. foreach ($paths as $path)
  46. {
  47. // Formulate the absolute path
  48. $fullpath = $path . DIRECTORY_SEPARATOR . $path_verified;
  49. // Check it
  50. if (file_exists($fullpath)) {
  51. return file_get_contents( $fullpath );
  52. }
  53. }
  54. return false;
  55. */
  56. }
  57. function has_template( ){
  58. return $this->_template;
  59. }
  60. function set_property( $name, $value ){
  61. $this->_properties[ $name ] = $value;
  62. }
  63. function set_properties( $values ){
  64. $this->_properties = array( );
  65. return $this->add_properties( $values );
  66. }
  67. function add_properties( $values ) {
  68. foreach( $values as $name => $value ){
  69. $this->set_property( $name, $value );
  70. }
  71. }
  72. function set_method( $key, $method_desc, $args = array( ) ) {
  73. $this->_allowed_methods[ $key ] = $method_desc;
  74. $this->_method_args[ $key ] = $args;
  75. }
  76. function execute( ){
  77. $this->_replace_tokens( );
  78. return $this->_output;
  79. }
  80. function _replace_tokens( ){
  81. $replace_set = array( );
  82. foreach( $this->_tokens_active as $token ){
  83. if ( !isset( $this->_properties[ $token ])) continue;
  84. $key = '%' . $token . '%';
  85. $replace_set[ $key ] = $this->_properties[ $token ];
  86. }
  87. $this->_output = str_replace( array_keys( $replace_set ), array_values( $replace_set ), $this->_template );
  88. $this->_execute_renderers( );
  89. $this->_execute_helpers( );
  90. }
  91. function _execute_renderers( ){
  92. $render_requests = array( );
  93. //$request_types = array( 'heading', 'conditional' );
  94. $render_request_types = array( 'render_partial_conditional', 'render_partial_heading', 'render_partial_loop', 'render_partial');
  95. foreach( $this->_tokens_active as $token ){
  96. if ( strpos( $token, 'render_partial_' ) === FALSE ) continue;
  97. $token_key = '%' . $token . '%';
  98. foreach( $render_request_types as $request_type ){
  99. if ( strpos( $token, $request_type ) !== 0 ) continue;
  100. $key = substr( $token, strlen( $request_type ) + 1 );
  101. $local_method = '_' . $request_type;
  102. $render_requests[ $token_key ] = $this->$local_method( $key );
  103. break;
  104. }
  105. /*
  106. if ( strpos( $token, 'render_partial_conditional_' ) === 0 ){
  107. $key = substr( $token, 27 );
  108. $render_requests[ $token_key ] = $this->_render_partial_conditional( $key );
  109. continue;
  110. }
  111. if ( strpos( $token, 'render_partial_heading_' ) === 0 ){
  112. $key = substr( $token, 23 );
  113. $render_requests[ $token_key ] = $this->_render_partial_heading( $key );
  114. continue;
  115. }
  116. //default
  117. $key = substr( $token, 15 );
  118. $render_requests[ $token_key ] = $this->_render_partial( $key );
  119. */
  120. }
  121. $this->_output = str_replace( array_keys( $render_requests ), array_values( $render_requests ), $this->_output );
  122. }
  123. function _render_partial_conditional( $key ){
  124. if ( isset( $this->_properties[ $key ]) && $this->_properties[ $key ]) {
  125. return $this->_render_partial( $key );
  126. }
  127. }
  128. function _render_partial_heading( $key ){
  129. if ( isset( $this->_properties[ $key ])
  130. && ( !isset( $this->_headings[ $key ])
  131. || strtolower( $this->_properties[ $key ] ) != strtolower( $this->_headings[ $key ]))) {
  132. $this->_headings[ $key ] = $this->_properties[ $key ];
  133. $result = $this->_render_partial( 'heading.'. $key );
  134. if ( $result ) return $result;
  135. //fallback use default heading template
  136. $this->set_property['heading'] = $this->_headings[ $key ];
  137. return $this->_render_partial( 'heading' );
  138. }
  139. }
  140. function _render_partial_loop( $key ) {
  141. if ( !is_array( $this->_properties[ $key ])) return false;
  142. $output = false;
  143. $display = &$this->_load_partial( $key );
  144. foreach( $this->_properties[$key] as $id => $source ){
  145. if ( is_array( $source )) {
  146. $display->set_properties( $source );
  147. }
  148. if ( is_object( $source )) {
  149. $display->set_properties( $source->getData( ) );
  150. }
  151. $output .= $display->execute( );
  152. }
  153. return $output;
  154. }
  155. function _render_partial( $key ){
  156. $display = &$this->_load_partial( $key );
  157. if ( !$display->has_template( )) return false;
  158. return $display->execute( );
  159. }
  160. function &_load_partial( $key ) {
  161. $file_name = $this->_make_partial_filename( $key );
  162. $partial_display = & new AMP_Display_Template( $file_name );
  163. $partial_display->inherit( $this );
  164. return $partial_display;
  165. }
  166. function get_properties( ){
  167. return $this->_properties;
  168. }
  169. function get_helpers( ){
  170. return $this->_helpers;
  171. }
  172. function add_helper( &$helper, $key ){
  173. $this->_helpers[ $key ] = &$helper;
  174. }
  175. function inherit( &$display ){
  176. $this->set_properties( $display->get_properties( ));
  177. foreach( $display->get_helpers( ) as $key => $helper ) {
  178. $this->add_helper( $helper, $key );
  179. }
  180. }
  181. function _execute_helpers( ){
  182. $helper_output = array( );
  183. foreach( $this->_tokens_active as $token ) {
  184. if ( strpos( $token, 'helper_' ) === FALSE ) continue;
  185. $token_key = '%' . $token . '%';
  186. $helper_request = split( '_', $token );
  187. $helper_key = $helper_request[1];
  188. if ( !isset( $this->_helpers[ $helper_key ])) continue;
  189. $helper_method = join( '_', array_slice( $helper_request, 2 ));
  190. if ( !method_exists( $this->_helpers[ $helper_key ], $helper_method )) continue;
  191. $active_helper = &$this->_helpers[ $helper_key ];
  192. $helper_output[ $token_key ] = $active_helper->$helper_method( $this->get_properties( ));
  193. }
  194. $this->_output = str_replace( array_keys( $helper_output ), array_values( $helper_output ), $this->_output );
  195. }
  196. function _make_partial_filename( $key ) {
  197. return $this->_path_current . $key . '.inc.' . $this->_extension;
  198. }
  199. /*
  200. function _populate_values( ){
  201. $this->_populate_properties( );
  202. $this->_populate_methods( );
  203. }
  204. function _populate_properties( ){
  205. $this->_output = str_replace( array_keys( $this->_properties ), array_values( $this->_properties ), $this->_template );
  206. }
  207. function _populate_methods( ){
  208. foreach( $this->_allowed_methods as $key => $method_def ){
  209. if ( !( $action_type = $this->_verify_method_def( $key ))) continue;
  210. $this->_methods_output[$key] = call_user_func_array( $action_type, $this->get_method_args( $key ) );
  211. }
  212. $this->_output = str_replace( array_keys( $this->_methods_output), array_values( $this->_methods_output), $this->_output );
  213. }
  214. function _verify_method_def( $key, $method_def ) {
  215. if ( isset( $this->_confirmed_methods[ $key ])) return $this->_confirmed_methods[ $key ];
  216. // free function
  217. $result = ( is_string( $method_def ) && function_exists( $method_def ));
  218. if ( $result ) return $this->_confirm_method( $key, $method );
  219. //class method
  220. $result = ( is_array( $method_def ) && class_exists( $method_def[0]) && method_exists( $method_def[0], $method_def[1] ));
  221. if ( $result ) return $this->_confirm_method( $key, array( $method_def[0], $method_def[1] );
  222. //actual object
  223. $result = ( is_array( $method_def ) && is_object( $method_def[0]) && method_exists( $method_def[0], $method_def[1] ));
  224. if ( $result ) return $this->_confirm_method( $key, array( $method_def[0], $method_def[1] );
  225. //confirm failed
  226. return $this->_confirm_method( $key, false );
  227. }
  228. function _confirm_method( $key, $method_simple ){
  229. $this->_confirmed_methods[$key] = $method_simple;
  230. return $method_simple;
  231. }
  232. function get_method_args( $key ) {
  233. $results = array( );
  234. foreach( $this->_method_args[ $key ] as $value ){
  235. $results[$key] = $value;
  236. if ( isset( $this->_properties[$value])) $results[$key] = $this->_properties[$value];
  237. }
  238. return $results;
  239. }
  240. */
  241. }
  242. ?>