smarty_internal_compilebase.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Smarty Internal Plugin CompileBase
  4. *
  5. * @package Smarty
  6. * @subpackage Compiler
  7. * @author Uwe Tews
  8. */
  9. /**
  10. * This class does extend all internal compile plugins
  11. */
  12. //abstract class Smarty_Internal_CompileBase implements TagCompilerInterface
  13. abstract class Smarty_Internal_CompileBase
  14. {
  15. function __construct()
  16. {
  17. // initialize valid attributes
  18. $this->required_attributes = array();
  19. $this->optional_attributes = array();
  20. }
  21. /**
  22. * This function checks if the attributes passed are valid
  23. *
  24. * The attributes passed for the tag to compile are checked against the list of required and
  25. * optional attributes. Required attributes must be present. Optional attributes are check against
  26. * against the corresponding list. The keyword '_any' specifies that any attribute will be accepted
  27. * as valid
  28. *
  29. * @todo More generallized handling of the nocache attributes in compile plugins
  30. * @param array $args attributes applied to the tag
  31. * @return array attributes for further processing
  32. */
  33. function _get_attributes ($args)
  34. {
  35. // check if all required attributes present
  36. foreach ($this->required_attributes as $attr) {
  37. if (!array_key_exists($attr, $args)) {
  38. $this->compiler->trigger_template_error("missing \"" . $attr . "\" attribute");
  39. }
  40. }
  41. // check for unallowed attributes
  42. if ($this->optional_attributes != array('_any')) {
  43. $tmp_array = array_merge($this->required_attributes, $this->optional_attributes);
  44. foreach ($args as $key => $dummy) {
  45. if (!in_array($key, $tmp_array) && $key !== 0) {
  46. $this->compiler->trigger_template_error("unexpected \"" . $key . "\" attribute");
  47. }
  48. }
  49. }
  50. return $args;
  51. }
  52. /**
  53. * Push opening tag name on stack
  54. *
  55. * Optionally additional data can be saved on stack
  56. *
  57. * @param string $open_tag the opening tag's name
  58. * @param anytype $data optional data which shall be saved on stack
  59. */
  60. function _open_tag($open_tag, $data = null)
  61. {
  62. array_push($this->compiler->_tag_stack, array($open_tag, $data));
  63. }
  64. /**
  65. * Pop closing tag
  66. *
  67. * Raise an error if this stack-top doesn't match with expected opening tags
  68. *
  69. * @param array $ |string $expected_tag the expected opening tag names
  70. * @return anytype the opening tag's name or saved data
  71. */
  72. function _close_tag($expected_tag)
  73. {
  74. if (count($this->compiler->_tag_stack) > 0) {
  75. // get stacked info
  76. list($_open_tag, $_data) = array_pop($this->compiler->_tag_stack);
  77. // open tag must match with the expected ones
  78. if (in_array($_open_tag, (array)$expected_tag)) {
  79. if (is_null($_data)) {
  80. // return opening tag
  81. return $_open_tag;
  82. } else {
  83. // return restored data
  84. return $_data;
  85. }
  86. }
  87. // wrong nesting of tags
  88. $this->compiler->trigger_template_error("unclosed {" . $_open_tag . "} tag");
  89. return;
  90. }
  91. // wrong nesting of tags
  92. $this->compiler->trigger_template_error("unexpected closing tag",$this->compiler->lex->taglineno);
  93. return;
  94. }
  95. }
  96. ?>