blob: c40f339b4ce35aff3fc449de1aef18712458e7de [file] [log] [blame]
Andrey Andreevee3bc292011-12-24 01:44:13 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Parser Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Parser
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/parser.html
36 */
37class CI_Parser {
38
Timothy Warren86611db2012-04-27 10:06:25 -040039 /**
40 * Left delimeter character for psuedo vars
41 *
42 * @var string
43 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020044 public $l_delim = '{';
Timothy Warren86611db2012-04-27 10:06:25 -040045
46 /**
47 * Right delimeter character for psuedo vars
48 *
49 * @var string
50 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020051 public $r_delim = '}';
Timothy Warren86611db2012-04-27 10:06:25 -040052
53 /**
54 * Reference to CodeIgniter instance
55 *
56 * @var object
57 */
Andrey Andreev74476e12012-03-26 15:03:40 +030058 protected $CI;
Derek Allardd8270582010-01-15 17:10:56 +000059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030061 * Parse a template
Derek Allard2067d1a2008-11-13 22:59:24 +000062 *
Derek Allardd8270582010-01-15 17:10:56 +000063 * Parses pseudo-variables contained in the specified template view,
Derek Allard2067d1a2008-11-13 22:59:24 +000064 * replacing them with the data in the second param
65 *
Derek Allard2067d1a2008-11-13 22:59:24 +000066 * @param string
67 * @param array
68 * @param bool
69 * @return string
70 */
Greg Aker857c6f52010-11-11 17:28:49 -060071 public function parse($template, $data, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000072 {
Andrey Andreevee3bc292011-12-24 01:44:13 +020073 $this->CI =& get_instance();
74 $template = $this->CI->load->view($template, $data, TRUE);
Derek Allardd8270582010-01-15 17:10:56 +000075
76 return $this->_parse($template, $data, $return);
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030082 * Parse a String
Derek Allardd8270582010-01-15 17:10:56 +000083 *
84 * Parses pseudo-variables contained in the specified string,
85 * replacing them with the data in the second param
86 *
Derek Allardd8270582010-01-15 17:10:56 +000087 * @param string
88 * @param array
89 * @param bool
90 * @return string
91 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020092 public function parse_string($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +000093 {
94 return $this->_parse($template, $data, $return);
95 }
96
97 // --------------------------------------------------------------------
98
99 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300100 * Parse a template
Derek Allardd8270582010-01-15 17:10:56 +0000101 *
102 * Parses pseudo-variables contained in the specified template,
103 * replacing them with the data in the second param
104 *
Derek Allardd8270582010-01-15 17:10:56 +0000105 * @param string
106 * @param array
107 * @param bool
108 * @return string
109 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300110 protected function _parse($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +0000111 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 if ($template == '')
113 {
114 return FALSE;
115 }
Derek Allardd8270582010-01-15 17:10:56 +0000116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 foreach ($data as $key => $val)
118 {
119 if (is_array($val))
120 {
Barry Mienydd671972010-10-04 16:33:58 +0200121 $template = $this->_parse_pair($key, $val, $template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
123 else
124 {
125 $template = $this->_parse_single($key, (string)$val, $template);
126 }
127 }
Derek Allardd8270582010-01-15 17:10:56 +0000128
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 if ($return == FALSE)
130 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200131 $this->CI->output->append_output($template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
Derek Allardd8270582010-01-15 17:10:56 +0000133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 return $template;
135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300140 * Set the left/right variable delimiters
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 * @param string
143 * @param string
144 * @return void
145 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200146 public function set_delimiters($l = '{', $r = '}')
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 $this->l_delim = $l;
149 $this->r_delim = $r;
150 }
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200153
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300155 * Parse a single key/value
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 * @param string
158 * @param string
159 * @param string
160 * @return string
161 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300162 protected function _parse_single($key, $val, $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200164 return str_replace($this->l_delim.$key.$this->r_delim, (string) $val, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300170 * Parse a tag pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 *
Andrey Andreev74476e12012-03-26 15:03:40 +0300172 * Parses tag pairs: {some_tag} string... {/some_tag}
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * @param string
175 * @param array
176 * @param string
177 * @return string
178 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300179 protected function _parse_pair($variable, $data, $string)
Barry Mienydd671972010-10-04 16:33:58 +0200180 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 if (FALSE === ($match = $this->_match_pair($string, $variable)))
182 {
183 return $string;
184 }
185
186 $str = '';
187 foreach ($data as $row)
188 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200189 $temp = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 foreach ($row as $key => $val)
191 {
192 if ( ! is_array($val))
193 {
194 $temp = $this->_parse_single($key, $val, $temp);
195 }
196 else
197 {
198 $temp = $this->_parse_pair($key, $val, $temp);
199 }
200 }
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 $str .= $temp;
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Andrey Andreevee3bc292011-12-24 01:44:13 +0200205 return str_replace($match[0], $str, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300211 * Matches a variable pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * @param string
214 * @param string
215 * @return mixed
216 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300217 protected function _match_pair($string, $variable)
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
Andrey Andreev74476e12012-03-26 15:03:40 +0300219 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',
220 $string, $match)
221 ? $match : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
223
224}
Derek Allard2067d1a2008-11-13 22:59:24 +0000225
226/* End of file Parser.php */
Andrey Andreev74476e12012-03-26 15:03:40 +0300227/* Location: ./system/libraries/Parser.php */