1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-08-03 04:37:39 +02:00

[2.1.4] [MFH] register() for DefinitionCacheFactory from r1464

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/branches/php4@1711 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2008-05-15 04:21:23 +00:00
parent 21c6803401
commit 97d3c8509c
4 changed files with 54 additions and 28 deletions

1
NEWS
View File

@@ -10,6 +10,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
========================== ==========================
2.1.4, unknown release date 2.1.4, unknown release date
! DefinitionCacheFactory now can register new implementations
- Colors missing # but in hex form will be corrected - Colors missing # but in hex form will be corrected
- CSS Number algorithm improved - CSS Number algorithm improved

View File

@@ -120,6 +120,9 @@ class HTMLPurifier_DefinitionCache
/** /**
* Clears all expired (older version or revision) objects from cache * 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.
*/ */
function cleanup($config) { function cleanup($config) {
trigger_error('Cannot call abstract method', E_USER_ERROR); trigger_error('Cannot call abstract method', E_USER_ERROR);

View File

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

View File

@@ -5,13 +5,14 @@ require_once 'HTMLPurifier/DefinitionCacheFactory.php';
class HTMLPurifier_DefinitionCacheFactoryTest extends HTMLPurifier_Harness class HTMLPurifier_DefinitionCacheFactoryTest extends HTMLPurifier_Harness
{ {
var $newFactory; var $factory;
var $oldFactory; var $oldFactory;
function setup() { function setup() {
$new = new HTMLPurifier_DefinitionCacheFactory(); parent::setup();
$this->factory = new HTMLPurifier_DefinitionCacheFactory();
$this->oldFactory = HTMLPurifier_DefinitionCacheFactory::instance(); $this->oldFactory = HTMLPurifier_DefinitionCacheFactory::instance();
HTMLPurifier_DefinitionCacheFactory::instance($new); HTMLPurifier_DefinitionCacheFactory::instance($this->factory);
} }
function teardown() { function teardown() {
@@ -19,46 +20,52 @@ class HTMLPurifier_DefinitionCacheFactoryTest extends HTMLPurifier_Harness
} }
function test_create() { function test_create() {
$config = HTMLPurifier_Config::createDefault(); $cache = $this->factory->create('Test', $this->config);
$factory = HTMLPurifier_DefinitionCacheFactory::instance();
$cache = $factory->create('Test', $config);
$this->assertEqual($cache, new HTMLPurifier_DefinitionCache_Serializer('Test')); $this->assertEqual($cache, new HTMLPurifier_DefinitionCache_Serializer('Test'));
} }
function test_create_withDecorator() { function test_create_withDecorator() {
$config = HTMLPurifier_Config::createDefault(); $this->factory->addDecorator('Memory');
$factory =& HTMLPurifier_DefinitionCacheFactory::instance(); $cache = $this->factory->create('Test', $this->config);
$factory->addDecorator('Memory');
$cache =& $factory->create('Test', $config);
$cache_real = new HTMLPurifier_DefinitionCache_Decorator_Memory(); $cache_real = new HTMLPurifier_DefinitionCache_Decorator_Memory();
$cache_real = $cache_real->decorate(new HTMLPurifier_DefinitionCache_Serializer('Test')); $cache_real = $cache_real->decorate(new HTMLPurifier_DefinitionCache_Serializer('Test'));
$this->assertEqual($cache, $cache_real); $this->assertEqual($cache, $cache_real);
} }
function test_create_withDecoratorObject() { function test_create_withDecoratorObject() {
$config = HTMLPurifier_Config::createDefault(); $this->factory->addDecorator(new HTMLPurifier_DefinitionCache_Decorator_Memory());
$factory =& HTMLPurifier_DefinitionCacheFactory::instance(); $cache = $this->factory->create('Test', $this->config);
$factory->addDecorator(new HTMLPurifier_DefinitionCache_Decorator_Memory());
$cache =& $factory->create('Test', $config);
$cache_real = new HTMLPurifier_DefinitionCache_Decorator_Memory(); $cache_real = new HTMLPurifier_DefinitionCache_Decorator_Memory();
$cache_real = $cache_real->decorate(new HTMLPurifier_DefinitionCache_Serializer('Test')); $cache_real = $cache_real->decorate(new HTMLPurifier_DefinitionCache_Serializer('Test'));
$this->assertEqual($cache, $cache_real); $this->assertEqual($cache, $cache_real);
} }
function test_create_recycling() { function test_create_recycling() {
$config = HTMLPurifier_Config::createDefault(); $cache =& $this->factory->create('Test', $this->config);
$factory =& HTMLPurifier_DefinitionCacheFactory::instance(); $cache2 =& $this->factory->create('Test', $this->config);
$cache =& $factory->create('Test', $config);
$cache2 =& $factory->create('Test', $config);
$this->assertReference($cache, $cache2); $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() { function test_null() {
$config = HTMLPurifier_Config::create(array('Core.DefinitionCache' => null)); $this->config->set('Core', 'DefinitionCache', null);
$factory =& HTMLPurifier_DefinitionCacheFactory::instance(); $cache = $this->factory->create('Test', $this->config);
$cache =& $factory->create('Test', $config);
$this->assertEqual($cache, new HTMLPurifier_DefinitionCache_Null('Test')); $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);
}
} }