/application/libraries/MY_Parser.php

https://github.com/fredquie/pyrocms · PHP · 273 lines · 149 code · 34 blank · 90 comment · 14 complexity · 0e979995975e9781231a5ecb82402598 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Parser Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Parser
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/libraries/parser.html
  24. */
  25. class MY_Parser extends CI_Parser {
  26. var $helper_delim = ':';
  27. var $param_delim = '|';
  28. var $func_l_delim = '[';
  29. var $func_r_delim = ']';
  30. /**
  31. * Parse a view file
  32. *
  33. * Parses pseudo-variables contained in the specified template,
  34. * replacing them with the data in the second param
  35. *
  36. * @access public
  37. * @param string
  38. * @param array
  39. * @param bool
  40. * @return string
  41. */
  42. function parse($template, $data, $return = FALSE)
  43. {
  44. $CI =& get_instance();
  45. $template = $CI->load->view($template, $data, TRUE);
  46. return $this->_parse($template, $data, $return);
  47. }
  48. /**
  49. * String parse
  50. *
  51. * Parses pseudo-variables contained in the string content,
  52. * replacing them with the data in the second param
  53. *
  54. * @access public
  55. * @param string
  56. * @param array
  57. * @param bool
  58. * @return string
  59. */
  60. function string_parse($template, $data = NULL, $return = FALSE)
  61. {
  62. return $this->_parse($template, $data, $return);
  63. }
  64. /**
  65. * Parse
  66. *
  67. * Parses pseudo-variables contained in the specified template,
  68. * replacing them with the data in the second param
  69. *
  70. * @access public
  71. * @param string
  72. * @param array
  73. * @param bool
  74. * @return string
  75. */
  76. function _parse($template, $data, $return = FALSE)
  77. {
  78. $CI =& get_instance();
  79. $this->_data =& $data;
  80. if ($template == '')
  81. {
  82. return FALSE;
  83. }
  84. if(is_array($this->_data))
  85. {
  86. foreach ($this->_data as $key => $val)
  87. {
  88. if (is_array($val))
  89. {
  90. $template = $this->_parse_pair($key, $val, $template);
  91. }
  92. else
  93. {
  94. $template = $this->_parse_single($key, (string)$val, $template);
  95. }
  96. }
  97. }
  98. // -- Helper stuff
  99. // Now lets find our functions
  100. $pattern = '/%1$s(([a-z0-9_]+)%2$s)?([a-z0-9_]+)(\%3$s([^%4$s]+)\])?%5$s/';
  101. $pattern = sprintf($pattern, $this->l_delim, $this->helper_delim, $this->func_l_delim, $this->func_r_delim, $this->r_delim);
  102. preg_match_all($pattern, $template, $matches);
  103. $matches_count = count($matches[0]);
  104. $from = array();
  105. $to = array();
  106. for($i = 0; $i < $matches_count; $i++)
  107. {
  108. // If a value is returned for this tag, use it. If not, perhaps it is text?
  109. if($val = $this->_parse_function($matches[2][$i], $matches[3][$i], $matches[5][$i]))
  110. {
  111. $from[] = $matches[0][$i];
  112. $to[] = $val;
  113. $template = str_replace($from, $to, $template);
  114. }
  115. }
  116. if($matches_count > 0)
  117. {
  118. $template = str_replace($from, $to, $template);
  119. }
  120. // -- End helper stuff
  121. if ($return == FALSE)
  122. {
  123. $CI->output->append_output($template);
  124. }
  125. return $template;
  126. }
  127. // --------------------------------------------------------------------
  128. /**
  129. * Parse function
  130. *
  131. * Parses a function or helper found in tbe template.
  132. *
  133. * @access private
  134. * @param string
  135. * @param string
  136. * @param string
  137. * @return string
  138. */
  139. function _parse_function($helper, $function, $parameters)
  140. {
  141. $CI =& get_instance();
  142. // If there is a helper file (not a native PHP function) and it has not yet been loaded
  143. if(!empty($helper) && !array_key_exists($helper, $CI->load->_ci_helpers))
  144. {
  145. // This includes the helper, but will only include it once
  146. $CI->load->helper($helper);
  147. }
  148. if(function_exists($function))
  149. {
  150. if($parameters !== '')
  151. {
  152. return call_user_func_array( $function, $this->_parse_parameters($parameters) );
  153. }
  154. else
  155. {
  156. return $function();
  157. }
  158. }
  159. return FALSE;
  160. }
  161. // --------------------------------------------------------------------
  162. /**
  163. * Parse Parameters
  164. *
  165. * Parse a parameter string into an array of parameters
  166. *
  167. * @access private
  168. * @param string
  169. * @param string
  170. * @param string
  171. * @return string
  172. */
  173. function _parse_parameters($parameter_string = '')
  174. {
  175. $double_string = '"[^(\\'.$this->func_r_delim .'|\\'. $this->param_delim.')]*"';
  176. $single_string = '\'[^(\\'.$this->func_r_delim .'|\\'. $this->param_delim.')]*\'';
  177. $bool = '(true|false|null)';
  178. $int = '[0-9., ]+';
  179. $variable = '\$[a-z_]+';
  180. $pattern = sprintf('/(\%s|\%s)?(%s|%s|%s|%s|%s)+/i', $this->func_l_delim, $this->param_delim, $double_string, $single_string, $bool, $int, $variable);
  181. preg_match_all($pattern, $parameter_string, $matches);
  182. $matches_count = count($matches[0]);
  183. $dirty_parameters =& $matches[2];
  184. $parameters = array();
  185. foreach($dirty_parameters as $param)
  186. {
  187. $first_char = substr($param, 0, 1);
  188. switch( $first_char )
  189. {
  190. // Parameter is a string, remove first and last "" or ''
  191. case "'":
  192. case '"':
  193. $param = substr($param, 1, -1);
  194. break;
  195. // Parameter is a CI view variable
  196. case '$':
  197. $param = substr($param, 1);
  198. $param = array_key_exists($param, $this->_data) ? $this->_data[$param] : NULL;
  199. break;
  200. // What else could it be?
  201. default:
  202. // Param is true/TRUE
  203. if(strtoupper($param) === 'TRUE')
  204. {
  205. $param = TRUE;
  206. }
  207. // Param is false/FALSE
  208. elseif(strtoupper($param) === 'FALSE')
  209. {
  210. $param = FALSE;
  211. }
  212. // Param is null/NULL
  213. elseif(strtoupper($param) === 'NULL')
  214. {
  215. $param = NULL;
  216. }
  217. break;
  218. }
  219. $parameters[] = $param;
  220. }
  221. return $parameters;
  222. }
  223. // --------------------------------------------------------------------
  224. }
  225. // END MY_Parser Class
  226. /* End of file MY_Parser.php */
  227. /* Location: ./application/libraries/MY_Parser.php */