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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, 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 | |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 131 | $replace = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | foreach ($data as $key => $val) |
| 133 | { |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 134 | $replace = array_merge( |
| 135 | $replace, |
| 136 | is_array($val) |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 137 | ? $this->_parse_pair($key, $val, $template) |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 138 | : $this->_parse_single($key, (string) $val, $template) |
| 139 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 141 | |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 142 | unset($data); |
| 143 | $template = strtr($template, $replace); |
| 144 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 145 | if ($return === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 147 | $this->CI->output->append_output($template); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 148 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 149 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | return $template; |
| 151 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 152 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 154 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 156 | * Set the left/right variable delimiters |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | * @param string |
| 159 | * @param string |
| 160 | * @return void |
| 161 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 162 | public function set_delimiters($l = '{', $r = '}') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | { |
| 164 | $this->l_delim = $l; |
| 165 | $this->r_delim = $r; |
| 166 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 169 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 170 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 171 | * Parse a single key/value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 172 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | * @param string |
| 174 | * @param string |
| 175 | * @param string |
| 176 | * @return string |
| 177 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 178 | protected function _parse_single($key, $val, $string) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | { |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 180 | return array($this->l_delim.$key.$this->r_delim => (string) $val); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 182 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 183 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 186 | * Parse a tag pair |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 187 | * |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 188 | * Parses tag pairs: {some_tag} string... {/some_tag} |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | * @param string |
| 191 | * @param array |
| 192 | * @param string |
| 193 | * @return string |
| 194 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame] | 195 | protected function _parse_pair($variable, $data, $string) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 196 | { |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 197 | $replace = array(); |
| 198 | preg_match_all( |
| 199 | '#'.preg_quote($this->l_delim.$variable.$this->r_delim).'(.+?)'.preg_quote($this->l_delim.'/'.$variable.$this->r_delim).'#s', |
| 200 | $string, |
| 201 | $matches, |
| 202 | PREG_SET_ORDER |
| 203 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | |
Andrey Andreev | a9c7d18 | 2014-01-06 14:38:00 +0200 | [diff] [blame] | 205 | foreach ($matches as $match) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | { |
Andrey Andreev | a9c7d18 | 2014-01-06 14:38:00 +0200 | [diff] [blame] | 207 | $str = ''; |
| 208 | foreach ($data as $row) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | { |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 210 | $temp = array(); |
Andrey Andreev | a9c7d18 | 2014-01-06 14:38:00 +0200 | [diff] [blame] | 211 | foreach ($row as $key => $val) |
| 212 | { |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 213 | if (is_array($val)) |
| 214 | { |
Andrey Andreev | 47c21c6 | 2014-08-28 01:01:32 +0300 | [diff] [blame^] | 215 | $pair = $this->_parse_pair($key, $val, $match[1]); |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 216 | if ( ! empty($pair)) |
| 217 | { |
| 218 | $temp = array_merge($temp, $pair); |
| 219 | } |
| 220 | |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | $temp[$this->l_delim.$key.$this->r_delim] = $val; |
Andrey Andreev | a9c7d18 | 2014-01-06 14:38:00 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 227 | $str .= strtr($match[1], $temp); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 229 | |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 230 | $replace[$match[0]] = $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 231 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 232 | |
Andrey Andreev | b27338a | 2014-08-27 12:16:52 +0300 | [diff] [blame] | 233 | return $replace; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | |
| 238 | /* End of file Parser.php */ |
Andrey Andreev | 2ecc06c | 2013-07-17 12:31:11 +0300 | [diff] [blame] | 239 | /* Location: ./system/libraries/Parser.php */ |