File indexing completed on 2024-12-22 05:36:40

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Feed_Writer
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @category   Zend
0024  * @package    Zend_Feed_Writer
0025  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0026  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0027  */
0028 class Zend_Feed_Writer
0029 {
0030     /**
0031      * Namespace constants
0032      */
0033     const NAMESPACE_ATOM_03  = 'http://purl.org/atom/ns#';
0034     const NAMESPACE_ATOM_10  = 'http://www.w3.org/2005/Atom';
0035     const NAMESPACE_RDF      = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
0036     const NAMESPACE_RSS_090  = 'http://my.netscape.com/rdf/simple/0.9/';
0037     const NAMESPACE_RSS_10   = 'http://purl.org/rss/1.0/';
0038 
0039     /**
0040      * Feed type constants
0041      */
0042     const TYPE_ANY              = 'any';
0043     const TYPE_ATOM_03          = 'atom-03';
0044     const TYPE_ATOM_10          = 'atom-10';
0045     const TYPE_ATOM_ANY         = 'atom';
0046     const TYPE_RSS_090          = 'rss-090';
0047     const TYPE_RSS_091          = 'rss-091';
0048     const TYPE_RSS_091_NETSCAPE = 'rss-091n';
0049     const TYPE_RSS_091_USERLAND = 'rss-091u';
0050     const TYPE_RSS_092          = 'rss-092';
0051     const TYPE_RSS_093          = 'rss-093';
0052     const TYPE_RSS_094          = 'rss-094';
0053     const TYPE_RSS_10           = 'rss-10';
0054     const TYPE_RSS_20           = 'rss-20';
0055     const TYPE_RSS_ANY          = 'rss';
0056 
0057     /**
0058      * PluginLoader instance used by component
0059      *
0060      * @var Zend_Loader_PluginLoader_Interface
0061      */
0062     protected static $_pluginLoader = null;
0063 
0064     /**
0065      * Path on which to search for Extension classes
0066      *
0067      * @var array
0068      */
0069     protected static $_prefixPaths = array();
0070 
0071     /**
0072      * Array of registered extensions by class postfix (after the base class
0073      * name) across four categories - data containers and renderers for entry
0074      * and feed levels.
0075      *
0076      * @var array
0077      */
0078     protected static $_extensions = array(
0079         'entry'         => array(),
0080         'feed'          => array(),
0081         'entryRenderer' => array(),
0082         'feedRenderer'  => array(),
0083     );
0084 
0085     /**
0086      * Set plugin loader for use with Extensions
0087      *
0088      * @param  Zend_Loader_PluginLoader_Interface
0089      */
0090     public static function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader)
0091     {
0092         self::$_pluginLoader = $loader;
0093     }
0094 
0095     /**
0096      * Get plugin loader for use with Extensions
0097      *
0098      * @return  Zend_Loader_PluginLoader_Interface
0099      */
0100     public static function getPluginLoader()
0101     {
0102         if (!isset(self::$_pluginLoader)) {
0103             // require_once 'Zend/Loader/PluginLoader.php';
0104             self::$_pluginLoader = new Zend_Loader_PluginLoader(array(
0105                 'Zend_Feed_Writer_Extension_' => 'Zend/Feed/Writer/Extension/',
0106             ));
0107         }
0108         return self::$_pluginLoader;
0109     }
0110 
0111     /**
0112      * Add prefix path for loading Extensions
0113      *
0114      * @param  string $prefix
0115      * @param  string $path
0116      * @return void
0117      */
0118     public static function addPrefixPath($prefix, $path)
0119     {
0120         $prefix = rtrim($prefix, '_');
0121         $path   = rtrim($path, DIRECTORY_SEPARATOR);
0122         self::getPluginLoader()->addPrefixPath($prefix, $path);
0123     }
0124 
0125     /**
0126      * Add multiple Extension prefix paths at once
0127      *
0128      * @param  array $spec
0129      * @return void
0130      */
0131     public static function addPrefixPaths(array $spec)
0132     {
0133         if (isset($spec['prefix']) && isset($spec['path'])) {
0134             self::addPrefixPath($spec['prefix'], $spec['path']);
0135         }
0136         foreach ($spec as $prefixPath) {
0137             if (isset($prefixPath['prefix']) && isset($prefixPath['path'])) {
0138                 self::addPrefixPath($prefixPath['prefix'], $prefixPath['path']);
0139             }
0140         }
0141     }
0142 
0143     /**
0144      * Register an Extension by name
0145      *
0146      * @param  string $name
0147      * @return void
0148      * @throws Zend_Feed_Exception if unable to resolve Extension class
0149      */
0150     public static function registerExtension($name)
0151     {
0152         $feedName  = $name . '_Feed';
0153         $entryName = $name . '_Entry';
0154         $feedRendererName  = $name . '_Renderer_Feed';
0155         $entryRendererName = $name . '_Renderer_Entry';
0156         if (self::isRegistered($name)) {
0157             if (self::getPluginLoader()->isLoaded($feedName)
0158                 || self::getPluginLoader()->isLoaded($entryName)
0159                 || self::getPluginLoader()->isLoaded($feedRendererName)
0160                 || self::getPluginLoader()->isLoaded($entryRendererName)
0161             ) {
0162                 return;
0163             }
0164         }
0165         try {
0166             self::getPluginLoader()->load($feedName);
0167             self::$_extensions['feed'][] = $feedName;
0168         } catch (Zend_Loader_PluginLoader_Exception $e) {
0169         }
0170         try {
0171             self::getPluginLoader()->load($entryName);
0172             self::$_extensions['entry'][] = $entryName;
0173         } catch (Zend_Loader_PluginLoader_Exception $e) {
0174         }
0175         try {
0176             self::getPluginLoader()->load($feedRendererName);
0177             self::$_extensions['feedRenderer'][] = $feedRendererName;
0178         } catch (Zend_Loader_PluginLoader_Exception $e) {
0179         }
0180         try {
0181             self::getPluginLoader()->load($entryRendererName);
0182             self::$_extensions['entryRenderer'][] = $entryRendererName;
0183         } catch (Zend_Loader_PluginLoader_Exception $e) {
0184         }
0185         if (!self::getPluginLoader()->isLoaded($feedName)
0186             && !self::getPluginLoader()->isLoaded($entryName)
0187             && !self::getPluginLoader()->isLoaded($feedRendererName)
0188             && !self::getPluginLoader()->isLoaded($entryRendererName)
0189         ) {
0190             // require_once 'Zend/Feed/Exception.php';
0191             throw new Zend_Feed_Exception('Could not load extension: ' . $name
0192                 . 'using Plugin Loader. Check prefix paths are configured and extension exists.');
0193         }
0194     }
0195 
0196     /**
0197      * Is a given named Extension registered?
0198      *
0199      * @param  string $extensionName
0200      * @return boolean
0201      */
0202     public static function isRegistered($extensionName)
0203     {
0204         $feedName  = $extensionName . '_Feed';
0205         $entryName = $extensionName . '_Entry';
0206         $feedRendererName  = $extensionName . '_Renderer_Feed';
0207         $entryRendererName = $extensionName . '_Renderer_Entry';
0208         if (in_array($feedName, self::$_extensions['feed'])
0209             || in_array($entryName, self::$_extensions['entry'])
0210             || in_array($feedRendererName, self::$_extensions['feedRenderer'])
0211             || in_array($entryRendererName, self::$_extensions['entryRenderer'])
0212         ) {
0213             return true;
0214         }
0215         return false;
0216     }
0217 
0218     /**
0219      * Get a list of extensions
0220      *
0221      * @return array
0222      */
0223     public static function getExtensions()
0224     {
0225         return self::$_extensions;
0226     }
0227 
0228     /**
0229      * Reset class state to defaults
0230      *
0231      * @return void
0232      */
0233     public static function reset()
0234     {
0235         self::$_pluginLoader = null;
0236         self::$_prefixPaths  = array();
0237         self::$_extensions   = array(
0238             'entry'         => array(),
0239             'feed'          => array(),
0240             'entryRenderer' => array(),
0241             'feedRenderer'  => array(),
0242         );
0243     }
0244 
0245     /**
0246      * Register core (default) extensions
0247      *
0248      * @return void
0249      */
0250     public static function registerCoreExtensions()
0251     {
0252         self::registerExtension('DublinCore');
0253         self::registerExtension('Content');
0254         self::registerExtension('Atom');
0255         self::registerExtension('Slash');
0256         self::registerExtension('WellFormedWeb');
0257         self::registerExtension('Threading');
0258         self::registerExtension('ITunes');
0259     }
0260 
0261     public static function lcfirst($str)
0262     {
0263         $str[0] = strtolower($str[0]);
0264         return $str;
0265     }
0266 
0267 }