PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/macro/src/compiler/lmbMacroTag.class.php

http://github.com/limb-php-framework/limb
PHP | 274 lines | 201 code | 48 blank | 25 comment | 15 complexity | 4d740cbcca8bda61950597fe3b26dad0 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. <?php
  2. /*
  3. * Limb PHP Framework
  4. *
  5. * @link http://limb-project.com
  6. * @copyright Copyright &copy; 2004-2009 BIT(http://bit-creative.com)
  7. * @license LGPL http://www.gnu.org/copyleft/lesser.html
  8. */
  9. /**
  10. * class lmbMacroTag.
  11. *
  12. * @package macro
  13. * @version $Id$
  14. */
  15. class lmbMacroTag extends lmbMacroNode
  16. {
  17. protected $tag;
  18. protected $tag_info;
  19. protected $has_closing_tag = true;
  20. protected $attributes = array();
  21. function __construct($location, $tag, $tag_info)
  22. {
  23. $this->tag = $tag;
  24. $this->tag_info = $tag_info;
  25. parent :: __construct($location);
  26. }
  27. function getTag()
  28. {
  29. return $this->tag;
  30. }
  31. function getHasClosingTag()
  32. {
  33. return $this->has_closing_tag;
  34. }
  35. function setHasClosingTag($flag)
  36. {
  37. return $this->has_closing_tag = $flag;
  38. }
  39. function getNodeId()
  40. {
  41. if($this->node_id)
  42. return $this->node_id;
  43. if($this->hasConstant('id'))
  44. $this->node_id = $this->get('id');
  45. else
  46. $this->node_id = self :: generateNewId();
  47. return $this->node_id;
  48. }
  49. function getEscapedNodeId()
  50. {
  51. $id = $this->getNodeId();
  52. return "'" . $id . "'";
  53. }
  54. function get($name)
  55. {
  56. if(!array_key_exists(strtolower($name), $this->attributes))
  57. return;
  58. return $this->attributes[strtolower($name)]->getValue();
  59. }
  60. function getAttributeObject($name)
  61. {
  62. if(!array_key_exists(strtolower($name), $this->attributes))
  63. return;
  64. return $this->attributes[strtolower($name)];
  65. }
  66. function getEscaped($name)
  67. {
  68. if(!$this->has($name))
  69. return;
  70. $value = $this->get($name);
  71. if($this->isDynamic($name))
  72. return $value;
  73. else
  74. return "'" . $value . "'";
  75. }
  76. /**
  77. * Should be used for testing purposes only since not parses $value for any output expressions
  78. */
  79. function set($name, $value)
  80. {
  81. $this->attributes[strtolower($name)] = new lmbMacroTagAttribute($name, $value);
  82. }
  83. function add($attribute)
  84. {
  85. $this->attributes[strtolower($attribute->getName())] = $attribute;
  86. }
  87. function has($name)
  88. {
  89. return array_key_exists(strtolower($name), $this->attributes);
  90. }
  91. function hasConstant($name)
  92. {
  93. return $this->has($name) && !$this->attributes[strtolower($name)]->isDynamic();
  94. }
  95. function isDynamic($name)
  96. {
  97. return !$this->hasConstant($name);
  98. }
  99. function getConstantAttributes()
  100. {
  101. $res = array();
  102. foreach($this->attributes as $key => $attr)
  103. {
  104. if(!$attr->isDynamic())
  105. $res[$attr->getName()] = $attr->getValue();
  106. }
  107. return $res;
  108. }
  109. /**
  110. * Return the value of a boolean attribute as a boolean.
  111. * ATTRIBUTE=ANYTHING (true)
  112. * ATTRIBUTE=(false|N|NA|NO|NONE|0) (false)
  113. * ATTRIBUTE (true)
  114. * (attribute unspecified) (default)
  115. */
  116. function getBool($name, $default = false)
  117. {
  118. if(!isset($this->attributes[strtolower($name)]))
  119. return $default;
  120. return self :: getBooleanValue($this->attributes[strtolower($name)]->getValue());
  121. }
  122. static function getBooleanValue($value)
  123. {
  124. if(!$value)
  125. return $value;
  126. switch(strtoupper($value))
  127. {
  128. case 'FALSE':
  129. case 'N':
  130. case 'NO':
  131. case 'NONE':
  132. case 'NA':
  133. case '0':
  134. return false;
  135. default:
  136. return true;
  137. }
  138. }
  139. function generate($code_writer)
  140. {
  141. $this->_preGenerateAttributes($code_writer);
  142. $this->_generateBeforeContent($code_writer);
  143. $this->_generateContent($code_writer);
  144. $this->_generateAfterContent($code_writer);
  145. }
  146. // children can override this method if they need to generate some code around content in simple cases
  147. // but it's recommended to override generateBeforeContent() and generateAfterContent() instead.
  148. protected function _generateContent($code_writer)
  149. {
  150. parent :: generate($code_writer);
  151. }
  152. protected function _generateBeforeContent($code_writer)
  153. {
  154. }
  155. protected function _generateAfterContent($code_writer)
  156. {
  157. }
  158. protected function _preGenerateAttributes($code_writer)
  159. {
  160. foreach($this->attributes as $attribute)
  161. $attribute->preGenerate($code_writer);
  162. }
  163. function remove($attrib)
  164. {
  165. unset($this->attributes[strtolower($attrib)]);
  166. }
  167. function raise($error, $vars = array())
  168. {
  169. $vars['tag'] = $this->tag;
  170. parent :: raise($error, $vars);
  171. }
  172. function raiseRequiredAttribute($attribute_name)
  173. {
  174. $this->raise('Missing required attribute', array('attribute' => $attribute_name));
  175. }
  176. function preParse($compiler)
  177. {
  178. foreach($this->tag_info->getRequiredAttributes() as $attr_name)
  179. {
  180. if(!$this->has($attr_name))
  181. $this->raiseRequiredAttribute($attr_name);
  182. }
  183. if($this->tag_info->isRestrictSelfNesting() && $parent = $this->findParentByClass(get_class($this)))
  184. $this->raise('Tag cannot be nested within the same tag',
  185. array('same_tag_file' => $parent->getTemplateFile(),
  186. 'same_tag_line' => $parent->getTemplateLine()));
  187. if(($parent_class = $this->tag_info->getParentClass()) &&
  188. !$parent = $this->findParentByClass($parent_class))
  189. {
  190. $this->raise('Tag must be enclosed by a proper parent tag',
  191. array('required_parent_tag_class' => $parent_class));
  192. }
  193. }
  194. function attributesIntoArgs()
  195. {
  196. $keys = array();
  197. $vals = array();
  198. foreach($this->attributes as $k => $attribute)
  199. {
  200. $keys[] = '$' . $attribute->getName();
  201. $vals[] = $this->getEscaped($k);
  202. }
  203. return array($keys, $vals);
  204. }
  205. function attributesIntoArray($skip = array())
  206. {
  207. $arr = array();
  208. foreach($this->attributes as $k => $attribute)
  209. {
  210. $name = $attribute->getName();
  211. if(in_array($name, $skip))
  212. continue;
  213. $arr[$name] = $this->getEscaped($k);
  214. }
  215. return $arr;
  216. }
  217. function attributesIntoArrayString($skip = array())
  218. {
  219. $args = $this->attributesIntoArray($skip);
  220. $arg_str = 'array(';
  221. foreach($args as $key => $value)
  222. $arg_str .= "'$key' => $value,";
  223. $arg_str .= ')';
  224. return $arg_str;
  225. }
  226. }