blob: d23a53423afec3e83f282766b463cd192acd4777 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevee3bc292011-12-24 01:44:13 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevee3bc292011-12-24 01:44:13 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Parser Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Parser
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/parser.html
37 */
38class CI_Parser {
39
Timothy Warren86611db2012-04-27 10:06:25 -040040 /**
Dumk005e6f1e2013-04-17 12:58:34 +030041 * Left delimiter character for pseudo vars
Timothy Warren86611db2012-04-27 10:06:25 -040042 *
43 * @var string
44 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020045 public $l_delim = '{';
Andrey Andreev56454792012-05-17 14:32:19 +030046
Timothy Warren86611db2012-04-27 10:06:25 -040047 /**
Dumk005e6f1e2013-04-17 12:58:34 +030048 * Right delimiter character for pseudo vars
Timothy Warren86611db2012-04-27 10:06:25 -040049 *
50 * @var string
51 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020052 public $r_delim = '}';
Andrey Andreev56454792012-05-17 14:32:19 +030053
Timothy Warren86611db2012-04-27 10:06:25 -040054 /**
55 * Reference to CodeIgniter instance
56 *
57 * @var object
58 */
Andrey Andreev74476e12012-03-26 15:03:40 +030059 protected $CI;
Derek Allardd8270582010-01-15 17:10:56 +000060
Andrey Andreevfe367a92012-11-12 21:05:45 +020061 // --------------------------------------------------------------------
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 Allard2067d1a2008-11-13 22:59:24 +000075 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030076 * Parse a template
Derek Allard2067d1a2008-11-13 22:59:24 +000077 *
Derek Allardd8270582010-01-15 17:10:56 +000078 * Parses pseudo-variables contained in the specified template view,
Derek Allard2067d1a2008-11-13 22:59:24 +000079 * replacing them with the data in the second param
80 *
Derek Allard2067d1a2008-11-13 22:59:24 +000081 * @param string
82 * @param array
83 * @param bool
84 * @return string
85 */
Greg Aker857c6f52010-11-11 17:28:49 -060086 public function parse($template, $data, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000087 {
Andrey Andreevee3bc292011-12-24 01:44:13 +020088 $template = $this->CI->load->view($template, $data, TRUE);
Derek Allardd8270582010-01-15 17:10:56 +000089
90 return $this->_parse($template, $data, $return);
91 }
92
93 // --------------------------------------------------------------------
94
95 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030096 * Parse a String
Derek Allardd8270582010-01-15 17:10:56 +000097 *
98 * Parses pseudo-variables contained in the specified string,
99 * replacing them with the data in the second param
100 *
Derek Allardd8270582010-01-15 17:10:56 +0000101 * @param string
102 * @param array
103 * @param bool
104 * @return string
105 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200106 public function parse_string($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +0000107 {
108 return $this->_parse($template, $data, $return);
109 }
110
111 // --------------------------------------------------------------------
112
113 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300114 * Parse a template
Derek Allardd8270582010-01-15 17:10:56 +0000115 *
116 * Parses pseudo-variables contained in the specified template,
117 * replacing them with the data in the second param
118 *
Derek Allardd8270582010-01-15 17:10:56 +0000119 * @param string
120 * @param array
121 * @param bool
122 * @return string
123 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300124 protected function _parse($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +0000125 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100126 if ($template === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
128 return FALSE;
129 }
Derek Allardd8270582010-01-15 17:10:56 +0000130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 foreach ($data as $key => $val)
132 {
Andrey Andreev56454792012-05-17 14:32:19 +0300133 $template = is_array($val)
134 ? $this->_parse_pair($key, $val, $template)
135 : $template = $this->_parse_single($key, (string) $val, $template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 }
Derek Allardd8270582010-01-15 17:10:56 +0000137
Alex Bilbied261b1e2012-06-02 11:12:16 +0100138 if ($return === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200140 $this->CI->output->append_output($template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 }
Derek Allardd8270582010-01-15 17:10:56 +0000142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 return $template;
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300149 * Set the left/right variable delimiters
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 * @param string
152 * @param string
153 * @return void
154 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200155 public function set_delimiters($l = '{', $r = '}')
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
157 $this->l_delim = $l;
158 $this->r_delim = $r;
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300164 * Parse a single key/value
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 * @param string
167 * @param string
168 * @param string
169 * @return string
170 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300171 protected function _parse_single($key, $val, $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200173 return str_replace($this->l_delim.$key.$this->r_delim, (string) $val, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300179 * Parse a tag pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 *
Andrey Andreev74476e12012-03-26 15:03:40 +0300181 * Parses tag pairs: {some_tag} string... {/some_tag}
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 * @param string
184 * @param array
185 * @param string
186 * @return string
187 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300188 protected function _parse_pair($variable, $data, $string)
Barry Mienydd671972010-10-04 16:33:58 +0200189 {
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200190 if (FALSE === ($matches = $this->_match_pair($string, $variable)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
192 return $string;
193 }
194
195 $str = '';
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200196 $search = $replace = array();
197 foreach ($matches as $match)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200199 $str = '';
200 foreach ($data as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 {
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200202 $temp = $match[1];
203 foreach ($row as $key => $val)
204 {
205 $temp = is_array($val)
Andrey Andreev56454792012-05-17 14:32:19 +0300206 ? $this->_parse_pair($key, $val, $temp)
207 : $this->_parse_single($key, $val, $temp);
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200208 }
209
210 $str .= $temp;
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
Barry Mienydd671972010-10-04 16:33:58 +0200212
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200213 $search[] = $match[0];
214 $replace[] = $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 }
Barry Mienydd671972010-10-04 16:33:58 +0200216
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200217 return str_replace($search, $replace, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300223 * Matches a variable pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 *
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200225 * @param string $string
226 * @param string $variable
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 * @return mixed
228 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300229 protected function _match_pair($string, $variable)
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200231 return preg_match_all('|'.preg_quote($this->l_delim).$variable.preg_quote($this->r_delim).'(.+?)'.preg_quote($this->l_delim).'/'.$variable.preg_quote($this->r_delim).'|s',
232 $string, $match, PREG_SET_ORDER)
Andrey Andreev74476e12012-03-26 15:03:40 +0300233 ? $match : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 }
235
236}
Derek Allard2067d1a2008-11-13 22:59:24 +0000237
238/* End of file Parser.php */
Andrey Andreev2ecc06c2013-07-17 12:31:11 +0300239/* Location: ./system/libraries/Parser.php */