TokenFactory.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /**
  16. * Prototypes that will be cloned.
  17. * @private
  18. */
  19. // p stands for prototype
  20. private $p_start, $p_end, $p_empty, $p_text, $p_comment;
  21. /**
  22. * Generates blank prototypes for cloning.
  23. */
  24. public function __construct() {
  25. $this->p_start = new HTMLPurifier_Token_Start('', array());
  26. $this->p_end = new HTMLPurifier_Token_End('');
  27. $this->p_empty = new HTMLPurifier_Token_Empty('', array());
  28. $this->p_text = new HTMLPurifier_Token_Text('');
  29. $this->p_comment= new HTMLPurifier_Token_Comment('');
  30. }
  31. /**
  32. * Creates a HTMLPurifier_Token_Start.
  33. * @param $name Tag name
  34. * @param $attr Associative array of attributes
  35. * @return Generated HTMLPurifier_Token_Start
  36. */
  37. public function createStart($name, $attr = array()) {
  38. $p = clone $this->p_start;
  39. $p->__construct($name, $attr);
  40. return $p;
  41. }
  42. /**
  43. * Creates a HTMLPurifier_Token_End.
  44. * @param $name Tag name
  45. * @return Generated HTMLPurifier_Token_End
  46. */
  47. public function createEnd($name) {
  48. $p = clone $this->p_end;
  49. $p->__construct($name);
  50. return $p;
  51. }
  52. /**
  53. * Creates a HTMLPurifier_Token_Empty.
  54. * @param $name Tag name
  55. * @param $attr Associative array of attributes
  56. * @return Generated HTMLPurifier_Token_Empty
  57. */
  58. public function createEmpty($name, $attr = array()) {
  59. $p = clone $this->p_empty;
  60. $p->__construct($name, $attr);
  61. return $p;
  62. }
  63. /**
  64. * Creates a HTMLPurifier_Token_Text.
  65. * @param $data Data of text token
  66. * @return Generated HTMLPurifier_Token_Text
  67. */
  68. public function createText($data) {
  69. $p = clone $this->p_text;
  70. $p->__construct($data);
  71. return $p;
  72. }
  73. /**
  74. * Creates a HTMLPurifier_Token_Comment.
  75. * @param $data Data of comment token
  76. * @return Generated HTMLPurifier_Token_Comment
  77. */
  78. public function createComment($data) {
  79. $p = clone $this->p_comment;
  80. $p->__construct($data);
  81. return $p;
  82. }
  83. }
  84. // vim: et sw=4 sts=4