File indexing completed on 2024-12-22 05:36:22

0001 <?php
0002 
0003 /**
0004  * Factory for token generation.
0005  *
0006  * @note Doing some benchmarking indicates that the new operator is much
0007  *       slower than the clone operator (even discounting the cost of the
0008  *       constructor).  This class is for that optimization.
0009  *       Other then that, there's not much point as we don't
0010  *       maintain parallel HTMLPurifier_Token hierarchies (the main reason why
0011  *       you'd want to use an abstract factory).
0012  * @todo Port DirectLex to use this
0013  */
0014 class HTMLPurifier_TokenFactory
0015 {
0016     // p stands for prototype
0017 
0018     /**
0019      * @type HTMLPurifier_Token_Start
0020      */
0021     private $p_start;
0022 
0023     /**
0024      * @type HTMLPurifier_Token_End
0025      */
0026     private $p_end;
0027 
0028     /**
0029      * @type HTMLPurifier_Token_Empty
0030      */
0031     private $p_empty;
0032 
0033     /**
0034      * @type HTMLPurifier_Token_Text
0035      */
0036     private $p_text;
0037 
0038     /**
0039      * @type HTMLPurifier_Token_Comment
0040      */
0041     private $p_comment;
0042 
0043     /**
0044      * Generates blank prototypes for cloning.
0045      */
0046     public function __construct()
0047     {
0048         $this->p_start = new HTMLPurifier_Token_Start('', array());
0049         $this->p_end = new HTMLPurifier_Token_End('');
0050         $this->p_empty = new HTMLPurifier_Token_Empty('', array());
0051         $this->p_text = new HTMLPurifier_Token_Text('');
0052         $this->p_comment = new HTMLPurifier_Token_Comment('');
0053     }
0054 
0055     /**
0056      * Creates a HTMLPurifier_Token_Start.
0057      * @param string $name Tag name
0058      * @param array $attr Associative array of attributes
0059      * @return HTMLPurifier_Token_Start Generated HTMLPurifier_Token_Start
0060      */
0061     public function createStart($name, $attr = array())
0062     {
0063         $p = clone $this->p_start;
0064         $p->__construct($name, $attr);
0065         return $p;
0066     }
0067 
0068     /**
0069      * Creates a HTMLPurifier_Token_End.
0070      * @param string $name Tag name
0071      * @return HTMLPurifier_Token_End Generated HTMLPurifier_Token_End
0072      */
0073     public function createEnd($name)
0074     {
0075         $p = clone $this->p_end;
0076         $p->__construct($name);
0077         return $p;
0078     }
0079 
0080     /**
0081      * Creates a HTMLPurifier_Token_Empty.
0082      * @param string $name Tag name
0083      * @param array $attr Associative array of attributes
0084      * @return HTMLPurifier_Token_Empty Generated HTMLPurifier_Token_Empty
0085      */
0086     public function createEmpty($name, $attr = array())
0087     {
0088         $p = clone $this->p_empty;
0089         $p->__construct($name, $attr);
0090         return $p;
0091     }
0092 
0093     /**
0094      * Creates a HTMLPurifier_Token_Text.
0095      * @param string $data Data of text token
0096      * @return HTMLPurifier_Token_Text Generated HTMLPurifier_Token_Text
0097      */
0098     public function createText($data)
0099     {
0100         $p = clone $this->p_text;
0101         $p->__construct($data);
0102         return $p;
0103     }
0104 
0105     /**
0106      * Creates a HTMLPurifier_Token_Comment.
0107      * @param string $data Data of comment token
0108      * @return HTMLPurifier_Token_Comment Generated HTMLPurifier_Token_Comment
0109      */
0110     public function createComment($data)
0111     {
0112         $p = clone $this->p_comment;
0113         $p->__construct($data);
0114         return $p;
0115     }
0116 }
0117 
0118 // vim: et sw=4 sts=4