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 | |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 39 | public $l_delim = '{'; |
| 40 | public $r_delim = '}'; |
| 41 | public $object; |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 42 | protected $CI; |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 43 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 45 | * Parse a template |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | * |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 47 | * Parses pseudo-variables contained in the specified template view, |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | * replacing them with the data in the second param |
| 49 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | * @param string |
| 51 | * @param array |
| 52 | * @param bool |
| 53 | * @return string |
| 54 | */ |
Greg Aker | 857c6f5 | 2010-11-11 17:28:49 -0600 | [diff] [blame] | 55 | public function parse($template, $data, $return = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 57 | $this->CI =& get_instance(); |
| 58 | $template = $this->CI->load->view($template, $data, TRUE); |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 59 | |
| 60 | return $this->_parse($template, $data, $return); |
| 61 | } |
| 62 | |
| 63 | // -------------------------------------------------------------------- |
| 64 | |
| 65 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 66 | * Parse a String |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 67 | * |
| 68 | * Parses pseudo-variables contained in the specified string, |
| 69 | * replacing them with the data in the second param |
| 70 | * |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 71 | * @param string |
| 72 | * @param array |
| 73 | * @param bool |
| 74 | * @return string |
| 75 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 76 | public function parse_string($template, $data, $return = FALSE) |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 77 | { |
| 78 | return $this->_parse($template, $data, $return); |
| 79 | } |
| 80 | |
| 81 | // -------------------------------------------------------------------- |
| 82 | |
| 83 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 84 | * Parse a template |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 85 | * |
| 86 | * Parses pseudo-variables contained in the specified template, |
| 87 | * replacing them with the data in the second param |
| 88 | * |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 89 | * @param string |
| 90 | * @param array |
| 91 | * @param bool |
| 92 | * @return string |
| 93 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 94 | protected function _parse($template, $data, $return = FALSE) |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 95 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | if ($template == '') |
| 97 | { |
| 98 | return FALSE; |
| 99 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 100 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | foreach ($data as $key => $val) |
| 102 | { |
| 103 | if (is_array($val)) |
| 104 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 105 | $template = $this->_parse_pair($key, $val, $template); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | } |
| 107 | else |
| 108 | { |
| 109 | $template = $this->_parse_single($key, (string)$val, $template); |
| 110 | } |
| 111 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 112 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | if ($return == FALSE) |
| 114 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 115 | $this->CI->output->append_output($template); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | } |
Derek Allard | d827058 | 2010-01-15 17:10:56 +0000 | [diff] [blame] | 117 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | return $template; |
| 119 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 122 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 124 | * Set the left/right variable delimiters |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | * @param string |
| 127 | * @param string |
| 128 | * @return void |
| 129 | */ |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 130 | public function set_delimiters($l = '{', $r = '}') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | { |
| 132 | $this->l_delim = $l; |
| 133 | $this->r_delim = $r; |
| 134 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 135 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 137 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 139 | * Parse a single key/value |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | * @param string |
| 142 | * @param string |
| 143 | * @param string |
| 144 | * @return string |
| 145 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 146 | protected function _parse_single($key, $val, $string) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 148 | 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] | 149 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 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 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 154 | * Parse a tag pair |
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 | * Parses tag pairs: {some_tag} string... {/some_tag} |
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 array |
| 160 | * @param string |
| 161 | * @return string |
| 162 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 163 | protected function _parse_pair($variable, $data, $string) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | if (FALSE === ($match = $this->_match_pair($string, $variable))) |
| 166 | { |
| 167 | return $string; |
| 168 | } |
| 169 | |
| 170 | $str = ''; |
| 171 | foreach ($data as $row) |
| 172 | { |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 173 | $temp = $match[1]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | foreach ($row as $key => $val) |
| 175 | { |
| 176 | if ( ! is_array($val)) |
| 177 | { |
| 178 | $temp = $this->_parse_single($key, $val, $temp); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | $temp = $this->_parse_pair($key, $val, $temp); |
| 183 | } |
| 184 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 185 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | $str .= $temp; |
| 187 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 188 | |
Andrey Andreev | ee3bc29 | 2011-12-24 01:44:13 +0200 | [diff] [blame] | 189 | return str_replace($match[0], $str, $string); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 191 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 193 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | /** |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 195 | * Matches a variable pair |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | * @param string |
| 198 | * @param string |
| 199 | * @return mixed |
| 200 | */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 201 | protected function _match_pair($string, $variable) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | { |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 203 | 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', |
| 204 | $string, $match) |
| 205 | ? $match : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | |
| 210 | /* End of file Parser.php */ |
Andrey Andreev | 74476e1 | 2012-03-26 15:03:40 +0300 | [diff] [blame^] | 211 | /* Location: ./system/libraries/Parser.php */ |