0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 16:31:53 +00:00

[3.0.0] [BACKPORT]

- Add register() for DefinitionCacheFactory so custom implementations can be specified
- Add docs for flush() regarding when this functionality is impossible to do
- Refactor unit tests in DefinitionCacheFactoryTest.php

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1464 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-12-09 03:14:34 +00:00
parent b5546ff6f0
commit 62f3fd894d
4 changed files with 53 additions and 28 deletions

1
NEWS
View File

@ -17,6 +17,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
+ Constructor methods renamed to __construct()
+ PHP4 reference/foreach cruft removed (in progress)
! CSS properties are no case-insensitive
! DefinitionCacheFactory now can register new implementations
. Unit tests for Injector improved
2.1.3, released 2007-11-05

View File

@ -104,6 +104,9 @@ abstract class HTMLPurifier_DefinitionCache
/**
* Clears all expired (older version or revision) objects from cache
* @note Be carefuly implementing this method as flush. Flush must
* not interfere with other Definition types, and cleanup()
* should not be repeatedly called by userland code.
*/
abstract public function cleanup($config);

View File

@ -10,10 +10,6 @@ to disable caching (not recommended, as you will see a definite
performance degradation). This directive has been available since 2.0.0.
');
HTMLPurifier_ConfigSchema::defineAllowedValues(
'Cache', 'DefinitionImpl', array('Serializer')
);
HTMLPurifier_ConfigSchema::defineAlias(
'Core', 'DefinitionCache',
'Cache', 'DefinitionImpl'
@ -27,6 +23,7 @@ class HTMLPurifier_DefinitionCacheFactory
{
protected $caches = array('Serializer' => array());
protected $implementations = array();
protected $decorators = array();
/**
@ -50,14 +47,21 @@ class HTMLPurifier_DefinitionCacheFactory
return $instance;
}
/**
* Registers a new definition cache object
* @param $short Short name of cache object, for reference
* @param $long Full class name of cache object, for construction
*/
public function register($short, $long) {
$this->implementations[$short] = $long;
}
/**
* Factory method that creates a cache object based on configuration
* @param $name Name of definitions handled by cache
* @param $config Instance of HTMLPurifier_Config
*/
public function &create($type, $config) {
// only one implementation as for right now, $config will
// be used to determine implementation
$method = $config->get('Cache', 'DefinitionImpl');
if ($method === null) {
$null = new HTMLPurifier_DefinitionCache_Null($type);
@ -66,7 +70,17 @@ class HTMLPurifier_DefinitionCacheFactory
if (!empty($this->caches[$method][$type])) {
return $this->caches[$method][$type];
}
$cache = new HTMLPurifier_DefinitionCache_Serializer($type);
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);
}
foreach ($this->decorators as $decorator) {
$new_cache = $decorator->decorate($cache);
// prevent infinite recursion in PHP 4

View File

@ -5,13 +5,14 @@ require_once 'HTMLPurifier/DefinitionCacheFactory.php';
class HTMLPurifier_DefinitionCacheFactoryTest extends HTMLPurifier_Harness
{
protected $newFactory;
protected $factory;
protected $oldFactory;
function setup() {
$new = new HTMLPurifier_DefinitionCacheFactory();
parent::setup();
$this->factory = new HTMLPurifier_DefinitionCacheFactory();
$this->oldFactory = HTMLPurifier_DefinitionCacheFactory::instance();
HTMLPurifier_DefinitionCacheFactory::instance($new);
HTMLPurifier_DefinitionCacheFactory::instance($this->factory);
}
function teardown() {
@ -19,46 +20,52 @@ class HTMLPurifier_DefinitionCacheFactoryTest extends HTMLPurifier_Harness
}
function test_create() {
$config = HTMLPurifier_Config::createDefault();
$factory = HTMLPurifier_DefinitionCacheFactory::instance();
$cache = $factory->create('Test', $config);
$cache = $this->factory->create('Test', $this->config);
$this->assertEqual($cache, new HTMLPurifier_DefinitionCache_Serializer('Test'));
}
function test_create_withDecorator() {
$config = HTMLPurifier_Config::createDefault();
$factory =& HTMLPurifier_DefinitionCacheFactory::instance();
$factory->addDecorator('Memory');
$cache =& $factory->create('Test', $config);
$this->factory->addDecorator('Memory');
$cache = $this->factory->create('Test', $this->config);
$cache_real = new HTMLPurifier_DefinitionCache_Decorator_Memory();
$cache_real = $cache_real->decorate(new HTMLPurifier_DefinitionCache_Serializer('Test'));
$this->assertEqual($cache, $cache_real);
}
function test_create_withDecoratorObject() {
$config = HTMLPurifier_Config::createDefault();
$factory =& HTMLPurifier_DefinitionCacheFactory::instance();
$factory->addDecorator(new HTMLPurifier_DefinitionCache_Decorator_Memory());
$cache =& $factory->create('Test', $config);
$this->factory->addDecorator(new HTMLPurifier_DefinitionCache_Decorator_Memory());
$cache = $this->factory->create('Test', $this->config);
$cache_real = new HTMLPurifier_DefinitionCache_Decorator_Memory();
$cache_real = $cache_real->decorate(new HTMLPurifier_DefinitionCache_Serializer('Test'));
$this->assertEqual($cache, $cache_real);
}
function test_create_recycling() {
$config = HTMLPurifier_Config::createDefault();
$factory =& HTMLPurifier_DefinitionCacheFactory::instance();
$cache =& $factory->create('Test', $config);
$cache2 =& $factory->create('Test', $config);
$cache = $this->factory->create('Test', $this->config);
$cache2 = $this->factory->create('Test', $this->config);
$this->assertReference($cache, $cache2);
}
function test_create_invalid() {
$this->config->set('Core', 'DefinitionCache', 'Invalid');
$this->expectError('Unrecognized DefinitionCache Invalid, using Serializer instead');
$cache = $this->factory->create('Test', $this->config);
$this->assertIsA($cache, 'HTMLPurifier_DefinitionCache_Serializer');
}
function test_null() {
$config = HTMLPurifier_Config::create(array('Core.DefinitionCache' => null));
$factory =& HTMLPurifier_DefinitionCacheFactory::instance();
$cache =& $factory->create('Test', $config);
$this->config->set('Core', 'DefinitionCache', null);
$cache = $this->factory->create('Test', $this->config);
$this->assertEqual($cache, new HTMLPurifier_DefinitionCache_Null('Test'));
}
function test_register() {
generate_mock_once('HTMLPurifier_DefinitionCache');
$this->config->set('Core', 'DefinitionCache', 'TestCache');
$this->factory->register('TestCache', $class = 'HTMLPurifier_DefinitionCacheMock');
$cache = $this->factory->create('Test', $this->config);
$this->assertIsA($cache, $class);
}
}