[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Feed/ -> FeedFactory.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2012 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Feed;
  11  
  12  use Joomla\CMS\Http\HttpFactory;
  13  use Joomla\Registry\Registry;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Feed factory class.
  21   *
  22   * @since  3.1.4
  23   */
  24  class FeedFactory
  25  {
  26      /**
  27       * @var    array  The list of registered parser classes for feeds.
  28       * @since  3.1.4
  29       */
  30      protected $parsers = array('rss' => 'Joomla\\CMS\\Feed\\Parser\\RssParser', 'feed' => 'Joomla\\CMS\\Feed\\Parser\\AtomParser');
  31  
  32      /**
  33       * Method to load a URI into the feed reader for parsing.
  34       *
  35       * @param   string  $uri  The URI of the feed to load. Idn uris must be passed already converted to punycode.
  36       *
  37       * @return  Feed
  38       *
  39       * @since   3.1.4
  40       * @throws  \InvalidArgumentException
  41       * @throws  \RuntimeException
  42       */
  43      public function getFeed($uri)
  44      {
  45          // Create the XMLReader object.
  46          $reader = new \XMLReader();
  47  
  48          // Open the URI within the stream reader.
  49          if (!@$reader->open($uri, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING)) {
  50              // Retry with JHttpFactory that allow using CURL and Sockets as alternative method when available
  51  
  52              // Adding a valid user agent string, otherwise some feed-servers returning an error
  53              $options = new Registry();
  54              $options->set('userAgent', 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0');
  55  
  56              try {
  57                  $response = HttpFactory::getHttp($options)->get($uri);
  58              } catch (\RuntimeException $e) {
  59                  throw new \RuntimeException('Unable to open the feed.', $e->getCode(), $e);
  60              }
  61  
  62              if ($response->code != 200) {
  63                  throw new \RuntimeException('Unable to open the feed.');
  64              }
  65  
  66              // Set the value to the XMLReader parser
  67              if (!$reader->XML($response->body, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING)) {
  68                  throw new \RuntimeException('Unable to parse the feed.');
  69              }
  70          }
  71  
  72          try {
  73              // Skip ahead to the root node.
  74              while ($reader->read()) {
  75                  if ($reader->nodeType == \XMLReader::ELEMENT) {
  76                      break;
  77                  }
  78              }
  79          } catch (\Exception $e) {
  80              throw new \RuntimeException('Error reading feed.', $e->getCode(), $e);
  81          }
  82  
  83          // Setup the appropriate feed parser for the feed.
  84          $parser = $this->_fetchFeedParser($reader->name, $reader);
  85  
  86          return $parser->parse();
  87      }
  88  
  89      /**
  90       * Method to register a FeedParser class for a given root tag name.
  91       *
  92       * @param   string   $tagName    The root tag name for which to register the parser class.
  93       * @param   string   $className  The FeedParser class name to register for a root tag name.
  94       * @param   boolean  $overwrite  True to overwrite the parser class if one is already registered.
  95       *
  96       * @return  FeedFactory
  97       *
  98       * @since   3.1.4
  99       * @throws  \InvalidArgumentException
 100       */
 101      public function registerParser($tagName, $className, $overwrite = false)
 102      {
 103          // Verify that the class exists.
 104          if (!class_exists($className)) {
 105              throw new \InvalidArgumentException('The feed parser class ' . $className . ' does not exist.');
 106          }
 107  
 108          // Validate that the tag name is valid.
 109          if (!preg_match('/\A(?!XML)[a-z][\w0-9-]*/i', $tagName)) {
 110              throw new \InvalidArgumentException('The tag name ' . $tagName . ' is not valid.');
 111          }
 112  
 113          // Register the given parser class for the tag name if nothing registered or the overwrite flag set.
 114          if (empty($this->parsers[$tagName]) || (bool) $overwrite) {
 115              $this->parsers[(string) $tagName] = (string) $className;
 116          }
 117  
 118          return $this;
 119      }
 120  
 121      /**
 122       * Method to get the registered Parsers
 123       *
 124       * @return array
 125       *
 126       * @since   4.0.0
 127       */
 128      public function getParsers()
 129      {
 130          return $this->parsers;
 131      }
 132  
 133      /**
 134       * Method to return a new JFeedParser object based on the registered parsers and a given type.
 135       *
 136       * @param   string      $type    The name of parser to return.
 137       * @param   \XMLReader  $reader  The XMLReader instance for the feed.
 138       *
 139       * @return  FeedParser
 140       *
 141       * @since   3.1.4
 142       * @throws  \LogicException
 143       */
 144      private function _fetchFeedParser($type, \XMLReader $reader)
 145      {
 146          // Look for a registered parser for the feed type.
 147          if (empty($this->parsers[$type])) {
 148              throw new \LogicException('No registered feed parser for type ' . $type . '.');
 149          }
 150  
 151          return new $this->parsers[$type]($reader);
 152      }
 153  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer