2
0

Writer.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Feed_Writer
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Feed_Writer
  24. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Feed_Writer
  28. {
  29. /**
  30. * Namespace constants
  31. */
  32. const NAMESPACE_ATOM_03 = 'http://purl.org/atom/ns#';
  33. const NAMESPACE_ATOM_10 = 'http://www.w3.org/2005/Atom';
  34. const NAMESPACE_RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
  35. const NAMESPACE_RSS_090 = 'http://my.netscape.com/rdf/simple/0.9/';
  36. const NAMESPACE_RSS_10 = 'http://purl.org/rss/1.0/';
  37. /**
  38. * Feed type constants
  39. */
  40. const TYPE_ANY = 'any';
  41. const TYPE_ATOM_03 = 'atom-03';
  42. const TYPE_ATOM_10 = 'atom-10';
  43. const TYPE_ATOM_ANY = 'atom';
  44. const TYPE_RSS_090 = 'rss-090';
  45. const TYPE_RSS_091 = 'rss-091';
  46. const TYPE_RSS_091_NETSCAPE = 'rss-091n';
  47. const TYPE_RSS_091_USERLAND = 'rss-091u';
  48. const TYPE_RSS_092 = 'rss-092';
  49. const TYPE_RSS_093 = 'rss-093';
  50. const TYPE_RSS_094 = 'rss-094';
  51. const TYPE_RSS_10 = 'rss-10';
  52. const TYPE_RSS_20 = 'rss-20';
  53. const TYPE_RSS_ANY = 'rss';
  54. /**
  55. * PluginLoader instance used by component
  56. *
  57. * @var Zend_Loader_PluginLoader_Interface
  58. */
  59. protected static $_pluginLoader = null;
  60. /**
  61. * Path on which to search for Extension classes
  62. *
  63. * @var array
  64. */
  65. protected static $_prefixPaths = array();
  66. /**
  67. * Array of registered extensions by class postfix (after the base class
  68. * name) across four categories - data containers and renderers for entry
  69. * and feed levels.
  70. *
  71. * @var array
  72. */
  73. protected static $_extensions = array(
  74. 'entry' => array(),
  75. 'feed' => array(),
  76. 'entryRenderer' => array(),
  77. 'feedRenderer' => array(),
  78. );
  79. /**
  80. * Set plugin loader for use with Extensions
  81. *
  82. * @param Zend_Loader_PluginLoader_Interface
  83. */
  84. public static function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader)
  85. {
  86. self::$_pluginLoader = $loader;
  87. }
  88. /**
  89. * Get plugin loader for use with Extensions
  90. *
  91. * @return Zend_Loader_PluginLoader_Interface
  92. */
  93. public static function getPluginLoader()
  94. {
  95. if (!isset(self::$_pluginLoader)) {
  96. require_once 'Zend/Loader/PluginLoader.php';
  97. self::$_pluginLoader = new Zend_Loader_PluginLoader(array(
  98. 'Zend_Feed_Writer_Extension_' => 'Zend/Feed/Writer/Extension/',
  99. ));
  100. }
  101. return self::$_pluginLoader;
  102. }
  103. /**
  104. * Add prefix path for loading Extensions
  105. *
  106. * @param string $prefix
  107. * @param string $path
  108. * @return void
  109. */
  110. public static function addPrefixPath($prefix, $path)
  111. {
  112. $prefix = rtrim($prefix, '_');
  113. $path = rtrim($path, DIRECTORY_SEPARATOR);
  114. self::getPluginLoader()->addPrefixPath($prefix, $path);
  115. }
  116. /**
  117. * Add multiple Extension prefix paths at once
  118. *
  119. * @param array $spec
  120. * @return void
  121. */
  122. public static function addPrefixPaths(array $spec)
  123. {
  124. if (isset($spec['prefix']) && isset($spec['path'])) {
  125. self::addPrefixPath($spec['prefix'], $spec['path']);
  126. }
  127. foreach ($spec as $prefixPath) {
  128. if (isset($prefixPath['prefix']) && isset($prefixPath['path'])) {
  129. self::addPrefixPath($prefixPath['prefix'], $prefixPath['path']);
  130. }
  131. }
  132. }
  133. /**
  134. * Register an Extension by name
  135. *
  136. * @param string $name
  137. * @return void
  138. * @throws Zend_Feed_Exception if unable to resolve Extension class
  139. */
  140. public static function registerExtension($name)
  141. {
  142. $feedName = $name . '_Feed';
  143. $entryName = $name . '_Entry';
  144. $feedRendererName = $name . '_Renderer_Feed';
  145. $entryRendererName = $name . '_Renderer_Entry';
  146. if (self::isRegistered($name)) {
  147. if (self::getPluginLoader()->isLoaded($feedName)
  148. || self::getPluginLoader()->isLoaded($entryName)
  149. || self::getPluginLoader()->isLoaded($feedRendererName)
  150. || self::getPluginLoader()->isLoaded($entryRendererName)
  151. ) {
  152. return;
  153. }
  154. }
  155. try {
  156. self::getPluginLoader()->load($feedName);
  157. self::$_extensions['feed'][] = $feedName;
  158. } catch (Zend_Loader_PluginLoader_Exception $e) {
  159. }
  160. try {
  161. self::getPluginLoader()->load($entryName);
  162. self::$_extensions['entry'][] = $entryName;
  163. } catch (Zend_Loader_PluginLoader_Exception $e) {
  164. }
  165. try {
  166. self::getPluginLoader()->load($feedRendererName);
  167. self::$_extensions['feedRenderer'][] = $feedRendererName;
  168. } catch (Zend_Loader_PluginLoader_Exception $e) {
  169. }
  170. try {
  171. self::getPluginLoader()->load($entryRendererName);
  172. self::$_extensions['entryRenderer'][] = $entryRendererName;
  173. } catch (Zend_Loader_PluginLoader_Exception $e) {
  174. }
  175. if (!self::getPluginLoader()->isLoaded($feedName)
  176. && !self::getPluginLoader()->isLoaded($entryName)
  177. && !self::getPluginLoader()->isLoaded($feedRendererName)
  178. && !self::getPluginLoader()->isLoaded($entryRendererName)
  179. ) {
  180. require_once 'Zend/Feed/Exception.php';
  181. throw new Zend_Feed_Exception('Could not load extension: ' . $name
  182. . 'using Plugin Loader. Check prefix paths are configured and extension exists.');
  183. }
  184. }
  185. /**
  186. * Is a given named Extension registered?
  187. *
  188. * @param string $extensionName
  189. * @return boolean
  190. */
  191. public static function isRegistered($extensionName)
  192. {
  193. $feedName = $extensionName . '_Feed';
  194. $entryName = $extensionName . '_Entry';
  195. $feedRendererName = $extensionName . '_Renderer_Feed';
  196. $entryRendererName = $extensionName . '_Renderer_Entry';
  197. if (in_array($feedName, self::$_extensions['feed'])
  198. || in_array($entryName, self::$_extensions['entry'])
  199. || in_array($feedRendererName, self::$_extensions['feedRenderer'])
  200. || in_array($entryRendererName, self::$_extensions['entryRenderer'])
  201. ) {
  202. return true;
  203. }
  204. return false;
  205. }
  206. /**
  207. * Get a list of extensions
  208. *
  209. * @return array
  210. */
  211. public static function getExtensions()
  212. {
  213. return self::$_extensions;
  214. }
  215. /**
  216. * Reset class state to defaults
  217. *
  218. * @return void
  219. */
  220. public static function reset()
  221. {
  222. self::$_pluginLoader = null;
  223. self::$_prefixPaths = array();
  224. self::$_extensions = array(
  225. 'entry' => array(),
  226. 'feed' => array(),
  227. 'entryRenderer' => array(),
  228. 'feedRenderer' => array(),
  229. );
  230. }
  231. /**
  232. * Register core (default) extensions
  233. *
  234. * @return void
  235. */
  236. public static function registerCoreExtensions()
  237. {
  238. self::registerExtension('DublinCore');
  239. self::registerExtension('Content');
  240. self::registerExtension('Atom');
  241. self::registerExtension('Slash');
  242. self::registerExtension('WellFormedWeb');
  243. self::registerExtension('Threading');
  244. self::registerExtension('ITunes');
  245. }
  246. public static function lcfirst($str)
  247. {
  248. $str[0] = strtolower($str[0]);
  249. return $str;
  250. }
  251. }