SimplePie_Registry

Handles creating objects and calling methods.

Defined (1)

The class is defined in the following location(s).

/wp-includes/SimplePie/Registry.php  
  1. class SimplePie_Registry 
  2. /** 
  3. * Default class mapping 
  4. * Overriding classes *must* subclass these. 
  5. * @var array 
  6. */ 
  7. protected $default = array( 
  8. 'Cache' => 'SimplePie_Cache',  
  9. 'Locator' => 'SimplePie_Locator',  
  10. 'Parser' => 'SimplePie_Parser',  
  11. 'File' => 'SimplePie_File',  
  12. 'Sanitize' => 'SimplePie_Sanitize',  
  13. 'Item' => 'SimplePie_Item',  
  14. 'Author' => 'SimplePie_Author',  
  15. 'Category' => 'SimplePie_Category',  
  16. 'Enclosure' => 'SimplePie_Enclosure',  
  17. 'Caption' => 'SimplePie_Caption',  
  18. 'Copyright' => 'SimplePie_Copyright',  
  19. 'Credit' => 'SimplePie_Credit',  
  20. 'Rating' => 'SimplePie_Rating',  
  21. 'Restriction' => 'SimplePie_Restriction',  
  22. 'Content_Type_Sniffer' => 'SimplePie_Content_Type_Sniffer',  
  23. 'Source' => 'SimplePie_Source',  
  24. 'Misc' => 'SimplePie_Misc',  
  25. 'XML_Declaration_Parser' => 'SimplePie_XML_Declaration_Parser',  
  26. 'Parse_Date' => 'SimplePie_Parse_Date',  
  27. ); 
  28.  
  29. /** 
  30. * Class mapping 
  31. * @see register() 
  32. * @var array 
  33. */ 
  34. protected $classes = array(); 
  35.  
  36. /** 
  37. * Legacy classes 
  38. * @see register() 
  39. * @var array 
  40. */ 
  41. protected $legacy = array(); 
  42.  
  43. /** 
  44. * Constructor 
  45. * No-op 
  46. */ 
  47. public function __construct() { } 
  48.  
  49. /** 
  50. * Register a class 
  51. * @param string $type See {@see $default} for names 
  52. * @param string $class Class name, must subclass the corresponding default 
  53. * @param bool $legacy Whether to enable legacy support for this class 
  54. * @return bool Successfulness 
  55. */ 
  56. public function register($type, $class, $legacy = false) 
  57. if (!is_subclass_of($class, $this->default[$type])) 
  58. return false; 
  59.  
  60. $this->classes[$type] = $class; 
  61.  
  62. if ($legacy) 
  63. $this->legacy[] = $class; 
  64.  
  65. return true; 
  66.  
  67. /** 
  68. * Get the class registered for a type 
  69. * Where possible, use {@see create()} or {@see call()} instead 
  70. * @param string $type 
  71. * @return string|null 
  72. */ 
  73. public function get_class($type) 
  74. if (!empty($this->classes[$type])) 
  75. return $this->classes[$type]; 
  76. if (!empty($this->default[$type])) 
  77. return $this->default[$type]; 
  78.  
  79. return null; 
  80.  
  81. /** 
  82. * Create a new instance of a given type 
  83. * @param string $type 
  84. * @param array $parameters Parameters to pass to the constructor 
  85. * @return object Instance of class 
  86. */ 
  87. public function &create($type, $parameters = array()) 
  88. $class = $this->get_class($type); 
  89.  
  90. if (in_array($class, $this->legacy)) 
  91. switch ($type) 
  92. case 'locator': 
  93. // Legacy: file, timeout, useragent, file_class, max_checked_feeds, content_type_sniffer_class 
  94. // Specified: file, timeout, useragent, max_checked_feeds 
  95. $replacement = array($this->get_class('file'), $parameters[3], $this->get_class('content_type_sniffer')); 
  96. array_splice($parameters, 3, 1, $replacement); 
  97. break; 
  98.  
  99. if (!method_exists($class, '__construct')) 
  100. $instance = new $class; 
  101. else 
  102. $reflector = new ReflectionClass($class); 
  103. $instance = $reflector->newInstanceArgs($parameters); 
  104.  
  105. if (method_exists($instance, 'set_registry')) 
  106. $instance->set_registry($this); 
  107. return $instance; 
  108.  
  109. /** 
  110. * Call a static method for a type 
  111. * @param string $type 
  112. * @param string $method 
  113. * @param array $parameters 
  114. * @return mixed 
  115. */ 
  116. public function &call($type, $method, $parameters = array()) 
  117. $class = $this->get_class($type); 
  118.  
  119. if (in_array($class, $this->legacy)) 
  120. switch ($type) 
  121. case 'Cache': 
  122. // For backwards compatibility with old non-static 
  123. // Cache::create() methods 
  124. if ($method === 'get_handler') 
  125. $result = @call_user_func_array(array($class, 'create'), $parameters); 
  126. return $result; 
  127. break; 
  128.  
  129. $result = call_user_func_array(array($class, $method), $parameters); 
  130. return $result;