A bunch of PHPdoc and php codesniffer corrections - no functional code changes
[htmlpurifier.git] / library / HTMLPurifier / DefinitionCacheFactory.php
blobfaa128996e6db1e2d16271978cb70da962125e37
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 /**
12 * @var HTMLPurifier_DefinitionCache_Decorator[]
14 protected $decorators = array();
16 /**
17 * Initialize default decorators
19 public function setup()
21 $this->addDecorator('Cleanup');
24 /**
25 * Retrieves an instance of global definition cache factory.
27 * @return HTMLPurifier_DefinitionCacheFactory
29 public static function instance($prototype = null)
31 static $instance;
32 if ($prototype !== null) {
33 $instance = $prototype;
34 } elseif ($instance === null || $prototype === true) {
35 $instance = new HTMLPurifier_DefinitionCacheFactory();
36 $instance->setup();
38 return $instance;
41 /**
42 * Registers a new definition cache object
43 * @param string $short Short name of cache object, for reference
44 * @param string $long Full class name of cache object, for construction
46 public function register($short, $long)
48 $this->implementations[$short] = $long;
51 /**
52 * Factory method that creates a cache object based on configuration
54 * @param string $name Name of definitions handled by cache
55 * @param HTMLPurifier_Config $config Config instance
57 public function create($type, $config)
59 $method = $config->get('Cache.DefinitionImpl');
60 if ($method === null) {
61 return new HTMLPurifier_DefinitionCache_Null($type);
63 if (!empty($this->caches[$method][$type])) {
64 return $this->caches[$method][$type];
66 if (
67 isset($this->implementations[$method]) &&
68 class_exists($class = $this->implementations[$method], false)
69 ) {
70 $cache = new $class($type);
71 } else {
72 if ($method != 'Serializer') {
73 trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING);
75 $cache = new HTMLPurifier_DefinitionCache_Serializer($type);
77 foreach ($this->decorators as $decorator) {
78 $new_cache = $decorator->decorate($cache);
79 // prevent infinite recursion in PHP 4
80 unset($cache);
81 $cache = $new_cache;
83 $this->caches[$method][$type] = $cache;
84 return $this->caches[$method][$type];
87 /**
88 * Registers a decorator to add to all new cache objects
89 * @param HTMLPurifier_DefinitionCache_Decorator|string $decorator An instance or the name of a decorator
91 public function addDecorator($decorator)
93 if (is_string($decorator)) {
94 $class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
95 $decorator = new $class;
97 $this->decorators[$decorator->name] = $decorator;
102 // vim: et sw=4 sts=4