/system/libraries/Parser.php

https://github.com/dchill42/CodeIgniter · PHP · 231 lines · 73 code · 29 blank · 129 comment · 3 complexity · d748d74032634b1f3bb9fb88ae59187f MD5 · raw file

  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.2.4 or newer
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. * If you did not receive a copy of the license and are unable to obtain it
  16. * through the world wide web, please send an email to
  17. * licensing@ellislab.com so we can send you a copy immediately.
  18. *
  19. * @package CodeIgniter
  20. * @author EllisLab Dev Team
  21. * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
  22. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  23. * @link http://codeigniter.com
  24. * @since Version 1.0
  25. * @filesource
  26. */
  27. defined('BASEPATH') OR exit('No direct script access allowed');
  28. /**
  29. * Parser Class
  30. *
  31. * @package CodeIgniter
  32. * @subpackage Libraries
  33. * @category Parser
  34. * @author EllisLab Dev Team
  35. * @link http://codeigniter.com/user_guide/libraries/parser.html
  36. */
  37. class CI_Parser {
  38. /**
  39. * Left delimeter character for psuedo vars
  40. *
  41. * @var string
  42. */
  43. public $l_delim = '{';
  44. /**
  45. * Right delimeter character for psuedo vars
  46. *
  47. * @var string
  48. */
  49. public $r_delim = '}';
  50. /**
  51. * Reference to CodeIgniter instance
  52. *
  53. * @var object
  54. */
  55. protected $CI;
  56. // --------------------------------------------------------------------
  57. /**
  58. * Class constructor
  59. *
  60. * @return void
  61. */
  62. public function __construct()
  63. {
  64. $this->CI =& get_instance();
  65. }
  66. // --------------------------------------------------------------------
  67. /**
  68. * Parse a template
  69. *
  70. * Parses pseudo-variables contained in the specified template view,
  71. * replacing them with the data in the second param
  72. *
  73. * @param string
  74. * @param array
  75. * @param bool
  76. * @return string
  77. */
  78. public function parse($template, $data, $return = FALSE)
  79. {
  80. $template = $this->CI->load->view($template, $data, TRUE);
  81. return $this->_parse($template, $data, $return);
  82. }
  83. // --------------------------------------------------------------------
  84. /**
  85. * Parse a String
  86. *
  87. * Parses pseudo-variables contained in the specified string,
  88. * replacing them with the data in the second param
  89. *
  90. * @param string
  91. * @param array
  92. * @param bool
  93. * @return string
  94. */
  95. public function parse_string($template, $data, $return = FALSE)
  96. {
  97. return $this->_parse($template, $data, $return);
  98. }
  99. // --------------------------------------------------------------------
  100. /**
  101. * Parse a template
  102. *
  103. * Parses pseudo-variables contained in the specified template,
  104. * replacing them with the data in the second param
  105. *
  106. * @param string
  107. * @param array
  108. * @param bool
  109. * @return string
  110. */
  111. protected function _parse($template, $data, $return = FALSE)
  112. {
  113. if ($template === '')
  114. {
  115. return FALSE;
  116. }
  117. foreach ($data as $key => $val)
  118. {
  119. $template = is_array($val)
  120. ? $this->_parse_pair($key, $val, $template)
  121. : $template = $this->_parse_single($key, (string) $val, $template);
  122. }
  123. if ($return === FALSE)
  124. {
  125. $this->CI->output->append_output($template);
  126. }
  127. return $template;
  128. }
  129. // --------------------------------------------------------------------
  130. /**
  131. * Set the left/right variable delimiters
  132. *
  133. * @param string
  134. * @param string
  135. * @return void
  136. */
  137. public function set_delimiters($l = '{', $r = '}')
  138. {
  139. $this->l_delim = $l;
  140. $this->r_delim = $r;
  141. }
  142. // --------------------------------------------------------------------
  143. /**
  144. * Parse a single key/value
  145. *
  146. * @param string
  147. * @param string
  148. * @param string
  149. * @return string
  150. */
  151. protected function _parse_single($key, $val, $string)
  152. {
  153. return str_replace($this->l_delim.$key.$this->r_delim, (string) $val, $string);
  154. }
  155. // --------------------------------------------------------------------
  156. /**
  157. * Parse a tag pair
  158. *
  159. * Parses tag pairs: {some_tag} string... {/some_tag}
  160. *
  161. * @param string
  162. * @param array
  163. * @param string
  164. * @return string
  165. */
  166. protected function _parse_pair($variable, $data, $string)
  167. {
  168. if (FALSE === ($match = $this->_match_pair($string, $variable)))
  169. {
  170. return $string;
  171. }
  172. $str = '';
  173. foreach ($data as $row)
  174. {
  175. $temp = $match[1];
  176. foreach ($row as $key => $val)
  177. {
  178. $temp = is_array($val)
  179. ? $this->_parse_pair($key, $val, $temp)
  180. : $this->_parse_single($key, $val, $temp);
  181. }
  182. $str .= $temp;
  183. }
  184. return str_replace($match[0], $str, $string);
  185. }
  186. // --------------------------------------------------------------------
  187. /**
  188. * Matches a variable pair
  189. *
  190. * @param string
  191. * @param string
  192. * @return mixed
  193. */
  194. protected function _match_pair($string, $variable)
  195. {
  196. return preg_match('|'.preg_quote($this->l_delim).$variable.preg_quote($this->r_delim).'(.+?)'.preg_quote($this->l_delim).'/'.$variable.preg_quote($this->r_delim).'|s',
  197. $string, $match)
  198. ? $match : FALSE;
  199. }
  200. }
  201. /* End of file Parser.php */
  202. /* Location: ./system/libraries/Parser.php */