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