TokenFactory.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Factory for token generation.
  4. *
  5. * @note Doing some benchmarking indicates that the new operator is much
  6. * slower than the clone operator (even discounting the cost of the
  7. * constructor). This class is for that optimization.
  8. * Other then that, there's not much point as we don't
  9. * maintain parallel HTMLPurifier_Token hierarchies (the main reason why
  10. * you'd want to use an abstract factory).
  11. * @todo Port DirectLex to use this
  12. */
  13. class HTMLPurifier_TokenFactory
  14. {
  15. // p stands for prototype
  16. /**
  17. * @type HTMLPurifier_Token_Start
  18. */
  19. private $p_start;
  20. /**
  21. * @type HTMLPurifier_Token_End
  22. */
  23. private $p_end;
  24. /**
  25. * @type HTMLPurifier_Token_Empty
  26. */
  27. private $p_empty;
  28. /**
  29. * @type HTMLPurifier_Token_Text
  30. */
  31. private $p_text;
  32. /**
  33. * @type HTMLPurifier_Token_Comment
  34. */
  35. private $p_comment;
  36. /**
  37. * Generates blank prototypes for cloning.
  38. */
  39. public function __construct()
  40. {
  41. $this->p_start = new HTMLPurifier_Token_Start('', array());
  42. $this->p_end = new HTMLPurifier_Token_End('');
  43. $this->p_empty = new HTMLPurifier_Token_Empty('', array());
  44. $this->p_text = new HTMLPurifier_Token_Text('');
  45. $this->p_comment = new HTMLPurifier_Token_Comment('');
  46. }
  47. /**
  48. * Creates a HTMLPurifier_Token_Start.
  49. * @param string $name Tag name
  50. * @param array $attr Associative array of attributes
  51. * @return HTMLPurifier_Token_Start Generated HTMLPurifier_Token_Start
  52. */
  53. public function createStart($name, $attr = array())
  54. {
  55. $p = clone $this->p_start;
  56. $p->__construct($name, $attr);
  57. return $p;
  58. }
  59. /**
  60. * Creates a HTMLPurifier_Token_End.
  61. * @param string $name Tag name
  62. * @return HTMLPurifier_Token_End Generated HTMLPurifier_Token_End
  63. */
  64. public function createEnd($name)
  65. {
  66. $p = clone $this->p_end;
  67. $p->__construct($name);
  68. return $p;
  69. }
  70. /**
  71. * Creates a HTMLPurifier_Token_Empty.
  72. * @param string $name Tag name
  73. * @param array $attr Associative array of attributes
  74. * @return HTMLPurifier_Token_Empty Generated HTMLPurifier_Token_Empty
  75. */
  76. public function createEmpty($name, $attr = array())
  77. {
  78. $p = clone $this->p_empty;
  79. $p->__construct($name, $attr);
  80. return $p;
  81. }
  82. /**
  83. * Creates a HTMLPurifier_Token_Text.
  84. * @param string $data Data of text token
  85. * @return HTMLPurifier_Token_Text Generated HTMLPurifier_Token_Text
  86. */
  87. public function createText($data)
  88. {
  89. $p = clone $this->p_text;
  90. $p->__construct($data);
  91. return $p;
  92. }
  93. /**
  94. * Creates a HTMLPurifier_Token_Comment.
  95. * @param string $data Data of comment token
  96. * @return HTMLPurifier_Token_Comment Generated HTMLPurifier_Token_Comment
  97. */
  98. public function createComment($data)
  99. {
  100. $p = clone $this->p_comment;
  101. $p->__construct($data);
  102. return $p;
  103. }
  104. }
  105. // vim: et sw=4 sts=4