2007-05-29 20:21:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Responsible for creating definition caches.
|
|
|
|
*/
|
|
|
|
class HTMLPurifier_DefinitionCacheFactory
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $caches = array('Serializer' => array());
|
2007-12-09 03:14:34 +00:00
|
|
|
protected $implementations = array();
|
2013-07-16 12:20:01 +00:00
|
|
|
/**
|
|
|
|
* @var HTMLPurifier_DefinitionCache_Decorator[]
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $decorators = array();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 20:49:33 +00:00
|
|
|
/**
|
|
|
|
* Initialize default decorators
|
|
|
|
*/
|
2013-07-16 12:20:01 +00:00
|
|
|
public function setup()
|
|
|
|
{
|
2007-05-29 20:49:33 +00:00
|
|
|
$this->addDecorator('Cleanup');
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 20:21:33 +00:00
|
|
|
/**
|
|
|
|
* Retrieves an instance of global definition cache factory.
|
2013-07-16 12:20:01 +00:00
|
|
|
*
|
|
|
|
* @return HTMLPurifier_DefinitionCacheFactory
|
2007-05-29 20:21:33 +00:00
|
|
|
*/
|
2013-07-16 12:20:01 +00:00
|
|
|
public static function instance($prototype = null)
|
|
|
|
{
|
2007-05-29 20:21:33 +00:00
|
|
|
static $instance;
|
|
|
|
if ($prototype !== null) {
|
|
|
|
$instance = $prototype;
|
|
|
|
} elseif ($instance === null || $prototype === true) {
|
|
|
|
$instance = new HTMLPurifier_DefinitionCacheFactory();
|
2007-05-29 20:49:33 +00:00
|
|
|
$instance->setup();
|
2007-05-29 20:21:33 +00:00
|
|
|
}
|
|
|
|
return $instance;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-12-09 03:14:34 +00:00
|
|
|
/**
|
|
|
|
* Registers a new definition cache object
|
2013-07-16 12:20:01 +00:00
|
|
|
* @param string $short Short name of cache object, for reference
|
|
|
|
* @param string $long Full class name of cache object, for construction
|
2007-12-09 03:14:34 +00:00
|
|
|
*/
|
2013-07-16 12:20:01 +00:00
|
|
|
public function register($short, $long)
|
|
|
|
{
|
2007-12-09 03:14:34 +00:00
|
|
|
$this->implementations[$short] = $long;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 20:21:33 +00:00
|
|
|
/**
|
|
|
|
* Factory method that creates a cache object based on configuration
|
2013-07-16 12:20:01 +00:00
|
|
|
*
|
|
|
|
* @param string $name Name of definitions handled by cache
|
|
|
|
* @param HTMLPurifier_Config $config Config instance
|
2007-05-29 20:21:33 +00:00
|
|
|
*/
|
2013-07-16 12:20:01 +00:00
|
|
|
public function create($type, $config)
|
|
|
|
{
|
2009-02-20 00:17:49 +00:00
|
|
|
$method = $config->get('Cache.DefinitionImpl');
|
2007-05-29 20:21:33 +00:00
|
|
|
if ($method === null) {
|
2008-04-26 19:28:14 +00:00
|
|
|
return new HTMLPurifier_DefinitionCache_Null($type);
|
2007-05-29 20:21:33 +00:00
|
|
|
}
|
|
|
|
if (!empty($this->caches[$method][$type])) {
|
|
|
|
return $this->caches[$method][$type];
|
|
|
|
}
|
2007-12-09 03:14:34 +00:00
|
|
|
if (
|
|
|
|
isset($this->implementations[$method]) &&
|
|
|
|
class_exists($class = $this->implementations[$method], false)
|
|
|
|
) {
|
|
|
|
$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);
|
|
|
|
}
|
2007-05-29 20:21:33 +00:00
|
|
|
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];
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 20:21:33 +00:00
|
|
|
/**
|
|
|
|
* Registers a decorator to add to all new cache objects
|
2013-07-16 12:20:01 +00:00
|
|
|
* @param HTMLPurifier_DefinitionCache_Decorator|string $decorator An instance or the name of a decorator
|
2007-05-29 20:21:33 +00:00
|
|
|
*/
|
2013-07-16 12:20:01 +00:00
|
|
|
public function addDecorator($decorator)
|
|
|
|
{
|
2007-05-29 20:21:33 +00:00
|
|
|
if (is_string($decorator)) {
|
|
|
|
$class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
|
|
|
|
$decorator = new $class;
|
|
|
|
}
|
2007-05-29 20:49:33 +00:00
|
|
|
$this->decorators[$decorator->name] = $decorator;
|
2007-05-29 20:21:33 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-05-29 20:21:33 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|