blob: 2c2fc73b6e93630b187d07d75b5864d8dc36e91e [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
Andrey Andreevb27338a2014-08-27 12:16:52 +0300131 $replace = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 foreach ($data as $key => $val)
133 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300134 $replace = array_merge(
135 $replace,
136 is_array($val)
Andrey Andreev56454792012-05-17 14:32:19 +0300137 ? $this->_parse_pair($key, $val, $template)
Andrey Andreevb27338a2014-08-27 12:16:52 +0300138 : $this->_parse_single($key, (string) $val, $template)
139 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 }
Derek Allardd8270582010-01-15 17:10:56 +0000141
Andrey Andreevb27338a2014-08-27 12:16:52 +0300142 unset($data);
143 $template = strtr($template, $replace);
144
Alex Bilbied261b1e2012-06-02 11:12:16 +0100145 if ($return === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200147 $this->CI->output->append_output($template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 }
Derek Allardd8270582010-01-15 17:10:56 +0000149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 return $template;
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300156 * Set the left/right variable delimiters
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @param string
159 * @param string
160 * @return void
161 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200162 public function set_delimiters($l = '{', $r = '}')
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 $this->l_delim = $l;
165 $this->r_delim = $r;
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300171 * Parse a single key/value
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * @param string
174 * @param string
175 * @param string
176 * @return string
177 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300178 protected function _parse_single($key, $val, $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300180 return array($this->l_delim.$key.$this->r_delim => (string) $val);
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 }
Barry Mienydd671972010-10-04 16:33:58 +0200182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300186 * Parse a tag pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 *
Andrey Andreev74476e12012-03-26 15:03:40 +0300188 * Parses tag pairs: {some_tag} string... {/some_tag}
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @param string
191 * @param array
192 * @param string
193 * @return string
194 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300195 protected function _parse_pair($variable, $data, $string)
Barry Mienydd671972010-10-04 16:33:58 +0200196 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300197 $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 Allard2067d1a2008-11-13 22:59:24 +0000204
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200205 foreach ($matches as $match)
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 {
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200207 $str = '';
208 foreach ($data as $row)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300210 $temp = array();
Andrey Andreeva9c7d182014-01-06 14:38:00 +0200211 foreach ($row as $key => $val)
212 {
Andrey Andreevb27338a2014-08-27 12:16:52 +0300213 if (is_array($val))
214 {
Andrey Andreev47c21c62014-08-28 01:01:32 +0300215 $pair = $this->_parse_pair($key, $val, $match[1]);
Andrey Andreevb27338a2014-08-27 12:16:52 +0300216 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 Andreeva9c7d182014-01-06 14:38:00 +0200225 }
226
Andrey Andreevb27338a2014-08-27 12:16:52 +0300227 $str .= strtr($match[1], $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
Barry Mienydd671972010-10-04 16:33:58 +0200229
Andrey Andreevb27338a2014-08-27 12:16:52 +0300230 $replace[$match[0]] = $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Andrey Andreevb27338a2014-08-27 12:16:52 +0300233 return $replace;
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 */