blob: d1b5b764bd2e7ae01d083bc30aa08f8856867d5d [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
Andrey Andreevee3bc292011-12-24 01:44:13 +020039 public $l_delim = '{';
40 public $r_delim = '}';
41 public $object;
Andrey Andreev74476e12012-03-26 15:03:40 +030042 protected $CI;
Derek Allardd8270582010-01-15 17:10:56 +000043
Derek Allard2067d1a2008-11-13 22:59:24 +000044 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030045 * Parse a template
Derek Allard2067d1a2008-11-13 22:59:24 +000046 *
Derek Allardd8270582010-01-15 17:10:56 +000047 * Parses pseudo-variables contained in the specified template view,
Derek Allard2067d1a2008-11-13 22:59:24 +000048 * replacing them with the data in the second param
49 *
Derek Allard2067d1a2008-11-13 22:59:24 +000050 * @param string
51 * @param array
52 * @param bool
53 * @return string
54 */
Greg Aker857c6f52010-11-11 17:28:49 -060055 public function parse($template, $data, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000056 {
Andrey Andreevee3bc292011-12-24 01:44:13 +020057 $this->CI =& get_instance();
58 $template = $this->CI->load->view($template, $data, TRUE);
Derek Allardd8270582010-01-15 17:10:56 +000059
60 return $this->_parse($template, $data, $return);
61 }
62
63 // --------------------------------------------------------------------
64
65 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030066 * Parse a String
Derek Allardd8270582010-01-15 17:10:56 +000067 *
68 * Parses pseudo-variables contained in the specified string,
69 * replacing them with the data in the second param
70 *
Derek Allardd8270582010-01-15 17:10:56 +000071 * @param string
72 * @param array
73 * @param bool
74 * @return string
75 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020076 public function parse_string($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +000077 {
78 return $this->_parse($template, $data, $return);
79 }
80
81 // --------------------------------------------------------------------
82
83 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030084 * Parse a template
Derek Allardd8270582010-01-15 17:10:56 +000085 *
86 * Parses pseudo-variables contained in the specified template,
87 * replacing them with the data in the second param
88 *
Derek Allardd8270582010-01-15 17:10:56 +000089 * @param string
90 * @param array
91 * @param bool
92 * @return string
93 */
Andrey Andreev74476e12012-03-26 15:03:40 +030094 protected function _parse($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +000095 {
Derek Allard2067d1a2008-11-13 22:59:24 +000096 if ($template == '')
97 {
98 return FALSE;
99 }
Derek Allardd8270582010-01-15 17:10:56 +0000100
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 foreach ($data as $key => $val)
102 {
103 if (is_array($val))
104 {
Barry Mienydd671972010-10-04 16:33:58 +0200105 $template = $this->_parse_pair($key, $val, $template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 }
107 else
108 {
109 $template = $this->_parse_single($key, (string)$val, $template);
110 }
111 }
Derek Allardd8270582010-01-15 17:10:56 +0000112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 if ($return == FALSE)
114 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200115 $this->CI->output->append_output($template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
Derek Allardd8270582010-01-15 17:10:56 +0000117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 return $template;
119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300124 * Set the left/right variable delimiters
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * @param string
127 * @param string
128 * @return void
129 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200130 public function set_delimiters($l = '{', $r = '}')
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
132 $this->l_delim = $l;
133 $this->r_delim = $r;
134 }
Barry Mienydd671972010-10-04 16:33:58 +0200135
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300139 * Parse a single key/value
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 * @param string
142 * @param string
143 * @param string
144 * @return string
145 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300146 protected function _parse_single($key, $val, $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200148 return str_replace($this->l_delim.$key.$this->r_delim, (string) $val, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 }
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300154 * Parse a tag pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 *
Andrey Andreev74476e12012-03-26 15:03:40 +0300156 * Parses tag pairs: {some_tag} string... {/some_tag}
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 * @param string
159 * @param array
160 * @param string
161 * @return string
162 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300163 protected function _parse_pair($variable, $data, $string)
Barry Mienydd671972010-10-04 16:33:58 +0200164 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 if (FALSE === ($match = $this->_match_pair($string, $variable)))
166 {
167 return $string;
168 }
169
170 $str = '';
171 foreach ($data as $row)
172 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200173 $temp = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 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 Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 $str .= $temp;
187 }
Barry Mienydd671972010-10-04 16:33:58 +0200188
Andrey Andreevee3bc292011-12-24 01:44:13 +0200189 return str_replace($match[0], $str, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 }
Barry Mienydd671972010-10-04 16:33:58 +0200191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300195 * Matches a variable pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 * @param string
198 * @param string
199 * @return mixed
200 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300201 protected function _match_pair($string, $variable)
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 {
Andrey Andreev74476e12012-03-26 15:03:40 +0300203 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 Allard2067d1a2008-11-13 22:59:24 +0000206 }
207
208}
Derek Allard2067d1a2008-11-13 22:59:24 +0000209
210/* End of file Parser.php */
Andrey Andreev74476e12012-03-26 15:03:40 +0300211/* Location: ./system/libraries/Parser.php */