3 require_once 'HTMLPurifier/DefinitionCache.php';
4 require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
6 HTMLPurifier_ConfigSchema
::define(
7 'Cache', 'DefinitionImpl', 'Serializer', 'string/null', '
8 This directive defines which method to use when caching definitions,
9 the complex data-type that makes HTML Purifier tick. Set to null
10 to disable caching (not recommended, as you will see a definite
11 performance degradation). This directive has been available since 2.0.0.
14 HTMLPurifier_ConfigSchema
::defineAlias(
15 'Core', 'DefinitionCache',
16 'Cache', 'DefinitionImpl'
21 * Responsible for creating definition caches.
23 class HTMLPurifier_DefinitionCacheFactory
26 var $caches = array('Serializer' => array());
27 var $implementations = array();
28 var $decorators = array();
31 * Initialize default decorators
34 $this->addDecorator('Cleanup');
38 * Retrieves an instance of global definition cache factory.
41 function &instance($prototype = null) {
43 if ($prototype !== null) {
44 $instance = $prototype;
45 } elseif ($instance === null ||
$prototype === true) {
46 $instance = new HTMLPurifier_DefinitionCacheFactory();
53 * Registers a new definition cache object
54 * @param $short Short name of cache object, for reference
55 * @param $long Full class name of cache object, for construction
57 function register($short, $long) {
58 $this->implementations
[$short] = $long;
62 * Factory method that creates a cache object based on configuration
63 * @param $name Name of definitions handled by cache
64 * @param $config Instance of HTMLPurifier_Config
66 function &create($type, $config) {
67 $method = $config->get('Cache', 'DefinitionImpl');
68 if ($method === null) {
69 $null = new HTMLPurifier_DefinitionCache_Null($type);
72 if (!empty($this->caches
[$method][$type])) {
73 return $this->caches
[$method][$type];
76 isset($this->implementations
[$method]) &&
77 class_exists($class = $this->implementations
[$method])
79 $cache = new $class($type);
81 if ($method != 'Serializer') {
82 trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING
);
84 $cache = new HTMLPurifier_DefinitionCache_Serializer($type);
86 foreach ($this->decorators
as $decorator) {
87 $new_cache = $decorator->decorate($cache);
88 // prevent infinite recursion in PHP 4
92 $this->caches
[$method][$type] = $cache;
93 return $this->caches
[$method][$type];
97 * Registers a decorator to add to all new cache objects
100 function addDecorator($decorator) {
101 if (is_string($decorator)) {
102 $class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
103 $decorator = new $class;
105 $this->decorators
[$decorator->name
] = $decorator;