CIS-LB: Randomized suboptimal load balancing implemented without low timeout features.
[living-lab-site.git] / system / libraries / Parser.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 5.1.6 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10  * @license             http://codeigniter.com/user_guide/license.html
11  * @link                http://codeigniter.com
12  * @since               Version 1.0
13  * @filesource
14  */
15
16 // ------------------------------------------------------------------------
17
18 /**
19  * Parser Class
20  *
21  * @package             CodeIgniter
22  * @subpackage  Libraries
23  * @category    Parser
24  * @author              ExpressionEngine Dev Team
25  * @link                http://codeigniter.com/user_guide/libraries/parser.html
26  */
27 class CI_Parser {
28
29         var $l_delim = '{';
30         var $r_delim = '}';
31         var $object;
32
33         /**
34          *  Parse a template
35          *
36          * Parses pseudo-variables contained in the specified template view,
37          * replacing them with the data in the second param
38          *
39          * @access      public
40          * @param       string
41          * @param       array
42          * @param       bool
43          * @return      string
44          */
45         public function parse($template, $data, $return = FALSE)
46         {
47                 $CI =& get_instance();
48                 $template = $CI->load->view($template, $data, TRUE);
49
50                 return $this->_parse($template, $data, $return);
51         }
52
53         // --------------------------------------------------------------------
54
55         /**
56          *  Parse a String
57          *
58          * Parses pseudo-variables contained in the specified string,
59          * replacing them with the data in the second param
60          *
61          * @access      public
62          * @param       string
63          * @param       array
64          * @param       bool
65          * @return      string
66          */
67         function parse_string($template, $data, $return = FALSE)
68         {
69                 return $this->_parse($template, $data, $return);
70         }
71
72         // --------------------------------------------------------------------
73
74         /**
75          *  Parse a template
76          *
77          * Parses pseudo-variables contained in the specified template,
78          * replacing them with the data in the second param
79          *
80          * @access      public
81          * @param       string
82          * @param       array
83          * @param       bool
84          * @return      string
85          */
86         function _parse($template, $data, $return = FALSE)
87         {
88                 if ($template == '')
89                 {
90                         return FALSE;
91                 }
92
93                 foreach ($data as $key => $val)
94                 {
95                         if (is_array($val))
96                         {
97                                 $template = $this->_parse_pair($key, $val, $template);
98                         }
99                         else
100                         {
101                                 $template = $this->_parse_single($key, (string)$val, $template);
102                         }
103                 }
104
105                 if ($return == FALSE)
106                 {
107                         $CI =& get_instance();
108                         $CI->output->append_output($template);
109                 }
110
111                 return $template;
112         }
113
114         // --------------------------------------------------------------------
115
116         /**
117          *  Set the left/right variable delimiters
118          *
119          * @access      public
120          * @param       string
121          * @param       string
122          * @return      void
123          */
124         function set_delimiters($l = '{', $r = '}')
125         {
126                 $this->l_delim = $l;
127                 $this->r_delim = $r;
128         }
129
130         // --------------------------------------------------------------------
131
132         /**
133          *  Parse a single key/value
134          *
135          * @access      private
136          * @param       string
137          * @param       string
138          * @param       string
139          * @return      string
140          */
141         function _parse_single($key, $val, $string)
142         {
143                 return str_replace($this->l_delim.$key.$this->r_delim, $val, $string);
144         }
145
146         // --------------------------------------------------------------------
147
148         /**
149          *  Parse a tag pair
150          *
151          * Parses tag pairs:  {some_tag} string... {/some_tag}
152          *
153          * @access      private
154          * @param       string
155          * @param       array
156          * @param       string
157          * @return      string
158          */
159         function _parse_pair($variable, $data, $string)
160         {
161                 if (FALSE === ($match = $this->_match_pair($string, $variable)))
162                 {
163                         return $string;
164                 }
165
166                 $str = '';
167                 foreach ($data as $row)
168                 {
169                         $temp = $match['1'];
170                         foreach ($row as $key => $val)
171                         {
172                                 if ( ! is_array($val))
173                                 {
174                                         $temp = $this->_parse_single($key, $val, $temp);
175                                 }
176                                 else
177                                 {
178                                         $temp = $this->_parse_pair($key, $val, $temp);
179                                 }
180                         }
181
182                         $str .= $temp;
183                 }
184
185                 return str_replace($match['0'], $str, $string);
186         }
187
188         // --------------------------------------------------------------------
189
190         /**
191          *  Matches a variable pair
192          *
193          * @access      private
194          * @param       string
195          * @param       string
196          * @return      mixed
197          */
198         function _match_pair($string, $variable)
199         {
200                 if ( ! 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", $string, $match))
201                 {
202                         return FALSE;
203                 }
204
205                 return $match;
206         }
207
208 }
209 // END Parser Class
210
211 /* End of file Parser.php */
212 /* Location: ./system/libraries/Parser.php */