1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-07-30 19:00:10 +02:00

[3.1.1] Memory optimizations for ConfigSchema. Changes include:

- Elimination of ConfigDef and subclasses in favor of stdclass. Most property names stay the same
- Added benchmark script for ConfigSchema
- Types are internally handled as magic integers. Use HTMLPurifier_VarParser->getTypeName to convert to human readable form. HTMLPurifier_VarParser still accepts strings.
- Parser in config schema only used for legacy interface


git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1764 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2008-05-23 16:43:24 +00:00
parent 9db891c3aa
commit 8ab30e24b7
18 changed files with 171 additions and 218 deletions

View File

@@ -7,21 +7,34 @@
class HTMLPurifier_VarParser
{
const STRING = 0;
const ISTRING = 1;
const TEXT = 2;
const ITEXT = 3;
const INT = 4;
const FLOAT = 5;
const BOOL = 6;
const LOOKUP = 7;
const ALIST = 8;
const HASH = 9;
const MIXED = 10;
/**
* Lookup table of allowed types.
* Lookup table of allowed types. Mainly for backwards compatibility, but
* also convenient for transforming string type names to the integer constants.
*/
static public $types = array(
'string' => true,
'istring' => true,
'text' => true,
'itext' => true,
'int' => true,
'float' => true,
'bool' => true,
'lookup' => true,
'list' => true,
'hash' => true,
'mixed' => true
'string' => self::STRING,
'istring' => self::ISTRING,
'text' => self::TEXT,
'itext' => self::ITEXT,
'int' => self::INT,
'float' => self::FLOAT,
'bool' => self::BOOL,
'lookup' => self::LOOKUP,
'list' => self::ALIST,
'hash' => self::HASH,
'mixed' => self::MIXED
);
/**
@@ -29,10 +42,10 @@ class HTMLPurifier_VarParser
* allowed value lists.
*/
static public $stringTypes = array(
'string' => true,
'istring' => true,
'text' => true,
'itext' => true,
self::STRING => true,
self::ISTRING => true,
self::TEXT => true,
self::ITEXT => true,
);
/**
@@ -46,42 +59,46 @@ class HTMLPurifier_VarParser
* @return Validated and type-coerced variable
*/
final public function parse($var, $type, $allow_null = false) {
if (!isset(HTMLPurifier_VarParser::$types[$type])) {
throw new HTMLPurifier_VarParserException("Invalid type '$type'");
if (is_string($type)) {
if (!isset(HTMLPurifier_VarParser::$types[$type])) {
throw new HTMLPurifier_VarParserException("Invalid type '$type'");
} else {
$type = HTMLPurifier_VarParser::$types[$type];
}
}
$var = $this->parseImplementation($var, $type, $allow_null);
if ($allow_null && $var === null) return null;
// These are basic checks, to make sure nothing horribly wrong
// happened in our implementations.
switch ($type) {
case 'string':
case 'istring':
case 'text':
case 'itext':
case (self::STRING):
case (self::ISTRING):
case (self::TEXT):
case (self::ITEXT):
if (!is_string($var)) break;
if ($type[0] == 'i') $var = strtolower($var);
if ($type == self::ISTRING || $type == self::ITEXT) $var = strtolower($var);
return $var;
case 'int':
case (self::INT):
if (!is_int($var)) break;
return $var;
case 'float':
case (self::FLOAT):
if (!is_float($var)) break;
return $var;
case 'bool':
case (self::BOOL):
if (!is_bool($var)) break;
return $var;
case 'lookup':
case 'list':
case 'hash':
case (self::LOOKUP):
case (self::ALIST):
case (self::HASH):
if (!is_array($var)) break;
if ($type === 'lookup') {
if ($type === self::LOOKUP) {
foreach ($var as $k) if ($k !== true) $this->error('Lookup table contains value other than true');
} elseif ($type === 'list') {
} elseif ($type === self::ALIST) {
$keys = array_keys($var);
if (array_keys($keys) !== $keys) $this->error('Indices for list are not uniform');
}
return $var;
case 'mixed':
case (self::MIXED):
return $var;
default:
$this->errorInconsistent(get_class($this), $type);
@@ -111,7 +128,7 @@ class HTMLPurifier_VarParser
* updating subclasses.
*/
protected function errorInconsistent($class, $type) {
throw new HTMLPurifier_Exception("Inconsistency in $class: $type not implemented");
throw new HTMLPurifier_Exception("Inconsistency in $class: ".HTMLPurifier_VarParser::getTypeName($type)." not implemented");
}
/**
@@ -119,7 +136,17 @@ class HTMLPurifier_VarParser
*/
protected function errorGeneric($var, $type) {
$vtype = gettype($var);
$this->error("Expected type $type, got $vtype");
$this->error("Expected type ".HTMLPurifier_VarParser::getTypeName($type).", got $vtype");
}
static public function getTypeName($type) {
static $lookup;
if (!$lookup) {
// Lazy load the alternative lookup table
$lookup = array_flip(HTMLPurifier_VarParser::$types);
}
if (!isset($lookup[$type])) return 'unknown';
return $lookup[$type];
}
}