PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/SimplePie/Registry.php

https://bitbucket.org/abnopanda/wordpress
PHP | 225 lines | 100 code | 17 blank | 108 comment | 11 complexity | 1cc8a2e6c0b5dd3176398d6400f0d9b8 MD5 | raw file
  1. <?php
  2. /**
  3. * SimplePie
  4. *
  5. * A PHP-Based RSS and Atom Feed Framework.
  6. * Takes the hard work out of managing a complete RSS/Atom solution.
  7. *
  8. * Copyright (c) 2004-2012, Ryan Parman, Geoffrey Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  22. * to endorse or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package SimplePie
  36. * @version 1.3.1
  37. * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue
  38. * @author Ryan Parman
  39. * @author Geoffrey Sneddon
  40. * @author Ryan McCue
  41. * @link http://simplepie.org/ SimplePie
  42. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  43. */
  44. /**
  45. * Handles creating objects and calling methods
  46. *
  47. * Access this via {@see SimplePie::get_registry()}
  48. *
  49. * @package SimplePie
  50. */
  51. class SimplePie_Registry
  52. {
  53. /**
  54. * Default class mapping
  55. *
  56. * Overriding classes *must* subclass these.
  57. *
  58. * @var array
  59. */
  60. protected $default = array(
  61. 'Cache' => 'SimplePie_Cache',
  62. 'Locator' => 'SimplePie_Locator',
  63. 'Parser' => 'SimplePie_Parser',
  64. 'File' => 'SimplePie_File',
  65. 'Sanitize' => 'SimplePie_Sanitize',
  66. 'Item' => 'SimplePie_Item',
  67. 'Author' => 'SimplePie_Author',
  68. 'Category' => 'SimplePie_Category',
  69. 'Enclosure' => 'SimplePie_Enclosure',
  70. 'Caption' => 'SimplePie_Caption',
  71. 'Copyright' => 'SimplePie_Copyright',
  72. 'Credit' => 'SimplePie_Credit',
  73. 'Rating' => 'SimplePie_Rating',
  74. 'Restriction' => 'SimplePie_Restriction',
  75. 'Content_Type_Sniffer' => 'SimplePie_Content_Type_Sniffer',
  76. 'Source' => 'SimplePie_Source',
  77. 'Misc' => 'SimplePie_Misc',
  78. 'XML_Declaration_Parser' => 'SimplePie_XML_Declaration_Parser',
  79. 'Parse_Date' => 'SimplePie_Parse_Date',
  80. );
  81. /**
  82. * Class mapping
  83. *
  84. * @see register()
  85. * @var array
  86. */
  87. protected $classes = array();
  88. /**
  89. * Legacy classes
  90. *
  91. * @see register()
  92. * @var array
  93. */
  94. protected $legacy = array();
  95. /**
  96. * Constructor
  97. *
  98. * No-op
  99. */
  100. public function __construct() { }
  101. /**
  102. * Register a class
  103. *
  104. * @param string $type See {@see $default} for names
  105. * @param string $class Class name, must subclass the corresponding default
  106. * @param bool $legacy Whether to enable legacy support for this class
  107. * @return bool Successfulness
  108. */
  109. public function register($type, $class, $legacy = false)
  110. {
  111. if (!is_subclass_of($class, $this->default[$type]))
  112. {
  113. return false;
  114. }
  115. $this->classes[$type] = $class;
  116. if ($legacy)
  117. {
  118. $this->legacy[] = $class;
  119. }
  120. return true;
  121. }
  122. /**
  123. * Get the class registered for a type
  124. *
  125. * Where possible, use {@see create()} or {@see call()} instead
  126. *
  127. * @param string $type
  128. * @return string|null
  129. */
  130. public function get_class($type)
  131. {
  132. if (!empty($this->classes[$type]))
  133. {
  134. return $this->classes[$type];
  135. }
  136. if (!empty($this->default[$type]))
  137. {
  138. return $this->default[$type];
  139. }
  140. return null;
  141. }
  142. /**
  143. * Create a new instance of a given type
  144. *
  145. * @param string $type
  146. * @param array $parameters Parameters to pass to the constructor
  147. * @return object Instance of class
  148. */
  149. public function &create($type, $parameters = array())
  150. {
  151. $class = $this->get_class($type);
  152. if (in_array($class, $this->legacy))
  153. {
  154. switch ($type)
  155. {
  156. case 'locator':
  157. // Legacy: file, timeout, useragent, file_class, max_checked_feeds, content_type_sniffer_class
  158. // Specified: file, timeout, useragent, max_checked_feeds
  159. $replacement = array($this->get_class('file'), $parameters[3], $this->get_class('content_type_sniffer'));
  160. array_splice($parameters, 3, 1, $replacement);
  161. break;
  162. }
  163. }
  164. if (!method_exists($class, '__construct'))
  165. {
  166. $instance = new $class;
  167. }
  168. else
  169. {
  170. $reflector = new ReflectionClass($class);
  171. $instance = $reflector->newInstanceArgs($parameters);
  172. }
  173. if (method_exists($instance, 'set_registry'))
  174. {
  175. $instance->set_registry($this);
  176. }
  177. return $instance;
  178. }
  179. /**
  180. * Call a static method for a type
  181. *
  182. * @param string $type
  183. * @param string $method
  184. * @param array $parameters
  185. * @return mixed
  186. */
  187. public function &call($type, $method, $parameters = array())
  188. {
  189. $class = $this->get_class($type);
  190. if (in_array($class, $this->legacy))
  191. {
  192. switch ($type)
  193. {
  194. case 'Cache':
  195. // For backwards compatibility with old non-static
  196. // Cache::create() methods
  197. if ($method === 'get_handler')
  198. {
  199. $result = @call_user_func_array(array($class, 'create'), $parameters);
  200. return $result;
  201. }
  202. break;
  203. }
  204. }
  205. $result = call_user_func_array(array($class, $method), $parameters);
  206. return $result;
  207. }
  208. }