diff --git a/NEWS b/NEWS index 2d8037bb..94605b0c 100644 --- a/NEWS +++ b/NEWS @@ -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 diff --git a/library/HTMLPurifier/DefinitionCache.php b/library/HTMLPurifier/DefinitionCache.php index 88a5abb9..93f149ef 100644 --- a/library/HTMLPurifier/DefinitionCache.php +++ b/library/HTMLPurifier/DefinitionCache.php @@ -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); diff --git a/library/HTMLPurifier/DefinitionCacheFactory.php b/library/HTMLPurifier/DefinitionCacheFactory.php index 4fa0ab5f..cdbeb1d4 100644 --- a/library/HTMLPurifier/DefinitionCacheFactory.php +++ b/library/HTMLPurifier/DefinitionCacheFactory.php @@ -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 diff --git a/tests/HTMLPurifier/DefinitionCacheFactoryTest.php b/tests/HTMLPurifier/DefinitionCacheFactoryTest.php index ba81801a..d2585be1 100644 --- a/tests/HTMLPurifier/DefinitionCacheFactoryTest.php +++ b/tests/HTMLPurifier/DefinitionCacheFactoryTest.php @@ -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); + } + }