PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/modules/fuel/libraries/Fuel_parser.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 396 lines | 181 code | 43 blank | 172 comment | 11 complexity | 81269dfcf175950572dafb8afedd8feb MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php defined('BASEPATH') OR 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. * Fuel_parser Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Parser
  22. * @author David McReynolds
  23. * @link http://docs.getfuelcms.com/libraries/fuel_parser
  24. */
  25. require_once(FUEL_PATH . 'libraries/parser/Fuel_dwoo_parser.php');
  26. require_once(FUEL_PATH . 'libraries/parser/Fuel_twig_parser.php');
  27. class Fuel_parser extends Fuel_Base_library {
  28. protected $compile_dir = ''; // the directory to compile to
  29. protected $compile_dir_perms = DIR_WRITE_MODE; // the compile directory permissions
  30. protected $compile_file_perms = FILE_WRITE_MODE; // the compiled file permissions
  31. protected $allowed_functions = array(); // functions that are allowed by the parsing engine
  32. protected $delimiters = array(); // the delimiters used for parsing
  33. protected $refs = array(); // object references that get passed to the parsing engine
  34. protected $data = array(); // variables that get passed to the parsing engine
  35. protected $engine = NULL; // the actual parsing engine object
  36. // --------------------------------------------------------------------
  37. /**
  38. * Constructor
  39. *
  40. * Accepts an associative array as input, containing preferences
  41. *
  42. * @access public
  43. * @param array config preferences
  44. * @return void
  45. */
  46. public function __construct()
  47. {
  48. parent::__construct();
  49. $this->initialize();
  50. }
  51. // --------------------------------------------------------------------
  52. /**
  53. * Initialize the object and set object parameters
  54. *
  55. * Accepts an associative array as input, containing object preferences.
  56. * Also will set the values in the parameters array as properties of this object
  57. *
  58. * @access public
  59. * @param array Config preferences
  60. * @return void
  61. */
  62. public function initialize($params = array())
  63. {
  64. parent::initialize($params);
  65. $_fuel_config = array('compile_dir', 'delimiters', 'allowed_functions', 'compile_dir_perms', 'compile_file_perms', 'refs');
  66. foreach($_fuel_config as $p)
  67. {
  68. $config = $this->fuel->config('parser_'.$p);
  69. if (!is_null($config))
  70. {
  71. $this->$p = $config;
  72. }
  73. }
  74. $this->set_engine($this->fuel->config('parser_engine'));
  75. $this->create_compile_dir();
  76. }
  77. // --------------------------------------------------------------------
  78. /**
  79. * Sets parsing engine
  80. *
  81. * @access public
  82. * @param mixed Can be a string (e.g. 'dwoo', 'twig', 'ci') or an object that implements the Fuel_parser_interface
  83. * @param string Configuration parameters to pass to the parsing engine (e.g. compile_dir, delimiters, allowed_functions, refs, data)
  84. * @return object A reference to the Fuel_parser object
  85. */
  86. public function set_engine($engine = NULL, $config = array())
  87. {
  88. if (empty($engine))
  89. {
  90. $engine = $this->fuel->config('parser_engine');
  91. }
  92. if (is_string($engine))
  93. {
  94. $default_config = array(
  95. 'compile_dir' => $this->compile_dir,
  96. 'delimiters' => $this->delimiters,
  97. 'allowed_functions' => $this->allowed_functions,
  98. 'refs' => $this->refs,
  99. 'data' => $this->data,
  100. );
  101. $config = array_merge($default_config, $config);
  102. switch($engine)
  103. {
  104. case 'ci':
  105. $this->CI->load->library('parser');
  106. $this->engine = new CI_Parser();
  107. $delimiters = (isset($config['delimiters']['tag_variable'])) ? $config['delimiters']['tag_variable'] : $config['delimiters'];
  108. $l_delim = $delimiters[0];
  109. $r_delim = $delimiters[1];
  110. $this->engine->set_delimiters($l_delim, $r_delim);
  111. break;
  112. case 'twig':
  113. $this->engine = new Fuel_twig_parser($config);
  114. break;
  115. default:
  116. $config['compile_file_perms'] = $this->compile_file_perms;
  117. $this->engine = new Fuel_dwoo_parser($config);
  118. }
  119. }
  120. elseif ($engine instanceof Fuel_parser_interface OR $engine instanceof CI_Parser)
  121. {
  122. $this->engine = $engine;
  123. }
  124. else
  125. {
  126. throw(new Exception('Invalid parsing engine specified.'));
  127. }
  128. return $this;
  129. }
  130. // --------------------------------------------------------------------
  131. /**
  132. * Returns the parsing engine object
  133. *
  134. * @access public
  135. * @return object
  136. */
  137. public function get_engine()
  138. {
  139. return $this->engine;
  140. }
  141. // --------------------------------------------------------------------
  142. /**
  143. * The parsing engine's name (e.g. dwoo, twig, ci)
  144. *
  145. * @access public
  146. * @return string
  147. */
  148. public function name()
  149. {
  150. if (method_exists($this->engine, 'name'))
  151. {
  152. return $this->engine->name();
  153. }
  154. else
  155. {
  156. return 'ci';
  157. }
  158. }
  159. // --------------------------------------------------------------------
  160. /**
  161. * Sets the compiled directory path
  162. *
  163. * @access public
  164. * @param string The directory path
  165. * @return object A reference to the Fuel_parser object
  166. */
  167. public function set_compile_dir($dir)
  168. {
  169. $this->compile_dir = $dir;
  170. return $this;
  171. }
  172. // --------------------------------------------------------------------
  173. /**
  174. * Returns the compiled directory path
  175. *
  176. * @access public
  177. * @return string
  178. */
  179. public function get_compiled_dir()
  180. {
  181. return $this->compile_dir;
  182. }
  183. // --------------------------------------------------------------------
  184. /**
  185. * Sets the delimiters for the parser
  186. *
  187. * @access public
  188. * @param string
  189. * @return string
  190. */
  191. public function set_delimiters($delimiters, $type = NULL)
  192. {
  193. if (!empty($type))
  194. {
  195. $this->delimiters['tag_'.$type] = $delimiters;
  196. }
  197. else
  198. {
  199. $this->delimiters = $delimiters;
  200. }
  201. $params = array('delimiters' => $this->delimiters);
  202. $delimiters = $this->engine->set_params($params);
  203. return $this;
  204. }
  205. // --------------------------------------------------------------------
  206. /**
  207. * Gets the delimiter for the parser
  208. *
  209. * @access public
  210. * @param string The type of delimiter
  211. * @return string
  212. */
  213. public function get_delimiters($type = NULL)
  214. {
  215. if (!empty($type))
  216. {
  217. $key = 'tag_'.$type;
  218. if (isset($this->delimiters[$key]))
  219. {
  220. return $this->delimiters[$key];
  221. }
  222. }
  223. return $this->delimiters;
  224. }
  225. // --------------------------------------------------------------------
  226. /**
  227. * Adds an allowed function
  228. *
  229. * @access public
  230. * @param string The name of the function to remove
  231. * @return object A reference to the Fuel_parser object
  232. */
  233. public function add_allowed_function($func)
  234. {
  235. if (!in_array($func, $this->allowed_functions))
  236. {
  237. $this->allowed_functions[] = $func;
  238. }
  239. return $this;
  240. }
  241. // --------------------------------------------------------------------
  242. /**
  243. * Removes a function from the allowed list of functions
  244. *
  245. * @access public
  246. * @param string The name of the function to remove
  247. * @return object A reference to the Fuel_parser object
  248. */
  249. public function remove_allowed_function($func)
  250. {
  251. $this->allowed_functions = array_diff($this->allowed_functions, array($func));
  252. return $this;
  253. }
  254. // --------------------------------------------------------------------
  255. /**
  256. * Adds data to be passed to the parsing engine
  257. *
  258. * @access public
  259. * @param mixed Can be an array or a string (but if string must pass the second $value parameter)
  260. * @param mixed A value to associate with the $data (which is the $variable name... optional)
  261. * @return object A reference to the Fuel_parser object
  262. */
  263. public function add_data($data, $value = NULL)
  264. {
  265. if (is_string($data))
  266. {
  267. $data = array($data => $value);
  268. }
  269. // convert from object to array
  270. if (!is_array($data))
  271. {
  272. $data = (array)$data;
  273. }
  274. $this->data = array_merge($this->data, $data);
  275. return $this;
  276. }
  277. // --------------------------------------------------------------------
  278. /**
  279. * Removes the data passed to the parsing engine
  280. *
  281. * @access public
  282. * @return object A reference to the Fuel_parser object
  283. */
  284. public function clear_data()
  285. {
  286. $this->data = array();
  287. return $this;
  288. }
  289. // --------------------------------------------------------------------
  290. /**
  291. * Parse a view file
  292. *
  293. * Parses pseudo-variables contained in the specified template,
  294. * replacing them with the data in the second param
  295. *
  296. * @access public
  297. * @param string
  298. * @param array
  299. * @param bool
  300. * @return string
  301. */
  302. public function parse($view, $data = array(), $return = FALSE)
  303. {
  304. return $this->engine->parse($view, $data, $return);
  305. }
  306. // --------------------------------------------------------------------
  307. /**
  308. * Parses a string
  309. *
  310. * @access public
  311. * @param string
  312. * @param array
  313. * @param bool
  314. * @return string
  315. */
  316. public function parse_string($string, $data = array(), $return = TRUE)
  317. {
  318. return $this->engine->parse_string($string, $data, $return);
  319. }
  320. // --------------------------------------------------------------------
  321. /**
  322. * Creates the compile dir if possible in case it doesn't exist
  323. *
  324. * @access public
  325. * @return bool
  326. */
  327. public function create_compile_dir()
  328. {
  329. // try to create directory if it doesn't exist'
  330. if (!is_dir($this->compile_dir))
  331. {
  332. $this->CI->load->helper('directory');
  333. $created = @mkdir($this->compile_dir, $this->compile_dir_perms, TRUE);
  334. chmodr($this->compile_dir, $this->compile_dir_perms);
  335. return $created;
  336. }
  337. return FALSE;
  338. }
  339. // --------------------------------------------------------------------
  340. /**
  341. * Converts PHP syntax to the templating syntax... as best it can without getting crazy
  342. *
  343. * @access public
  344. * @param string
  345. * @return string
  346. */
  347. public function php_to_syntax($str)
  348. {
  349. return $this->engine->php_to_syntax($str);
  350. }
  351. }
  352. /* End of file Fuel_parser.php */
  353. /* Location: ./modules/fuel/libraries/Fuel_parser.php */