1 : <?php
2 :
3 : /**
4 : * Responsible for creating definition caches.
5 : */
6 : class HTMLPurifier_DefinitionCacheFactory
7 1 : {
8 :
9 : protected $caches = array('Serializer' => array());
10 : protected $implementations = array();
11 : protected $decorators = array();
12 :
13 : /**
14 : * Initialize default decorators
15 : */
16 : public function setup() {
17 1 : $this->addDecorator('Cleanup');
18 1 : }
19 :
20 : /**
21 : * Retrieves an instance of global definition cache factory.
22 : */
23 : public static function instance($prototype = null) {
24 2 : static $instance;
25 2 : if ($prototype !== null) {
26 0 : $instance = $prototype;
27 2 : } elseif ($instance === null || $prototype === true) {
28 1 : $instance = new HTMLPurifier_DefinitionCacheFactory();
29 1 : $instance->setup();
30 1 : }
31 2 : return $instance;
32 : }
33 :
34 : /**
35 : * Registers a new definition cache object
36 : * @param $short Short name of cache object, for reference
37 : * @param $long Full class name of cache object, for construction
38 : */
39 : public function register($short, $long) {
40 0 : $this->implementations[$short] = $long;
41 0 : }
42 :
43 : /**
44 : * Factory method that creates a cache object based on configuration
45 : * @param $name Name of definitions handled by cache
46 : * @param $config Instance of HTMLPurifier_Config
47 : */
48 : public function create($type, $config) {
49 2 : $method = $config->get('Cache', 'DefinitionImpl');
50 2 : if ($method === null) {
51 0 : return new HTMLPurifier_DefinitionCache_Null($type);
52 : }
53 2 : if (!empty($this->caches[$method][$type])) {
54 2 : return $this->caches[$method][$type];
55 : }
56 : if (
57 1 : isset($this->implementations[$method]) &&
58 0 : class_exists($class = $this->implementations[$method], false)
59 1 : ) {
60 0 : $cache = new $class($type);
61 0 : } else {
62 1 : if ($method != 'Serializer') {
63 0 : trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING);
64 0 : }
65 1 : $cache = new HTMLPurifier_DefinitionCache_Serializer($type);
66 : }
67 1 : foreach ($this->decorators as $decorator) {
68 1 : $new_cache = $decorator->decorate($cache);
69 : // prevent infinite recursion in PHP 4
70 1 : unset($cache);
71 1 : $cache = $new_cache;
72 1 : }
73 1 : $this->caches[$method][$type] = $cache;
74 1 : return $this->caches[$method][$type];
75 : }
76 :
77 : /**
78 : * Registers a decorator to add to all new cache objects
79 : * @param
80 : */
81 : public function addDecorator($decorator) {
82 1 : if (is_string($decorator)) {
83 1 : $class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
84 1 : $decorator = new $class;
85 1 : }
86 1 : $this->decorators[$decorator->name] = $decorator;
87 1 : }
88 :
89 : }
90 :
|