PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/Opl/Opt/Xml/Buffer.php

https://bitbucket.org/kandsten/hitta.sverok.se
PHP | 305 lines | 216 code | 22 blank | 67 comment | 33 complexity | 954846955c58b6eda1238f56aec79394 MD5 | raw file
Possible License(s): GPL-3.0, MIT
  1. <?php
  2. /*
  3. * OPEN POWER LIBS <http://www.invenzzia.org>
  4. *
  5. * This file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE. It is also available through
  7. * WWW at this URL: <http://www.invenzzia.org/license/new-bsd>
  8. *
  9. * Copyright (c) Invenzzia Group <http://www.invenzzia.org>
  10. * and other contributors. See website for details.
  11. *
  12. * $Id: Buffer.php 231 2009-09-19 07:13:14Z zyxist $
  13. */
  14. /*
  15. * This file provides an implementation of basic code buffer for all XML nodes.
  16. * The buffer allows to insert some PHP code in some predefined areas.
  17. */
  18. function OptPushArray(&$array, $from, $cnt = null)
  19. {
  20. if(!isset($array[$from]))
  21. {
  22. return;
  23. }
  24. $i = 0;
  25. if(is_null($cnt))
  26. {
  27. $cnt = sizeof($array);
  28. while($cnt > 0 && $from != $i)
  29. {
  30. if(isset($array[$i]))
  31. {
  32. $cnt--;
  33. }
  34. $i++;
  35. }
  36. }
  37. $tmp = $tmp2 = null;
  38. while($cnt > 0)
  39. {
  40. if(isset($array[$i]))
  41. {
  42. if(is_null($tmp))
  43. {
  44. $tmp = $array[$i];
  45. }
  46. else
  47. {
  48. $tmp2 = $array[$i];
  49. $array[$i] = $tmp;
  50. $tmp = $tmp2;
  51. }
  52. $cnt--;
  53. }
  54. $i++;
  55. }
  56. if(!is_null($tmp))
  57. {
  58. $array[$i] = $tmp;
  59. }
  60. } // end OptPushArray();
  61. abstract class Opt_Xml_Buffer
  62. {
  63. const TAG_BEFORE = 0;
  64. const TAG_AFTER = 1;
  65. const TAG_OPENING_BEFORE = 2;
  66. const TAG_OPENING_AFTER = 3;
  67. const TAG_CLOSING_BEFORE = 4;
  68. const TAG_CLOSING_AFTER = 5;
  69. const TAG_CONTENT_BEFORE = 6;
  70. const TAG_CONTENT_AFTER = 7;
  71. const TAG_CONTENT = 17; // For private use only
  72. const TAG_SINGLE_BEFORE = 18;
  73. const TAG_SINGLE_AFTER = 19;
  74. const TAG_NAME = 8;
  75. const TAG_ATTRIBUTES_BEFORE = 9;
  76. const TAG_ATTRIBUTES_AFTER = 10;
  77. const TAG_BEGINNING_ATTRIBUTES = 11;
  78. const TAG_ENDING_ATTRIBUTES = 12;
  79. const ATTRIBUTE_BEGIN = 13;
  80. const ATTRIBUTE_END = 14;
  81. const ATTRIBUTE_NAME = 15;
  82. const ATTRIBUTE_VALUE = 16;
  83. protected $_buffers;
  84. protected $_hidden = NULL;
  85. protected $_args = null;
  86. /**
  87. * Adds a new PHP code snippet at the end of the buffer.
  88. * @param Integer $buffer The code buffer
  89. * @param String $code The PHP code snippet
  90. */
  91. public function addAfter($buffer, $code)
  92. {
  93. if(!is_array($this->_buffers))
  94. {
  95. $this->_buffers = array();
  96. }
  97. if(!isset($this->_buffers[$buffer]))
  98. {
  99. $this->_buffers[$buffer] = array();
  100. }
  101. $this->_buffers[$buffer][] = $code;
  102. } // end addAfter();
  103. /**
  104. * Adds a new PHP code snippet at the beginning of the buffer.
  105. * @param Integer $buffer The code buffer
  106. * @param String $code The PHP code snippet
  107. */
  108. public function addBefore($buffer, $code)
  109. {
  110. if(!is_array($this->_buffers))
  111. {
  112. $this->_buffers = array();
  113. }
  114. if(!isset($this->_buffers[$buffer]))
  115. {
  116. $this->_buffers[$buffer] = array();
  117. }
  118. array_unshift($this->_buffers[$buffer], $code);
  119. } // end addBefore();
  120. /**
  121. * Copies an existing buffer from other node.
  122. * @param Opt_Xml_Buffer $from The external node that the buffer should be copied from.
  123. * @param Integer $fromBuffer The source buffer
  124. * @param Integer $toBuffer The destination buffer
  125. */
  126. public function copyBuffer(Opt_Xml_Buffer $from, $fromBuffer, $toBuffer)
  127. {
  128. if(!is_array($this->_buffers))
  129. {
  130. $this->_buffers = array();
  131. }
  132. if(!isset($this->_buffers[$toBuffer]))
  133. {
  134. $this->_buffers[$toBuffer] = $from->getBuffer($fromBuffer);
  135. }
  136. else
  137. {
  138. $this->_buffers[$toBuffer] = array_merge($from->getBuffer($fromBuffer), $this->_buffers[$toBuffer]);
  139. }
  140. } // end copyBuffer();
  141. /**
  142. * Returns the contents of the specified buffer.
  143. * @param Integer $buffer The buffer
  144. * @return Array
  145. */
  146. public function getBuffer($buffer)
  147. {
  148. if(!is_array($this->_buffers))
  149. {
  150. return array();
  151. }
  152. if(!isset($this->_buffers[$buffer]))
  153. {
  154. return array();
  155. }
  156. return $this->_buffers[$buffer];
  157. } // end getBuffer();
  158. /**
  159. * Returns the number of snippets in the specified buffer.
  160. * @param Integer $buffer The buffer
  161. * @return Integer
  162. */
  163. public function bufferSize($buffer)
  164. {
  165. if(!is_array($this->_buffers))
  166. {
  167. return 0;
  168. }
  169. if(!isset($this->_buffers[$buffer]))
  170. {
  171. return 0;
  172. }
  173. return sizeof($this->_buffers[$buffer]);
  174. } // end bufferSize();
  175. /**
  176. * Builds a valid PHP code from the specified buffers and their contents.
  177. * The buffers are specified as a list of arguments. The method also allows
  178. * to pass strings in the argument list, which are simply concatenated to
  179. * the output in the specified place.
  180. * @param Integer|String ... The buffers or hard-coded snippets between them.
  181. * @return String
  182. */
  183. public function buildCode()
  184. {
  185. if(!is_array($this->_buffers))
  186. {
  187. return '';
  188. }
  189. $list = func_get_args();
  190. $out = '';
  191. $used = false;
  192. foreach($list as $item)
  193. {
  194. if(is_string($item))
  195. {
  196. if($used)
  197. {
  198. $out .= ' echo \''.$item.'\'; ';
  199. }
  200. $used = false;
  201. continue;
  202. }
  203. if(isset($this->_buffers[$item]))
  204. {
  205. if(sizeof($this->_buffers[$item]) > 0)
  206. {
  207. $out .= implode(' ', $this->_buffers[$item]).' ';
  208. $used = true;
  209. }
  210. }
  211. else
  212. {
  213. $used = false;
  214. }
  215. }
  216. if(strlen($out) > 0)
  217. {
  218. return ($this->get('nophp') ? trim($out) : '<'.'?php '.$out.' ?'.'>');
  219. }
  220. return '';
  221. } // end buildCode();
  222. /**
  223. * Clears the buffers or the specified buffer.
  224. * @param Integer $buffer The buffer to clear.
  225. */
  226. public function clear($buffer = NULL)
  227. {
  228. if(is_null($buffer))
  229. {
  230. $this->_buffers = null;
  231. }
  232. elseif(is_array($this->_buffers) && isset($this->_buffers[$buffer]))
  233. {
  234. unset($this->_buffers[$buffer]);
  235. }
  236. } // end clear();
  237. /**
  238. * Sets the node variable.
  239. * @param String $name The node variable name.
  240. * @param Mixed $value The variable value
  241. */
  242. public function set($name, $value)
  243. {
  244. if($name == 'hidden')
  245. {
  246. $this->_hidden = $value;
  247. return;
  248. }
  249. if(!is_array($this->_args))
  250. {
  251. $this->_args = array($name => $value);
  252. }
  253. else
  254. {
  255. $this->_args[$name] = $value;
  256. }
  257. } // end set();
  258. /**
  259. * Returns the node variable value.
  260. * @param String $name The node variable name.
  261. * @return Mixed
  262. */
  263. public function get($name)
  264. {
  265. if($name == 'hidden')
  266. {
  267. return $this->_hidden;
  268. }
  269. if(!is_array($this->_args) || !isset($this->_args[$name]))
  270. {
  271. return null;
  272. }
  273. return $this->_args[$name];
  274. } // end get();
  275. /**
  276. * Destroys the object.
  277. */
  278. public function __destruct()
  279. {
  280. $this->_args = null;
  281. $this->_buffers = null;
  282. } // end __destruct();
  283. } // end Opt_Xml_Buffer;