2010-09-09 05:14:17 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Responsible for creating definition caches.
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_DefinitionCacheFactory
|
|
|
|
{
|
2016-02-09 11:06:17 +01:00
|
|
|
/**
|
|
|
|
* @type array
|
|
|
|
*/
|
2010-09-09 05:14:17 +02:00
|
|
|
protected $caches = array('Serializer' => array());
|
2016-02-09 11:06:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type array
|
|
|
|
*/
|
2010-09-09 05:14:17 +02:00
|
|
|
protected $implementations = array();
|
2016-02-09 11:06:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @type HTMLPurifier_DefinitionCache_Decorator[]
|
|
|
|
*/
|
2010-09-09 05:14:17 +02:00
|
|
|
protected $decorators = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize default decorators
|
|
|
|
*/
|
2016-02-09 11:06:17 +01:00
|
|
|
public function setup()
|
|
|
|
{
|
2010-09-09 05:14:17 +02:00
|
|
|
$this->addDecorator('Cleanup');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves an instance of global definition cache factory.
|
2016-02-09 11:06:17 +01:00
|
|
|
* @param HTMLPurifier_DefinitionCacheFactory $prototype
|
|
|
|
* @return HTMLPurifier_DefinitionCacheFactory
|
2010-09-09 05:14:17 +02:00
|
|
|
*/
|
2016-02-09 11:06:17 +01:00
|
|
|
public static function instance($prototype = null)
|
|
|
|
{
|
2010-09-09 05:14:17 +02:00
|
|
|
static $instance;
|
|
|
|
if ($prototype !== null) {
|
|
|
|
$instance = $prototype;
|
|
|
|
} elseif ($instance === null || $prototype === true) {
|
|
|
|
$instance = new HTMLPurifier_DefinitionCacheFactory();
|
|
|
|
$instance->setup();
|
|
|
|
}
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a new definition cache object
|
2016-02-09 11:06:17 +01:00
|
|
|
* @param string $short Short name of cache object, for reference
|
|
|
|
* @param string $long Full class name of cache object, for construction
|
2010-09-09 05:14:17 +02:00
|
|
|
*/
|
2016-02-09 11:06:17 +01:00
|
|
|
public function register($short, $long)
|
|
|
|
{
|
2010-09-09 05:14:17 +02:00
|
|
|
$this->implementations[$short] = $long;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method that creates a cache object based on configuration
|
2016-02-09 11:06:17 +01:00
|
|
|
* @param string $type Name of definitions handled by cache
|
|
|
|
* @param HTMLPurifier_Config $config Config instance
|
|
|
|
* @return mixed
|
2010-09-09 05:14:17 +02:00
|
|
|
*/
|
2016-02-09 11:06:17 +01:00
|
|
|
public function create($type, $config)
|
|
|
|
{
|
2010-09-09 05:14:17 +02:00
|
|
|
$method = $config->get('Cache.DefinitionImpl');
|
|
|
|
if ($method === null) {
|
|
|
|
return new HTMLPurifier_DefinitionCache_Null($type);
|
|
|
|
}
|
|
|
|
if (!empty($this->caches[$method][$type])) {
|
|
|
|
return $this->caches[$method][$type];
|
|
|
|
}
|
2016-02-09 11:06:17 +01:00
|
|
|
if (isset($this->implementations[$method]) &&
|
|
|
|
class_exists($class = $this->implementations[$method], false)) {
|
2010-09-09 05:14:17 +02:00
|
|
|
$cache = new $class($type);
|
|
|
|
} else {
|
|
|
|
if ($method != 'Serializer') {
|
|
|
|
trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING);
|
|
|
|
}
|
|
|
|
$cache = new HTMLPurifier_DefinitionCache_Serializer($type);
|
|
|
|
}
|
|
|
|
foreach ($this->decorators as $decorator) {
|
|
|
|
$new_cache = $decorator->decorate($cache);
|
|
|
|
// prevent infinite recursion in PHP 4
|
|
|
|
unset($cache);
|
|
|
|
$cache = $new_cache;
|
|
|
|
}
|
|
|
|
$this->caches[$method][$type] = $cache;
|
|
|
|
return $this->caches[$method][$type];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a decorator to add to all new cache objects
|
2016-02-09 11:06:17 +01:00
|
|
|
* @param HTMLPurifier_DefinitionCache_Decorator|string $decorator An instance or the name of a decorator
|
2010-09-09 05:14:17 +02:00
|
|
|
*/
|
2016-02-09 11:06:17 +01:00
|
|
|
public function addDecorator($decorator)
|
|
|
|
{
|
2010-09-09 05:14:17 +02:00
|
|
|
if (is_string($decorator)) {
|
|
|
|
$class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
|
|
|
|
$decorator = new $class;
|
|
|
|
}
|
|
|
|
$this->decorators[$decorator->name] = $decorator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|