Parser.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * Parser Class
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Libraries
  44. * @category Parser
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/libraries/parser.html
  47. */
  48. class CI_Parser {
  49. /**
  50. * Left delimiter character for pseudo vars
  51. *
  52. * @var string
  53. */
  54. public $l_delim = '{';
  55. /**
  56. * Right delimiter character for pseudo vars
  57. *
  58. * @var string
  59. */
  60. public $r_delim = '}';
  61. /**
  62. * Reference to CodeIgniter instance
  63. *
  64. * @var object
  65. */
  66. protected $CI;
  67. // --------------------------------------------------------------------
  68. /**
  69. * Class constructor
  70. *
  71. * @return void
  72. */
  73. public function __construct()
  74. {
  75. $this->CI =& get_instance();
  76. log_message('info', 'Parser Class Initialized');
  77. }
  78. // --------------------------------------------------------------------
  79. /**
  80. * Parse a template
  81. *
  82. * Parses pseudo-variables contained in the specified template view,
  83. * replacing them with the data in the second param
  84. *
  85. * @param string
  86. * @param array
  87. * @param bool
  88. * @return string
  89. */
  90. public function parse($template, $data, $return = FALSE)
  91. {
  92. $template = $this->CI->load->view($template, $data, TRUE);
  93. return $this->_parse($template, $data, $return);
  94. }
  95. // --------------------------------------------------------------------
  96. /**
  97. * Parse a String
  98. *
  99. * Parses pseudo-variables contained in the specified string,
  100. * replacing them with the data in the second param
  101. *
  102. * @param string
  103. * @param array
  104. * @param bool
  105. * @return string
  106. */
  107. public function parse_string($template, $data, $return = FALSE)
  108. {
  109. return $this->_parse($template, $data, $return);
  110. }
  111. // --------------------------------------------------------------------
  112. /**
  113. * Parse a template
  114. *
  115. * Parses pseudo-variables contained in the specified template,
  116. * replacing them with the data in the second param
  117. *
  118. * @param string
  119. * @param array
  120. * @param bool
  121. * @return string
  122. */
  123. protected function _parse($template, $data, $return = FALSE)
  124. {
  125. if ($template === '')
  126. {
  127. return FALSE;
  128. }
  129. $replace = array();
  130. foreach ($data as $key => $val)
  131. {
  132. $replace = array_merge(
  133. $replace,
  134. is_array($val)
  135. ? $this->_parse_pair($key, $val, $template)
  136. : $this->_parse_single($key, (string) $val, $template)
  137. );
  138. }
  139. unset($data);
  140. $template = strtr($template, $replace);
  141. if ($return === FALSE)
  142. {
  143. $this->CI->output->append_output($template);
  144. }
  145. return $template;
  146. }
  147. // --------------------------------------------------------------------
  148. /**
  149. * Set the left/right variable delimiters
  150. *
  151. * @param string
  152. * @param string
  153. * @return void
  154. */
  155. public function set_delimiters($l = '{', $r = '}')
  156. {
  157. $this->l_delim = $l;
  158. $this->r_delim = $r;
  159. }
  160. // --------------------------------------------------------------------
  161. /**
  162. * Parse a single key/value
  163. *
  164. * @param string
  165. * @param string
  166. * @param string
  167. * @return string
  168. */
  169. protected function _parse_single($key, $val, $string)
  170. {
  171. return array($this->l_delim.$key.$this->r_delim => (string) $val);
  172. }
  173. // --------------------------------------------------------------------
  174. /**
  175. * Parse a tag pair
  176. *
  177. * Parses tag pairs: {some_tag} string... {/some_tag}
  178. *
  179. * @param string
  180. * @param array
  181. * @param string
  182. * @return string
  183. */
  184. protected function _parse_pair($variable, $data, $string)
  185. {
  186. $replace = array();
  187. preg_match_all(
  188. '#'.preg_quote($this->l_delim.$variable.$this->r_delim).'(.+?)'.preg_quote($this->l_delim.'/'.$variable.$this->r_delim).'#s',
  189. $string,
  190. $matches,
  191. PREG_SET_ORDER
  192. );
  193. foreach ($matches as $match)
  194. {
  195. $str = '';
  196. foreach ($data as $row)
  197. {
  198. $temp = array();
  199. foreach ($row as $key => $val)
  200. {
  201. if (is_array($val))
  202. {
  203. $pair = $this->_parse_pair($key, $val, $match[1]);
  204. if ( ! empty($pair))
  205. {
  206. $temp = array_merge($temp, $pair);
  207. }
  208. continue;
  209. }
  210. $temp[$this->l_delim.$key.$this->r_delim] = $val;
  211. }
  212. $str .= strtr($match[1], $temp);
  213. }
  214. $replace[$match[0]] = $str;
  215. }
  216. return $replace;
  217. }
  218. }