PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/halogy/application/libraries/MY_Parser.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 234 lines | 139 code | 30 blank | 65 comment | 35 complexity | 27c3d873562462db91f913e6f2f0577d 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. * MY Parser Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Parser
  22. * @author Haloweb Ltd
  23. */
  24. class MY_Parser extends CI_Parser {
  25. /**
  26. * Parse a template
  27. *
  28. * Parses pseudo-variables contained in the specified template,
  29. * replacing them with the data in the second param
  30. *
  31. * @access public
  32. * @param string
  33. * @param array
  34. * @param bool
  35. * @return string
  36. */
  37. function parse($template, $data, $return = FALSE, $include = FALSE)
  38. {
  39. $CI =& get_instance();
  40. if ($template == '')
  41. {
  42. return FALSE;
  43. }
  44. if ($include === FALSE)
  45. {
  46. $template = $CI->load->view($template, $data, TRUE);
  47. }
  48. if (isset($data) && $data != '')
  49. {
  50. foreach ($data as $key => $val)
  51. {
  52. if (is_array($val))
  53. {
  54. $template = $this->_parse_pair($key, $val, $template);
  55. }
  56. else
  57. {
  58. $template = $this->_parse_single($key, (string)$val, $template);
  59. }
  60. }
  61. }
  62. // Check for conditional statments
  63. $template = $this->conditionals($template, $data);
  64. // populate form tags
  65. preg_match_all('/{form:(.+)}/i', $template, $posts);
  66. if ($posts)
  67. {
  68. foreach ($posts[1] as $post => $value)
  69. {
  70. if ($postValue = $CI->input->post($value))
  71. {
  72. $template = str_replace('{form:'.$value.'}', $postValue, $template);
  73. }
  74. else
  75. {
  76. $template = str_replace('{form:'.$value.'}', '', $template);
  77. }
  78. }
  79. }
  80. //$template = preg_replace('/{(.+)}/i', '', $template);
  81. if ($return == FALSE)
  82. {
  83. $CI->output->append_output($template);
  84. }
  85. return $template;
  86. }
  87. // --------------------------------------------------------------------
  88. /**
  89. * Parse conditional statments
  90. *
  91. * @access public
  92. * @param string
  93. * @param bool
  94. * @return string
  95. */
  96. function conditionals($template, $data)
  97. {
  98. if (preg_match_all('/'.$this->l_delim.'if (.+)'.$this->r_delim.'(.+)'.$this->l_delim.'\/if'.$this->r_delim.'/siU', $template, $conditionals, PREG_SET_ORDER))
  99. {
  100. if (count($conditionals) > 0)
  101. {
  102. // filter through conditionals
  103. foreach($conditionals as $condition)
  104. {
  105. // get conditional and the string inside
  106. $code = (isset($condition[0])) ? $condition[0] : FALSE;
  107. $condString = (isset($condition[1])) ? str_replace(' ', '', $condition[1]) : FALSE;
  108. $insert = (isset($condition[2])) ? $condition[2] : '';
  109. // check code is valid
  110. if (!preg_match('/('.$this->l_delim.'|'.$this->r_delim.')/', $condString, $condProblem))
  111. {
  112. if (!empty($code) || $condString != FALSE || !empty($insert))
  113. {
  114. if (preg_match("/^!(.*)$/", $condString, $matches))
  115. {
  116. $condVar = (@!$data[trim($matches[1])]) ? 0 : $data[trim($matches[1])];
  117. @$result = (!$condVar) ? TRUE : FALSE;
  118. }
  119. elseif (preg_match("/([a-z0-9\-_:\(\)]+)(\!=|=|==|>|<)([a-z0-9\-_\/]+)/", $condString, $matches))
  120. {
  121. $condVar = (@!$data[$matches[1]]) ? 0 : $data[trim($matches[1])];
  122. if ($matches[2] == '==' || $matches[2] == '=')
  123. {
  124. @$result = ($condVar === $matches[3]) ? TRUE : FALSE;
  125. }
  126. elseif ($matches[2] == '!=')
  127. {
  128. @$result = ($condVar !== $matches[3]) ? TRUE : FALSE;
  129. }
  130. elseif ($matches[2] == '>')
  131. {
  132. @$result = ($condVar > $matches[3]) ? TRUE : FALSE;
  133. }
  134. elseif ($matches[2] == '<')
  135. {
  136. @$result = ($condVar < $matches[3]) ? TRUE : FALSE;
  137. }
  138. }
  139. else
  140. {
  141. // if the variable is set
  142. if (isset($data[$condString]) && is_array($data[$condString]))
  143. {
  144. $result = (count($data[$condString]) > 0) ? TRUE : FALSE;
  145. }
  146. else
  147. {
  148. $result = (isset($data[$condString]) && $data[$condString] != '') ? TRUE : FALSE;
  149. }
  150. }
  151. // filter for else
  152. $insert = preg_split('/'.$this->l_delim.'else'.$this->r_delim.'/siU', $insert);
  153. if ($result == TRUE)
  154. {
  155. // show the string inside
  156. $template = str_replace($code, $insert[0], $template);
  157. }
  158. else
  159. {
  160. if (is_array($insert))
  161. {
  162. $insert = (isset($insert[1])) ? $insert[1] : '';
  163. $template = str_replace($code, $insert, $template);
  164. }
  165. else
  166. {
  167. $template = str_replace($code, '', $template);
  168. }
  169. }
  170. }
  171. }
  172. else
  173. {
  174. // remove any conditionals we cant process
  175. $template = str_replace($code, '', $template);
  176. }
  177. }
  178. }
  179. //print_r($conditionals);
  180. }
  181. return $template;
  182. }
  183. // --------------------------------------------------------------------
  184. /**
  185. * Matches a variable pair
  186. *
  187. * @access private
  188. * @param string
  189. * @param string
  190. * @return mixed
  191. */
  192. function _match_pair($string, $variable)
  193. {
  194. $variable = str_replace('(', '\(', $variable);
  195. $variable = str_replace(')', '\)', $variable);
  196. if ( ! preg_match("|".$this->l_delim . $variable . $this->r_delim."(.+?)".$this->l_delim . '/' . $variable . $this->r_delim."|s", $string, $match))
  197. {
  198. return FALSE;
  199. }
  200. return $match;
  201. }
  202. }
  203. // END Parser Class
  204. /* End of file */