Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Parser Class |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Libraries |
| 33 | * @category Parser |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/libraries/parser.html |
| 36 | */ |
| 37 | class CI_Parser { |
| 38 | |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame^] | 39 | /** |
| 40 | * Left delimeter character for psuedo vars |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 44 | public $l_delim = '{'; |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame^] | 45 | |
| 46 | /** |
| 47 | * Right delimeter character for psuedo vars |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 51 | public $r_delim = '}'; |
Timothy Warren | 86611db | 2012-04-27 10:06:25 -0400 | [diff] [blame^] | 52 | |
| 53 | /** |
| 54 | * Reference to CodeIgniter instance |
| 55 | * |
| 56 | * @var object |
| 57 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 58 | protected $CI; |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 59 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 61 | * Parse a template |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | * |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 63 | * Parses pseudo-variables contained in the specified template view, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | * replacing them with the data in the second param |
| 65 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | * @param string |
| 67 | * @param array |
| 68 | * @param bool |
| 69 | * @return string |
| 70 | */ |
Greg Aker | 857c6f5 | 2010-11-11 17:28:49 -0600 | [diff] [blame] | 71 | public function parse($template, $data, $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 73 | $this->CI =& get_instance(); |
| 74 | $template = $this->CI->load->view($template, $data, TRUE); |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 75 | |
| 76 | return $this->_parse($template, $data, $return); |
| 77 | } |
| 78 | |
| 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 82 | * Parse a String |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 83 | * |
| 84 | * Parses pseudo-variables contained in the specified string, |
| 85 | * replacing them with the data in the second param |
| 86 | * |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 87 | * @param string |
| 88 | * @param array |
| 89 | * @param bool |
| 90 | * @return string |
| 91 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 92 | public function parse_string($template, $data, $return = FALSE) |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 93 | { |
| 94 | return $this->_parse($template, $data, $return); |
| 95 | } |
| 96 | |
| 97 | // -------------------------------------------------------------------- |
| 98 | |
| 99 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 100 | * Parse a template |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 101 | * |
| 102 | * Parses pseudo-variables contained in the specified template, |
| 103 | * replacing them with the data in the second param |
| 104 | * |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 105 | * @param string |
| 106 | * @param array |
| 107 | * @param bool |
| 108 | * @return string |
| 109 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 110 | protected function _parse($template, $data, $return = FALSE) |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 111 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | if ($template == '') |
| 113 | { |
| 114 | return FALSE; |
| 115 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 116 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | foreach ($data as $key => $val) |
| 118 | { |
| 119 | if (is_array($val)) |
| 120 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | $template = $this->_parse_pair($key, $val, $template); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | } |
| 123 | else |
| 124 | { |
| 125 | $template = $this->_parse_single($key, (string)$val, $template); |
| 126 | } |
| 127 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 128 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | if ($return == FALSE) |
| 130 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 131 | $this->CI->output->append_output($template); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 133 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | return $template; |
| 135 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 136 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 138 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 139 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 140 | * Set the left/right variable delimiters |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | * @param string |
| 143 | * @param string |
| 144 | * @return void |
| 145 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 146 | public function set_delimiters($l = '{', $r = '}') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | { |
| 148 | $this->l_delim = $l; |
| 149 | $this->r_delim = $r; |
| 150 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 152 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 153 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 155 | * Parse a single key/value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | * @param string |
| 158 | * @param string |
| 159 | * @param string |
| 160 | * @return string |
| 161 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 162 | protected function _parse_single($key, $val, $string) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 164 | return str_replace($this->l_delim.$key.$this->r_delim, (string) $val, $string); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 168 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 170 | * Parse a tag pair |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | * |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 172 | * Parses tag pairs: {some_tag} string... {/some_tag} |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | * @param string |
| 175 | * @param array |
| 176 | * @param string |
| 177 | * @return string |
| 178 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 179 | protected function _parse_pair($variable, $data, $string) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 180 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | if (FALSE === ($match = $this->_match_pair($string, $variable))) |
| 182 | { |
| 183 | return $string; |
| 184 | } |
| 185 | |
| 186 | $str = ''; |
| 187 | foreach ($data as $row) |
| 188 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 189 | $temp = $match[1]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | foreach ($row as $key => $val) |
| 191 | { |
| 192 | if ( ! is_array($val)) |
| 193 | { |
| 194 | $temp = $this->_parse_single($key, $val, $temp); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | $temp = $this->_parse_pair($key, $val, $temp); |
| 199 | } |
| 200 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 201 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | $str .= $temp; |
| 203 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 204 | |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 205 | return str_replace($match[0], $str, $string); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 207 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 208 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 209 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 210 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 211 | * Matches a variable pair |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | * @param string |
| 214 | * @param string |
| 215 | * @return mixed |
| 216 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 217 | protected function _match_pair($string, $variable) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | { |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 219 | 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', |
| 220 | $string, $match) |
| 221 | ? $match : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | |
| 226 | /* End of file Parser.php */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 227 | /* Location: ./system/libraries/Parser.php */ |