DefinitionCacheFactory.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Responsible for creating definition caches.
  4. */
  5. class HTMLPurifier_DefinitionCacheFactory
  6. {
  7. /**
  8. * @type array
  9. */
  10. protected $caches = array('Serializer' => array());
  11. /**
  12. * @type array
  13. */
  14. protected $implementations = array();
  15. /**
  16. * @type HTMLPurifier_DefinitionCache_Decorator[]
  17. */
  18. protected $decorators = array();
  19. /**
  20. * Initialize default decorators
  21. */
  22. public function setup()
  23. {
  24. $this->addDecorator('Cleanup');
  25. }
  26. /**
  27. * Retrieves an instance of global definition cache factory.
  28. * @param HTMLPurifier_DefinitionCacheFactory $prototype
  29. * @return HTMLPurifier_DefinitionCacheFactory
  30. */
  31. public static function instance($prototype = null)
  32. {
  33. static $instance;
  34. if ($prototype !== null) {
  35. $instance = $prototype;
  36. } elseif ($instance === null || $prototype === true) {
  37. $instance = new HTMLPurifier_DefinitionCacheFactory();
  38. $instance->setup();
  39. }
  40. return $instance;
  41. }
  42. /**
  43. * Registers a new definition cache object
  44. * @param string $short Short name of cache object, for reference
  45. * @param string $long Full class name of cache object, for construction
  46. */
  47. public function register($short, $long)
  48. {
  49. $this->implementations[$short] = $long;
  50. }
  51. /**
  52. * Factory method that creates a cache object based on configuration
  53. * @param string $type Name of definitions handled by cache
  54. * @param HTMLPurifier_Config $config Config instance
  55. * @return mixed
  56. */
  57. public function create($type, $config)
  58. {
  59. $method = $config->get('Cache.DefinitionImpl');
  60. if ($method === null) {
  61. return new HTMLPurifier_DefinitionCache_Null($type);
  62. }
  63. if (!empty($this->caches[$method][$type])) {
  64. return $this->caches[$method][$type];
  65. }
  66. if (isset($this->implementations[$method]) &&
  67. class_exists($class = $this->implementations[$method], false)) {
  68. $cache = new $class($type);
  69. } else {
  70. if ($method != 'Serializer') {
  71. trigger_error("Unrecognized DefinitionCache $method, using Serializer instead", E_USER_WARNING);
  72. }
  73. $cache = new HTMLPurifier_DefinitionCache_Serializer($type);
  74. }
  75. foreach ($this->decorators as $decorator) {
  76. $new_cache = $decorator->decorate($cache);
  77. // prevent infinite recursion in PHP 4
  78. unset($cache);
  79. $cache = $new_cache;
  80. }
  81. $this->caches[$method][$type] = $cache;
  82. return $this->caches[$method][$type];
  83. }
  84. /**
  85. * Registers a decorator to add to all new cache objects
  86. * @param HTMLPurifier_DefinitionCache_Decorator|string $decorator An instance or the name of a decorator
  87. */
  88. public function addDecorator($decorator)
  89. {
  90. if (is_string($decorator)) {
  91. $class = "HTMLPurifier_DefinitionCache_Decorator_$decorator";
  92. $decorator = new $class;
  93. }
  94. $this->decorators[$decorator->name] = $decorator;
  95. }
  96. }
  97. // vim: et sw=4 sts=4