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