PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/feed/factory.php

https://gitlab.com/vitaliylukin91/lavka
PHP | 142 lines | 66 code | 14 blank | 62 comment | 11 complexity | 8bf70da3bec2388ff3c7e23d05bf46ab MD5 | raw file
  1<?php
  2/**
  3 * @package     Joomla.Platform
  4 * @subpackage  Feed
  5 *
  6 * @copyright   Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7 * @license     GNU General Public License version 2 or later; see LICENSE
  8 */
  9
 10defined('JPATH_PLATFORM') or die;
 11
 12/**
 13 * Feed factory class.
 14 *
 15 * @since  12.3
 16 */
 17class JFeedFactory
 18{
 19	/**
 20	 * @var    array  The list of registered parser classes for feeds.
 21	 * @since  12.3
 22	 */
 23	protected $parsers = array('rss' => 'JFeedParserRss', 'feed' => 'JFeedParserAtom');
 24
 25	/**
 26	 * Method to load a URI into the feed reader for parsing.
 27	 *
 28	 * @param   string  $uri  The URI of the feed to load. Idn uris must be passed already converted to punycode.
 29	 *
 30	 * @return  JFeedReader
 31	 *
 32	 * @since   12.3
 33	 * @throws  InvalidArgumentException
 34	 * @throws  RuntimeException
 35	 */
 36	public function getFeed($uri)
 37	{
 38		// Create the XMLReader object.
 39		$reader = new XMLReader;
 40
 41		// Open the URI within the stream reader.
 42		if (!@$reader->open($uri, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING))
 43		{
 44			// If allow_url_fopen is enabled
 45			if (ini_get('allow_url_fopen'))
 46			{
 47				// This is an error
 48				throw new RuntimeException('Unable to open the feed.');
 49			}
 50			else
 51			{
 52				// Retry with JHttpFactory that allow using CURL and Sockets as alternative method when available
 53				$connector = JHttpFactory::getHttp();
 54				$feed = $connector->get($uri);
 55
 56				// Set the value to the XMLReader parser
 57				if (!$reader->xml($feed->body, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING))
 58				{
 59					throw new RuntimeException('Unable to parse the feed.');
 60				}
 61			}
 62		}
 63
 64		try
 65		{
 66			// Skip ahead to the root node.
 67			while ($reader->read())
 68			{
 69				if ($reader->nodeType == XMLReader::ELEMENT)
 70				{
 71					break;
 72				}
 73			}
 74		}
 75		catch (Exception $e)
 76		{
 77			throw new RuntimeException('Error reading feed.');
 78		}
 79
 80		// Setup the appopriate feed parser for the feed.
 81		$parser = $this->_fetchFeedParser($reader->name, $reader);
 82
 83		return $parser->parse();
 84	}
 85
 86	/**
 87	 * Method to register a JFeedParser class for a given root tag name.
 88	 *
 89	 * @param   string   $tagName    The root tag name for which to register the parser class.
 90	 * @param   string   $className  The JFeedParser class name to register for a root tag name.
 91	 * @param   boolean  $overwrite  True to overwrite the parser class if one is already registered.
 92	 *
 93	 * @return  JFeedFactory
 94	 *
 95	 * @since   12.3
 96	 * @throws  InvalidArgumentException
 97	 */
 98	public function registerParser($tagName, $className, $overwrite = false)
 99	{
100		// Verify that the class exists.
101		if (!class_exists($className))
102		{
103			throw new InvalidArgumentException('The feed parser class ' . $className . ' does not exist.');
104		}
105
106		// Validate that the tag name is valid.
107		if (!preg_match('/\A(?!XML)[a-z][\w0-9-]*/i', $tagName))
108		{
109			throw new InvalidArgumentException('The tag name ' . $tagName . ' is not valid.');
110		}
111
112		// Register the given parser class for the tag name if nothing registered or the overwrite flag set.
113		if (empty($this->parsers[$tagName]) || (bool) $overwrite)
114		{
115			$this->parsers[(string) $tagName] = (string) $className;
116		}
117
118		return $this;
119	}
120
121	/**
122	 * Method to return a new JFeedParser object based on the registered parsers and a given type.
123	 *
124	 * @param   string     $type    The name of parser to return.
125	 * @param   XMLReader  $reader  The XMLReader instance for the feed.
126	 *
127	 * @return  JFeedParser
128	 *
129	 * @since   12.3
130	 * @throws  LogicException
131	 */
132	private function _fetchFeedParser($type, XMLReader $reader)
133	{
134		// Look for a registered parser for the feed type.
135		if (empty($this->parsers[$type]))
136		{
137			throw new LogicException('No registered feed parser for type ' . $type . '.');
138		}
139
140		return new $this->parsers[$type]($reader);
141	}
142}