"MDL-12304, fix double text"
[moodle-linuxchix.git] / lib / htmlpurifier / HTMLPurifier / DefinitionCacheFactory.php
blobdead92a32e9217d902be75db6a9d800aa66a6196
1 <?php
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.
12 ');
14 HTMLPurifier_ConfigSchema::defineAlias(
15 'Core', 'DefinitionCache',
16 'Cache', 'DefinitionImpl'
20 /**
21 * Responsible for creating definition caches.
23 class HTMLPurifier_DefinitionCacheFactory
26 var $caches = array('Serializer' => array());
27 var $implementations = array();
28 var $decorators = array();
30 /**
31 * Initialize default decorators
33 function setup() {
34 $this->addDecorator('Cleanup');
37 /**
38 * Retrieves an instance of global definition cache factory.
39 * @static
41 function &instance($prototype = null) {
42 static $instance;
43 if ($prototype !== null) {
44 $instance = $prototype;
45 } elseif ($instance === null || $prototype === true) {
46 $instance = new HTMLPurifier_DefinitionCacheFactory();
47 $instance->setup();
49 return $instance;
52 /**
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;
61 /**
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);
70 return $null;
72 if (!empty($this->caches[$method][$type])) {
73 return $this->caches[$method][$type];
75 if (
76 isset($this->implementations[$method]) &&
77 class_exists($class = $this->implementations[$method])
78 ) {
79 $cache = new $class($type);
80 } else {
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
89 unset($cache);
90 $cache = $new_cache;
92 $this->caches[$method][$type] = $cache;
93 return $this->caches[$method][$type];
96 /**
97 * Registers a decorator to add to all new cache objects
98 * @param
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;