PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simplepie/library/SimplePie/Registry.php

https://bitbucket.org/moodle/moodle
PHP | 224 lines | 100 code | 17 blank | 107 comment | 11 complexity | f8a0cc5c97becf533810a358fe605974 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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-2016, Ryan Parman, Sam 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. * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
  37. * @author Ryan Parman
  38. * @author Sam Sneddon
  39. * @author Ryan McCue
  40. * @link http://simplepie.org/ SimplePie
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. */
  43. /**
  44. * Handles creating objects and calling methods
  45. *
  46. * Access this via {@see SimplePie::get_registry()}
  47. *
  48. * @package SimplePie
  49. */
  50. class SimplePie_Registry
  51. {
  52. /**
  53. * Default class mapping
  54. *
  55. * Overriding classes *must* subclass these.
  56. *
  57. * @var array
  58. */
  59. protected $default = array(
  60. 'Cache' => 'SimplePie_Cache',
  61. 'Locator' => 'SimplePie_Locator',
  62. 'Parser' => 'SimplePie_Parser',
  63. 'File' => 'SimplePie_File',
  64. 'Sanitize' => 'SimplePie_Sanitize',
  65. 'Item' => 'SimplePie_Item',
  66. 'Author' => 'SimplePie_Author',
  67. 'Category' => 'SimplePie_Category',
  68. 'Enclosure' => 'SimplePie_Enclosure',
  69. 'Caption' => 'SimplePie_Caption',
  70. 'Copyright' => 'SimplePie_Copyright',
  71. 'Credit' => 'SimplePie_Credit',
  72. 'Rating' => 'SimplePie_Rating',
  73. 'Restriction' => 'SimplePie_Restriction',
  74. 'Content_Type_Sniffer' => 'SimplePie_Content_Type_Sniffer',
  75. 'Source' => 'SimplePie_Source',
  76. 'Misc' => 'SimplePie_Misc',
  77. 'XML_Declaration_Parser' => 'SimplePie_XML_Declaration_Parser',
  78. 'Parse_Date' => 'SimplePie_Parse_Date',
  79. );
  80. /**
  81. * Class mapping
  82. *
  83. * @see register()
  84. * @var array
  85. */
  86. protected $classes = array();
  87. /**
  88. * Legacy classes
  89. *
  90. * @see register()
  91. * @var array
  92. */
  93. protected $legacy = array();
  94. /**
  95. * Constructor
  96. *
  97. * No-op
  98. */
  99. public function __construct() { }
  100. /**
  101. * Register a class
  102. *
  103. * @param string $type See {@see $default} for names
  104. * @param string $class Class name, must subclass the corresponding default
  105. * @param bool $legacy Whether to enable legacy support for this class
  106. * @return bool Successfulness
  107. */
  108. public function register($type, $class, $legacy = false)
  109. {
  110. if (!@is_subclass_of($class, $this->default[$type]))
  111. {
  112. return false;
  113. }
  114. $this->classes[$type] = $class;
  115. if ($legacy)
  116. {
  117. $this->legacy[] = $class;
  118. }
  119. return true;
  120. }
  121. /**
  122. * Get the class registered for a type
  123. *
  124. * Where possible, use {@see create()} or {@see call()} instead
  125. *
  126. * @param string $type
  127. * @return string|null
  128. */
  129. public function get_class($type)
  130. {
  131. if (!empty($this->classes[$type]))
  132. {
  133. return $this->classes[$type];
  134. }
  135. if (!empty($this->default[$type]))
  136. {
  137. return $this->default[$type];
  138. }
  139. return null;
  140. }
  141. /**
  142. * Create a new instance of a given type
  143. *
  144. * @param string $type
  145. * @param array $parameters Parameters to pass to the constructor
  146. * @return object Instance of class
  147. */
  148. public function &create($type, $parameters = array())
  149. {
  150. $class = $this->get_class($type);
  151. if (in_array($class, $this->legacy))
  152. {
  153. switch ($type)
  154. {
  155. case 'locator':
  156. // Legacy: file, timeout, useragent, file_class, max_checked_feeds, content_type_sniffer_class
  157. // Specified: file, timeout, useragent, max_checked_feeds
  158. $replacement = array($this->get_class('file'), $parameters[3], $this->get_class('content_type_sniffer'));
  159. array_splice($parameters, 3, 1, $replacement);
  160. break;
  161. }
  162. }
  163. if (!method_exists($class, '__construct'))
  164. {
  165. $instance = new $class;
  166. }
  167. else
  168. {
  169. $reflector = new ReflectionClass($class);
  170. $instance = $reflector->newInstanceArgs($parameters);
  171. }
  172. if (method_exists($instance, 'set_registry'))
  173. {
  174. $instance->set_registry($this);
  175. }
  176. return $instance;
  177. }
  178. /**
  179. * Call a static method for a type
  180. *
  181. * @param string $type
  182. * @param string $method
  183. * @param array $parameters
  184. * @return mixed
  185. */
  186. public function &call($type, $method, $parameters = array())
  187. {
  188. $class = $this->get_class($type);
  189. if (in_array($class, $this->legacy))
  190. {
  191. switch ($type)
  192. {
  193. case 'Cache':
  194. // For backwards compatibility with old non-static
  195. // Cache::create() methods
  196. if ($method === 'get_handler')
  197. {
  198. $result = @call_user_func_array(array($class, 'create'), $parameters);
  199. return $result;
  200. }
  201. break;
  202. }
  203. }
  204. $result = call_user_func_array(array($class, $method), $parameters);
  205. return $result;
  206. }
  207. }