blob: d29eede020bc362287a7105ad78f8c6ce919c8cf [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
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 */
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 /**
41 * Left delimeter character for psuedo vars
42 *
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 /**
48 * Right delimeter character for psuedo vars
49 *
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
Derek Allard2067d1a2008-11-13 22:59:24 +000061 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030062 * Parse a template
Derek Allard2067d1a2008-11-13 22:59:24 +000063 *
Derek Allardd8270582010-01-15 17:10:56 +000064 * Parses pseudo-variables contained in the specified template view,
Derek Allard2067d1a2008-11-13 22:59:24 +000065 * replacing them with the data in the second param
66 *
Derek Allard2067d1a2008-11-13 22:59:24 +000067 * @param string
68 * @param array
69 * @param bool
70 * @return string
71 */
Greg Aker857c6f52010-11-11 17:28:49 -060072 public function parse($template, $data, $return = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
Andrey Andreevee3bc292011-12-24 01:44:13 +020074 $this->CI =& get_instance();
75 $template = $this->CI->load->view($template, $data, TRUE);
Derek Allardd8270582010-01-15 17:10:56 +000076
77 return $this->_parse($template, $data, $return);
78 }
79
80 // --------------------------------------------------------------------
81
82 /**
Andrey Andreev74476e12012-03-26 15:03:40 +030083 * Parse a String
Derek Allardd8270582010-01-15 17:10:56 +000084 *
85 * Parses pseudo-variables contained in the specified string,
86 * replacing them with the data in the second param
87 *
Derek Allardd8270582010-01-15 17:10:56 +000088 * @param string
89 * @param array
90 * @param bool
91 * @return string
92 */
Andrey Andreevee3bc292011-12-24 01:44:13 +020093 public function parse_string($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +000094 {
95 return $this->_parse($template, $data, $return);
96 }
97
98 // --------------------------------------------------------------------
99
100 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300101 * Parse a template
Derek Allardd8270582010-01-15 17:10:56 +0000102 *
103 * Parses pseudo-variables contained in the specified template,
104 * replacing them with the data in the second param
105 *
Derek Allardd8270582010-01-15 17:10:56 +0000106 * @param string
107 * @param array
108 * @param bool
109 * @return string
110 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300111 protected function _parse($template, $data, $return = FALSE)
Derek Allardd8270582010-01-15 17:10:56 +0000112 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100113 if ($template === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 {
115 return FALSE;
116 }
Derek Allardd8270582010-01-15 17:10:56 +0000117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 foreach ($data as $key => $val)
119 {
Andrey Andreev56454792012-05-17 14:32:19 +0300120 $template = is_array($val)
121 ? $this->_parse_pair($key, $val, $template)
122 : $template = $this->_parse_single($key, (string) $val, $template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 }
Derek Allardd8270582010-01-15 17:10:56 +0000124
Alex Bilbied261b1e2012-06-02 11:12:16 +0100125 if ($return === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200127 $this->CI->output->append_output($template);
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
Derek Allardd8270582010-01-15 17:10:56 +0000129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 return $template;
131 }
Barry Mienydd671972010-10-04 16:33:58 +0200132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300136 * Set the left/right variable delimiters
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 * @param string
139 * @param string
140 * @return void
141 */
Andrey Andreevee3bc292011-12-24 01:44:13 +0200142 public function set_delimiters($l = '{', $r = '}')
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
144 $this->l_delim = $l;
145 $this->r_delim = $r;
146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300151 * Parse a single key/value
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 * @param string
154 * @param string
155 * @param string
156 * @return string
157 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300158 protected function _parse_single($key, $val, $string)
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200160 return str_replace($this->l_delim.$key.$this->r_delim, (string) $val, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300166 * Parse a tag pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 *
Andrey Andreev74476e12012-03-26 15:03:40 +0300168 * Parses tag pairs: {some_tag} string... {/some_tag}
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @param string
171 * @param array
172 * @param string
173 * @return string
174 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300175 protected function _parse_pair($variable, $data, $string)
Barry Mienydd671972010-10-04 16:33:58 +0200176 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 if (FALSE === ($match = $this->_match_pair($string, $variable)))
178 {
179 return $string;
180 }
181
182 $str = '';
183 foreach ($data as $row)
184 {
Andrey Andreevee3bc292011-12-24 01:44:13 +0200185 $temp = $match[1];
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 foreach ($row as $key => $val)
187 {
Andrey Andreev56454792012-05-17 14:32:19 +0300188 $temp = is_array($val)
189 ? $this->_parse_pair($key, $val, $temp)
190 : $this->_parse_single($key, $val, $temp);
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 }
Barry Mienydd671972010-10-04 16:33:58 +0200192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 $str .= $temp;
194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Andrey Andreevee3bc292011-12-24 01:44:13 +0200196 return str_replace($match[0], $str, $string);
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 /**
Andrey Andreev74476e12012-03-26 15:03:40 +0300202 * Matches a variable pair
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 * @param string
205 * @param string
206 * @return mixed
207 */
Andrey Andreev74476e12012-03-26 15:03:40 +0300208 protected function _match_pair($string, $variable)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Andrey Andreev74476e12012-03-26 15:03:40 +0300210 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',
211 $string, $match)
212 ? $match : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
214
215}
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
217/* End of file Parser.php */
Andrey Andreev74476e12012-03-26 15:03:40 +0300218/* Location: ./system/libraries/Parser.php */