Fix two minor bugs, updating Phorum and removing unused $dir variable.
[htmlpurifier/darkodev.git] / library / HTMLPurifier / DefinitionCacheFactory.php
bloba6ead62818754b90c0c3aca45cbf9f4cae5e0f86
1 <?php
3 /**
4 * Responsible for creating definition caches.
5 */
6 class HTMLPurifier_DefinitionCacheFactory
9 protected $caches = array('Serializer' => array());
10 protected $implementations = array();
11 protected $decorators = array();
13 /**
14 * Initialize default decorators
16 public function setup() {
17 $this->addDecorator('Cleanup');
20 /**
21 * Retrieves an instance of global definition cache factory.
23 public static function instance($prototype = null) {
24 static $instance;
25 if ($prototype !== null) {
26 $instance = $prototype;
27 } elseif ($instance === null || $prototype === true) {
28 $instance = new HTMLPurifier_DefinitionCacheFactory();
29 $instance->setup();
31 return $instance;
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
39 public function register($short, $long) {
40 $this->implementations[$short] = $long;
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
48 public function create($type, $config) {
49 $method = $config->get('Cache.DefinitionImpl');
50 if ($method === null) {
51 return new HTMLPurifier_DefinitionCache_Null($type);
53 if (!empty($this->caches[$method][$type])) {
54 return $this->caches[$method][$type];
56 if (
57 isset($this->implementations[$method]) &&
58 class_exists($class = $this->implementations[$method], false)
59 ) {
60 $cache = new $class($type);
61 } else {
62 if ($method != 'Serializer') {
63 trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING);
65 $cache = new HTMLPurifier_DefinitionCache_Serializer($type);
67 foreach ($this->decorators as $decorator) {
68 $new_cache = $decorator->decorate($cache);
69 // prevent infinite recursion in PHP 4
70 unset($cache);
71 $cache = $new_cache;
73 $this->caches[$method][$type] = $cache;
74 return $this->caches[$method][$type];
77 /**
78 * Registers a decorator to add to all new cache objects
79 * @param
81 public function addDecorator($decorator) {
82 if (is_string($decorator)) {
83 $class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
84 $decorator = new $class;
86 $this->decorators[$decorator->name] = $decorator;
91 // vim: et sw=4 sts=4