HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/DefinitionCacheFactory.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class HTMLPurifier_DefinitionCacheFactory
00007 {
00008 
00009     protected $caches = array('Serializer' => array());
00010     protected $implementations = array();
00011     protected $decorators = array();
00012 
00016     public function setup() {
00017         $this->addDecorator('Cleanup');
00018     }
00019 
00023     public static function instance($prototype = null) {
00024         static $instance;
00025         if ($prototype !== null) {
00026             $instance = $prototype;
00027         } elseif ($instance === null || $prototype === true) {
00028             $instance = new HTMLPurifier_DefinitionCacheFactory();
00029             $instance->setup();
00030         }
00031         return $instance;
00032     }
00033 
00039     public function register($short, $long) {
00040         $this->implementations[$short] = $long;
00041     }
00042 
00048     public function create($type, $config) {
00049         $method = $config->get('Cache.DefinitionImpl');
00050         if ($method === null) {
00051             return new HTMLPurifier_DefinitionCache_Null($type);
00052         }
00053         if (!empty($this->caches[$method][$type])) {
00054             return $this->caches[$method][$type];
00055         }
00056         if (
00057           isset($this->implementations[$method]) &&
00058           class_exists($class = $this->implementations[$method], false)
00059         ) {
00060             $cache = new $class($type);
00061         } else {
00062             if ($method != 'Serializer') {
00063                 trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING);
00064             }
00065             $cache = new HTMLPurifier_DefinitionCache_Serializer($type);
00066         }
00067         foreach ($this->decorators as $decorator) {
00068             $new_cache = $decorator->decorate($cache);
00069             // prevent infinite recursion in PHP 4
00070             unset($cache);
00071             $cache = $new_cache;
00072         }
00073         $this->caches[$method][$type] = $cache;
00074         return $this->caches[$method][$type];
00075     }
00076 
00081     public function addDecorator($decorator) {
00082         if (is_string($decorator)) {
00083             $class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
00084             $decorator = new $class;
00085         }
00086         $this->decorators[$decorator->name] = $decorator;
00087     }
00088 
00089 }
00090 
00091 // vim: et sw=4 sts=4