4 * Responsible for creating definition caches.
6 class HTMLPurifier_DefinitionCacheFactory
11 protected $caches = array('Serializer' => array());
16 protected $implementations = array();
19 * @type HTMLPurifier_DefinitionCache_Decorator[]
21 protected $decorators = array();
24 * Initialize default decorators
26 public function setup()
28 $this->addDecorator('Cleanup');
32 * Retrieves an instance of global definition cache factory.
33 * @param HTMLPurifier_DefinitionCacheFactory $prototype
34 * @return HTMLPurifier_DefinitionCacheFactory
36 public static function instance($prototype = null)
39 if ($prototype !== null) {
40 $instance = $prototype;
41 } elseif ($instance === null ||
$prototype === true) {
42 $instance = new HTMLPurifier_DefinitionCacheFactory();
49 * Registers a new definition cache object
50 * @param string $short Short name of cache object, for reference
51 * @param string $long Full class name of cache object, for construction
53 public function register($short, $long)
55 $this->implementations
[$short] = $long;
59 * Factory method that creates a cache object based on configuration
60 * @param string $type Name of definitions handled by cache
61 * @param HTMLPurifier_Config $config Config instance
64 public function create($type, $config)
66 $method = $config->get('Cache.DefinitionImpl');
67 if ($method === null) {
68 return new HTMLPurifier_DefinitionCache_Null($type);
70 if (!empty($this->caches
[$method][$type])) {
71 return $this->caches
[$method][$type];
73 if (isset($this->implementations
[$method]) &&
74 class_exists($class = $this->implementations
[$method], false)) {
75 $cache = new $class($type);
77 if ($method != 'Serializer') {
78 trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING
);
80 $cache = new HTMLPurifier_DefinitionCache_Serializer($type);
82 foreach ($this->decorators
as $decorator) {
83 $new_cache = $decorator->decorate($cache);
84 // prevent infinite recursion in PHP 4
88 $this->caches
[$method][$type] = $cache;
89 return $this->caches
[$method][$type];
93 * Registers a decorator to add to all new cache objects
94 * @param HTMLPurifier_DefinitionCache_Decorator|string $decorator An instance or the name of a decorator
96 public function addDecorator($decorator)
98 if (is_string($decorator)) {
99 $class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
100 $decorator = new $class;
102 $this->decorators
[$decorator->name
] = $decorator;
106 // vim: et sw=4 sts=4