PageRenderTime 37ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Parser.php

https://github.com/betchi/CodeIgniter
PHP | 239 lines | 80 code | 30 blank | 129 comment | 3 complexity | fc50b05f005e45770302cd72778c0664 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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 - 2014, 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 delimiter character for pseudo vars
  40. *
  41. * @var string
  42. */
  43. public $l_delim = '{';
  44. /**
  45. * Right delimiter character for pseudo 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 === ($matches = $this->_match_pair($string, $variable)))
  169. {
  170. return $string;
  171. }
  172. $str = '';
  173. $search = $replace = array();
  174. foreach ($matches as $match)
  175. {
  176. $str = '';
  177. foreach ($data as $row)
  178. {
  179. $temp = $match[1];
  180. foreach ($row as $key => $val)
  181. {
  182. $temp = is_array($val)
  183. ? $this->_parse_pair($key, $val, $temp)
  184. : $this->_parse_single($key, $val, $temp);
  185. }
  186. $str .= $temp;
  187. }
  188. $search[] = $match[0];
  189. $replace[] = $str;
  190. }
  191. return str_replace($search, $replace, $string);
  192. }
  193. // --------------------------------------------------------------------
  194. /**
  195. * Matches a variable pair
  196. *
  197. * @param string $string
  198. * @param string $variable
  199. * @return mixed
  200. */
  201. protected function _match_pair($string, $variable)
  202. {
  203. return preg_match_all('|'.preg_quote($this->l_delim).$variable.preg_quote($this->r_delim).'(.+?)'.preg_quote($this->l_delim).'/'.$variable.preg_quote($this->r_delim).'|s',
  204. $string, $match, PREG_SET_ORDER)
  205. ? $match : FALSE;
  206. }
  207. }
  208. /* End of file Parser.php */
  209. /* Location: ./system/libraries/Parser.php */