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