PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/moodle/lib/htmlpurifier/HTMLPurifier/DefinitionCacheFactory.php

https://bitbucket.org/geek745/moodle-db2
PHP | 109 lines | 71 code | 13 blank | 25 comment | 9 complexity | 44236ef38460b805b7e65b77c942e189 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0
  1. <?php
  2. require_once 'HTMLPurifier/DefinitionCache.php';
  3. require_once 'HTMLPurifier/DefinitionCache/Serializer.php';
  4. HTMLPurifier_ConfigSchema::define(
  5. 'Cache', 'DefinitionImpl', 'Serializer', 'string/null', '
  6. This directive defines which method to use when caching definitions,
  7. the complex data-type that makes HTML Purifier tick. Set to null
  8. to disable caching (not recommended, as you will see a definite
  9. performance degradation). This directive has been available since 2.0.0.
  10. ');
  11. HTMLPurifier_ConfigSchema::defineAlias(
  12. 'Core', 'DefinitionCache',
  13. 'Cache', 'DefinitionImpl'
  14. );
  15. /**
  16. * Responsible for creating definition caches.
  17. */
  18. class HTMLPurifier_DefinitionCacheFactory
  19. {
  20. var $caches = array('Serializer' => array());
  21. var $implementations = array();
  22. var $decorators = array();
  23. /**
  24. * Initialize default decorators
  25. */
  26. function setup() {
  27. $this->addDecorator('Cleanup');
  28. }
  29. /**
  30. * Retrieves an instance of global definition cache factory.
  31. * @static
  32. */
  33. function &instance($prototype = null) {
  34. static $instance;
  35. if ($prototype !== null) {
  36. $instance = $prototype;
  37. } elseif ($instance === null || $prototype === true) {
  38. $instance = new HTMLPurifier_DefinitionCacheFactory();
  39. $instance->setup();
  40. }
  41. return $instance;
  42. }
  43. /**
  44. * Registers a new definition cache object
  45. * @param $short Short name of cache object, for reference
  46. * @param $long Full class name of cache object, for construction
  47. */
  48. function register($short, $long) {
  49. $this->implementations[$short] = $long;
  50. }
  51. /**
  52. * Factory method that creates a cache object based on configuration
  53. * @param $name Name of definitions handled by cache
  54. * @param $config Instance of HTMLPurifier_Config
  55. */
  56. function &create($type, $config) {
  57. $method = $config->get('Cache', 'DefinitionImpl');
  58. if ($method === null) {
  59. $null = new HTMLPurifier_DefinitionCache_Null($type);
  60. return $null;
  61. }
  62. if (!empty($this->caches[$method][$type])) {
  63. return $this->caches[$method][$type];
  64. }
  65. if (
  66. isset($this->implementations[$method]) &&
  67. class_exists($class = $this->implementations[$method])
  68. ) {
  69. $cache = new $class($type);
  70. } else {
  71. if ($method != 'Serializer') {
  72. trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING);
  73. }
  74. $cache = new HTMLPurifier_DefinitionCache_Serializer($type);
  75. }
  76. foreach ($this->decorators as $decorator) {
  77. $new_cache = $decorator->decorate($cache);
  78. // prevent infinite recursion in PHP 4
  79. unset($cache);
  80. $cache = $new_cache;
  81. }
  82. $this->caches[$method][$type] = $cache;
  83. return $this->caches[$method][$type];
  84. }
  85. /**
  86. * Registers a decorator to add to all new cache objects
  87. * @param
  88. */
  89. function addDecorator($decorator) {
  90. if (is_string($decorator)) {
  91. $class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
  92. $decorator = new $class;
  93. }
  94. $this->decorators[$decorator->name] = $decorator;
  95. }
  96. }