blob: 1534e4d9d637ca17d34351d45c6d8c576d345c57 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreevee3bc292011-12-24 01:44:13 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreevee3bc292011-12-24 01:44:13 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Parser Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Parser
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/parser.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49class CI_Parser {
50
Timothy Warren86611db2012-04-27 10:06:25 -040051 /**
Dumk005e6f1e2013-04-17 12:58:34 +030052 * Left delimiter character for pseudo vars
Timothy Warren86611db2012-04-27 10:06:25 -040053 *
54 * @var string
55 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020056 public $l_delim = '{';
Andrey Andreev56454792012-05-17 14:32:19 +030057
Timothy Warren86611db2012-04-27 10:06:25 -040058 /**
Dumk005e6f1e2013-04-17 12:58:34 +030059 * Right delimiter character for pseudo vars
Timothy Warren86611db2012-04-27 10:06:25 -040060 *
61 * @var string
62 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020063 public $r_delim = '}';
Andrey Andreev56454792012-05-17 14:32:19 +030064
Timothy Warren86611db2012-04-27 10:06:25 -040065 /**
66 * Reference to CodeIgniter instance
67 *
68 * @var object
69 */
Andrey Andreev74476e12012-03-26 15:03:40 +030070 protected $CI;
Derek Allardd8270582010-01-15 17:10:56 +000071
Andrey Andreevfe367a92012-11-12 21:05:45 +020072 // --------------------------------------------------------------------
73
74 /**
75 * Class constructor
76 *
77 * @return void
78 */
79 public function __construct()
80 {
81 $this->CI =& get_instance();
Andrey Andreev90726b82015-01-20 12:39:22 +020082 log_message('info', 'Parser Class Initialized');
Andrey Andreevfe367a92012-11-12 21:05:45 +020083 }
84
85 // --------------------------------------------------------------------
86
Derek Allard2067d1a2008-11-13 22:59:24 +000087 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030088 * Parse a template
Derek Allard2067d1a2008-11-13 22:59:24 +000089 *
Derek Allardd8270582010-01-15 17:10:56 +000090 * Parses pseudo-variables contained in the specified template view,
Derek Allard2067d1a2008-11-13 22:59:24 +000091 * replacing them with the data in the second param
92 *
Derek Allard2067d1a2008-11-13 22:59:24 +000093 * @param string
94 * @param array
95 * @param bool
96 * @return string
97 */
Greg Aker857c6f52010-11-11 17:28:49 -060098 public function parse($template, $data, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000099 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200100 $template = $this->CI->load->view($template, $data, TRUE);
Derek Allardd8270582010-01-15 17:10:56 +0000101
102 return $this->_parse($template, $data, $return);
103 }
104
105 // --------------------------------------------------------------------
106
107 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300108 * Parse a String
Derek Allardd8270582010-01-15 17:10:56 +0000109 *
110 * Parses pseudo-variables contained in the specified string,
111 * replacing them with the data in the second param
112 *
Derek Allardd8270582010-01-15 17:10:56 +0000113 * @param string
114 * @param array
115 * @param bool
116 * @return string
117 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200118 public function parse_string($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +0000119 {
120 return $this->_parse($template, $data, $return);
121 }
122
123 // --------------------------------------------------------------------
124
125 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300126 * Parse a template
Derek Allardd8270582010-01-15 17:10:56 +0000127 *
128 * Parses pseudo-variables contained in the specified template,
129 * replacing them with the data in the second param
130 *
Derek Allardd8270582010-01-15 17:10:56 +0000131 * @param string
132 * @param array
133 * @param bool
134 * @return string
135 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300136 protected function _parse($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +0000137 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100138 if ($template === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
140 return FALSE;
141 }
Derek Allardd8270582010-01-15 17:10:56 +0000142
Andrey Andreevb27338a2014-08-27 12:16:52 +0300143 $replace = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 foreach ($data as $key => $val)
145 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300146 $replace = array_merge(
147 $replace,
148 is_array($val)
Andrey Andreev56454792012-05-17 14:32:19 +0300149 ? $this->_parse_pair($key, $val, $template)
Andrey Andreevb27338a2014-08-27 12:16:52 +0300150 : $this->_parse_single($key, (string) $val, $template)
151 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 }
Derek Allardd8270582010-01-15 17:10:56 +0000153
Andrey Andreevb27338a2014-08-27 12:16:52 +0300154 unset($data);
155 $template = strtr($template, $replace);
156
Alex Bilbied261b1e2012-06-02 11:12:16 +0100157 if ($return === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200159 $this->CI->output->append_output($template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 }
Derek Allardd8270582010-01-15 17:10:56 +0000161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 return $template;
163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300168 * Set the left/right variable delimiters
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @param string
171 * @param string
172 * @return void
173 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200174 public function set_delimiters($l = '{', $r = '}')
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 $this->l_delim = $l;
177 $this->r_delim = $r;
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300183 * Parse a single key/value
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 * @param string
186 * @param string
187 * @param string
188 * @return string
189 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300190 protected function _parse_single($key, $val, $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300192 return array($this->l_delim.$key.$this->r_delim => (string) $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300198 * Parse a tag pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 *
Andrey Andreev74476e12012-03-26 15:03:40 +0300200 * Parses tag pairs: {some_tag} string... {/some_tag}
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 * @param string
203 * @param array
204 * @param string
205 * @return string
206 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300207 protected function _parse_pair($variable, $data, $string)
Barry Mienydd671972010-10-04 16:33:58 +0200208 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300209 $replace = array();
210 preg_match_all(
211 '#'.preg_quote($this->l_delim.$variable.$this->r_delim).'(.+?)'.preg_quote($this->l_delim.'/'.$variable.$this->r_delim).'#s',
212 $string,
213 $matches,
214 PREG_SET_ORDER
215 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200217 foreach ($matches as $match)
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200219 $str = '';
220 foreach ($data as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300222 $temp = array();
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200223 foreach ($row as $key => $val)
224 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300225 if (is_array($val))
226 {
Andrey Andreev47c21c62014-08-28 01:01:32 +0300227 $pair = $this->_parse_pair($key, $val, $match[1]);
Andrey Andreevb27338a2014-08-27 12:16:52 +0300228 if ( ! empty($pair))
229 {
230 $temp = array_merge($temp, $pair);
231 }
232
233 continue;
234 }
235
236 $temp[$this->l_delim.$key.$this->r_delim] = $val;
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200237 }
238
Andrey Andreevb27338a2014-08-27 12:16:52 +0300239 $str .= strtr($match[1], $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
Barry Mienydd671972010-10-04 16:33:58 +0200241
Andrey Andreevb27338a2014-08-27 12:16:52 +0300242 $replace[$match[0]] = $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Andrey Andreevb27338a2014-08-27 12:16:52 +0300245 return $replace;
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247
248}