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

0001 <?php
0002 
0003 /**
0004  * Responsible for creating definition caches.
0005  */
0006 class HTMLPurifier_DefinitionCacheFactory
0007 {
0008     /**
0009      * @type array
0010      */
0011     protected $caches = array('Serializer' => array());
0012 
0013     /**
0014      * @type array
0015      */
0016     protected $implementations = array();
0017 
0018     /**
0019      * @type HTMLPurifier_DefinitionCache_Decorator[]
0020      */
0021     protected $decorators = array();
0022 
0023     /**
0024      * Initialize default decorators
0025      */
0026     public function setup()
0027     {
0028         $this->addDecorator('Cleanup');
0029     }
0030 
0031     /**
0032      * Retrieves an instance of global definition cache factory.
0033      * @param HTMLPurifier_DefinitionCacheFactory $prototype
0034      * @return HTMLPurifier_DefinitionCacheFactory
0035      */
0036     public static function instance($prototype = null)
0037     {
0038         static $instance;
0039         if ($prototype !== null) {
0040             $instance = $prototype;
0041         } elseif ($instance === null || $prototype === true) {
0042             $instance = new HTMLPurifier_DefinitionCacheFactory();
0043             $instance->setup();
0044         }
0045         return $instance;
0046     }
0047 
0048     /**
0049      * Registers a new definition cache object
0050      * @param string $short Short name of cache object, for reference
0051      * @param string $long Full class name of cache object, for construction
0052      */
0053     public function register($short, $long)
0054     {
0055         $this->implementations[$short] = $long;
0056     }
0057 
0058     /**
0059      * Factory method that creates a cache object based on configuration
0060      * @param string $type Name of definitions handled by cache
0061      * @param HTMLPurifier_Config $config Config instance
0062      * @return mixed
0063      */
0064     public function create($type, $config)
0065     {
0066         $method = $config->get('Cache.DefinitionImpl');
0067         if ($method === null) {
0068             return new HTMLPurifier_DefinitionCache_Null($type);
0069         }
0070         if (!empty($this->caches[$method][$type])) {
0071             return $this->caches[$method][$type];
0072         }
0073         if (isset($this->implementations[$method]) &&
0074             class_exists($class = $this->implementations[$method], false)) {
0075             $cache = new $class($type);
0076         } else {
0077             if ($method != 'Serializer') {
0078                 trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING);
0079             }
0080             $cache = new HTMLPurifier_DefinitionCache_Serializer($type);
0081         }
0082         foreach ($this->decorators as $decorator) {
0083             $new_cache = $decorator->decorate($cache);
0084             // prevent infinite recursion in PHP 4
0085             unset($cache);
0086             $cache = $new_cache;
0087         }
0088         $this->caches[$method][$type] = $cache;
0089         return $this->caches[$method][$type];
0090     }
0091 
0092     /**
0093      * Registers a decorator to add to all new cache objects
0094      * @param HTMLPurifier_DefinitionCache_Decorator|string $decorator An instance or the name of a decorator
0095      */
0096     public function addDecorator($decorator)
0097     {
0098         if (is_string($decorator)) {
0099             $class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
0100             $decorator = new $class;
0101         }
0102         $this->decorators[$decorator->name] = $decorator;
0103     }
0104 }
0105 
0106 // vim: et sw=4 sts=4