blob: 5a2024955e1e3c87cf4b03f2fe2db36858b1fd93 [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 Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @link http://codeigniter.com/user_guide/libraries/parser.html
48 */
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();
82 }
83
84 // --------------------------------------------------------------------
85
Derek Allard2067d1a2008-11-13 22:59:24 +000086 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030087 * Parse a template
Derek Allard2067d1a2008-11-13 22:59:24 +000088 *
Derek Allardd8270582010-01-15 17:10:56 +000089 * Parses pseudo-variables contained in the specified template view,
Derek Allard2067d1a2008-11-13 22:59:24 +000090 * replacing them with the data in the second param
91 *
Derek Allard2067d1a2008-11-13 22:59:24 +000092 * @param string
93 * @param array
94 * @param bool
95 * @return string
96 */
Greg Aker857c6f52010-11-11 17:28:49 -060097 public function parse($template, $data, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Andrey Andreevee3bc292011-12-24 01:44:13 +020099 $template = $this->CI->load->view($template, $data, TRUE);
Derek Allardd8270582010-01-15 17:10:56 +0000100
101 return $this->_parse($template, $data, $return);
102 }
103
104 // --------------------------------------------------------------------
105
106 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300107 * Parse a String
Derek Allardd8270582010-01-15 17:10:56 +0000108 *
109 * Parses pseudo-variables contained in the specified string,
110 * replacing them with the data in the second param
111 *
Derek Allardd8270582010-01-15 17:10:56 +0000112 * @param string
113 * @param array
114 * @param bool
115 * @return string
116 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200117 public function parse_string($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +0000118 {
119 return $this->_parse($template, $data, $return);
120 }
121
122 // --------------------------------------------------------------------
123
124 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300125 * Parse a template
Derek Allardd8270582010-01-15 17:10:56 +0000126 *
127 * Parses pseudo-variables contained in the specified template,
128 * replacing them with the data in the second param
129 *
Derek Allardd8270582010-01-15 17:10:56 +0000130 * @param string
131 * @param array
132 * @param bool
133 * @return string
134 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300135 protected function _parse($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +0000136 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100137 if ($template === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
139 return FALSE;
140 }
Derek Allardd8270582010-01-15 17:10:56 +0000141
Andrey Andreevb27338a2014-08-27 12:16:52 +0300142 $replace = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 foreach ($data as $key => $val)
144 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300145 $replace = array_merge(
146 $replace,
147 is_array($val)
Andrey Andreev56454792012-05-17 14:32:19 +0300148 ? $this->_parse_pair($key, $val, $template)
Andrey Andreevb27338a2014-08-27 12:16:52 +0300149 : $this->_parse_single($key, (string) $val, $template)
150 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
Derek Allardd8270582010-01-15 17:10:56 +0000152
Andrey Andreevb27338a2014-08-27 12:16:52 +0300153 unset($data);
154 $template = strtr($template, $replace);
155
Alex Bilbied261b1e2012-06-02 11:12:16 +0100156 if ($return === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200158 $this->CI->output->append_output($template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 }
Derek Allardd8270582010-01-15 17:10:56 +0000160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 return $template;
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300167 * Set the left/right variable delimiters
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 * @param string
170 * @param string
171 * @return void
172 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200173 public function set_delimiters($l = '{', $r = '}')
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
175 $this->l_delim = $l;
176 $this->r_delim = $r;
177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300182 * Parse a single key/value
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 * @param string
185 * @param string
186 * @param string
187 * @return string
188 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300189 protected function _parse_single($key, $val, $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300191 return array($this->l_delim.$key.$this->r_delim => (string) $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300197 * Parse a tag pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 *
Andrey Andreev74476e12012-03-26 15:03:40 +0300199 * Parses tag pairs: {some_tag} string... {/some_tag}
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 * @param string
202 * @param array
203 * @param string
204 * @return string
205 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300206 protected function _parse_pair($variable, $data, $string)
Barry Mienydd671972010-10-04 16:33:58 +0200207 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300208 $replace = array();
209 preg_match_all(
210 '#'.preg_quote($this->l_delim.$variable.$this->r_delim).'(.+?)'.preg_quote($this->l_delim.'/'.$variable.$this->r_delim).'#s',
211 $string,
212 $matches,
213 PREG_SET_ORDER
214 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200216 foreach ($matches as $match)
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 {
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200218 $str = '';
219 foreach ($data as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300221 $temp = array();
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200222 foreach ($row as $key => $val)
223 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300224 if (is_array($val))
225 {
Andrey Andreev47c21c62014-08-28 01:01:32 +0300226 $pair = $this->_parse_pair($key, $val, $match[1]);
Andrey Andreevb27338a2014-08-27 12:16:52 +0300227 if ( ! empty($pair))
228 {
229 $temp = array_merge($temp, $pair);
230 }
231
232 continue;
233 }
234
235 $temp[$this->l_delim.$key.$this->r_delim] = $val;
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200236 }
237
Andrey Andreevb27338a2014-08-27 12:16:52 +0300238 $str .= strtr($match[1], $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 }
Barry Mienydd671972010-10-04 16:33:58 +0200240
Andrey Andreevb27338a2014-08-27 12:16:52 +0300241 $replace[$match[0]] = $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
Barry Mienydd671972010-10-04 16:33:58 +0200243
Andrey Andreevb27338a2014-08-27 12:16:52 +0300244 return $replace;
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246
247}
Derek Allard2067d1a2008-11-13 22:59:24 +0000248
249/* End of file Parser.php */
Andrey Andreev2ecc06c2013-07-17 12:31:11 +0300250/* Location: ./system/libraries/Parser.php */