PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/halogy/application/libraries/MY_Parser.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 234 lines | 139 code | 30 blank | 65 comment | 35 complexity | 27c3d873562462db91f913e6f2f0577d MD5 | raw file
  1<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2/**
  3 * CodeIgniter
  4 *
  5 * An open source application development framework for PHP 4.3.2 or newer
  6 *
  7 * @package		CodeIgniter
  8 * @author		ExpressionEngine Dev Team
  9 * @copyright	Copyright (c) 2008, 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 * MY Parser Class
 20 *
 21 * @package		CodeIgniter
 22 * @subpackage	Libraries
 23 * @category	Parser
 24 * @author		Haloweb Ltd
 25 */
 26 
 27class MY_Parser extends CI_Parser {
 28	
 29	/**
 30	 *  Parse a template
 31	 *
 32	 * Parses pseudo-variables contained in the specified template,
 33	 * replacing them with the data in the second param
 34	 *
 35	 * @access	public
 36	 * @param	string
 37	 * @param	array
 38	 * @param	bool
 39	 * @return	string
 40	*/
 41	 
 42	function parse($template, $data, $return = FALSE, $include = FALSE)
 43	{	
 44		$CI =& get_instance();
 45
 46		if ($template == '')
 47		{
 48			return FALSE;
 49		}
 50
 51		if ($include === FALSE)
 52		{		
 53			$template = $CI->load->view($template, $data, TRUE);
 54		}
 55		
 56		if (isset($data) && $data != '')
 57		{
 58			foreach ($data as $key => $val)
 59			{
 60				if (is_array($val))
 61				{
 62					$template = $this->_parse_pair($key, $val, $template);		
 63				}
 64				else
 65				{
 66					$template = $this->_parse_single($key, (string)$val, $template);
 67				}
 68			}
 69		}
 70
 71		// Check for conditional statments
 72		$template = $this->conditionals($template, $data);
 73
 74		// populate form tags
 75		preg_match_all('/{form:(.+)}/i', $template, $posts);
 76		if ($posts)
 77		{
 78			foreach ($posts[1] as $post => $value)
 79			{
 80				if ($postValue = $CI->input->post($value))
 81				{
 82					$template = str_replace('{form:'.$value.'}', $postValue, $template);
 83				}
 84				else
 85				{
 86					$template = str_replace('{form:'.$value.'}', '', $template);
 87				}
 88			}
 89		}
 90		
 91		//$template = preg_replace('/{(.+)}/i', '', $template);
 92		
 93		if ($return == FALSE)
 94		{
 95			$CI->output->append_output($template);
 96		}
 97		
 98		return $template;
 99	}
100
101	// --------------------------------------------------------------------
102
103	/**
104	 *  Parse conditional statments
105	 * 
106	 * @access    public
107	 * @param    string
108	 * @param    bool
109	 * @return    string
110	*/
111	
112	function conditionals($template, $data)
113	{
114		if (preg_match_all('/'.$this->l_delim.'if (.+)'.$this->r_delim.'(.+)'.$this->l_delim.'\/if'.$this->r_delim.'/siU', $template, $conditionals, PREG_SET_ORDER))
115		{
116			if (count($conditionals) > 0)
117			{
118				// filter through conditionals
119				foreach($conditionals as $condition)
120				{
121					// get conditional and the string inside
122					$code = (isset($condition[0])) ? $condition[0] : FALSE;
123					$condString = (isset($condition[1])) ? str_replace(' ', '', $condition[1]) : FALSE;
124					$insert = (isset($condition[2])) ? $condition[2] : '';
125
126					// check code is valid
127					if (!preg_match('/('.$this->l_delim.'|'.$this->r_delim.')/', $condString, $condProblem))
128					{
129						if (!empty($code) || $condString != FALSE || !empty($insert))
130						{
131							if (preg_match("/^!(.*)$/", $condString, $matches))
132							{
133								$condVar = (@!$data[trim($matches[1])]) ? 0 : $data[trim($matches[1])];
134
135								@$result = (!$condVar) ? TRUE : FALSE;
136							}
137							elseif (preg_match("/([a-z0-9\-_:\(\)]+)(\!=|=|==|>|<)([a-z0-9\-_\/]+)/", $condString, $matches))
138							{
139								$condVar = (@!$data[$matches[1]]) ? 0 : $data[trim($matches[1])];
140
141								if ($matches[2] == '==' || $matches[2] == '=')
142								{
143									@$result = ($condVar === $matches[3]) ? TRUE : FALSE;
144								}
145								elseif ($matches[2] == '!=')
146								{
147									@$result = ($condVar !== $matches[3]) ? TRUE : FALSE;
148								}
149								elseif ($matches[2] == '>')
150								{
151									@$result = ($condVar > $matches[3]) ? TRUE : FALSE;
152								}
153								elseif ($matches[2] == '<')
154								{
155									@$result = ($condVar < $matches[3]) ? TRUE : FALSE;
156								}							
157							}
158							else
159							{
160								// if the variable is set
161								if (isset($data[$condString]) && is_array($data[$condString]))
162								{
163									$result = (count($data[$condString]) > 0) ? TRUE : FALSE;							
164								}
165								else
166								{
167									$result = (isset($data[$condString]) && $data[$condString] != '') ? TRUE : FALSE;
168								}
169							}
170
171							// filter for else
172							$insert = preg_split('/'.$this->l_delim.'else'.$this->r_delim.'/siU', $insert);
173
174							if ($result == TRUE)
175							{
176								// show the string inside
177								$template = str_replace($code, $insert[0], $template);
178							}
179							else
180							{
181								if (is_array($insert))
182								{
183									$insert = (isset($insert[1])) ? $insert[1] : '';
184									$template = str_replace($code, $insert, $template);
185								}
186								else
187								{
188									$template = str_replace($code, '', $template);
189								}
190							}
191						}
192					}
193					else
194					{
195						// remove any conditionals we cant process
196						$template = str_replace($code, '', $template);
197					}
198				}
199			}					
200
201			//print_r($conditionals);
202		}
203		
204		return $template;
205	}
206
207	// --------------------------------------------------------------------
208	
209	/**
210	 *  Matches a variable pair
211	 *
212	 * @access	private
213	 * @param	string
214	 * @param	string
215	 * @return	mixed
216	 */ 
217	 
218	function _match_pair($string, $variable)
219	{
220		$variable = str_replace('(', '\(', $variable);
221		$variable = str_replace(')', '\)', $variable);		
222
223		if ( ! preg_match("|".$this->l_delim . $variable . $this->r_delim."(.+?)".$this->l_delim . '/' . $variable . $this->r_delim."|s", $string, $match))
224		{
225			return FALSE;
226		}
227		
228		return $match;
229	}	
230
231}
232// END Parser Class
233
234/* End of file */