Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
factory.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Feed
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Feed factory class.
14  *
15  * @package Joomla.Platform
16  * @subpackage Feed
17  * @since 12.3
18  */
20 {
21  /**
22  * @var array The list of registered parser classes for feeds.
23  * @since 12.3
24  */
25  protected $parsers = array('rss' => 'JFeedParserRss', 'feed' => 'JFeedParserAtom');
26 
27  /**
28  * Method to load a URI into the feed reader for parsing.
29  *
30  * @param string $uri The URI of the feed to load. Idn uris must be passed already converted to punycode.
31  *
32  * @return JFeedReader
33  *
34  * @since 12.3
35  * @throws InvalidArgumentException
36  * @throws RuntimeException
37  */
38  public function getFeed($uri)
39  {
40  // Create the XMLReader object.
41  $reader = new XMLReader;
42 
43  // Open the URI within the stream reader.
44  if (!$reader->open($uri, null, LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING))
45  {
46  throw new RuntimeException('Unable to open the feed.');
47  }
48 
49  try
50  {
51  // Skip ahead to the root node.
52  do
53  {
54  $reader->read();
55  }
56  while ($reader->nodeType !== XMLReader::ELEMENT);
57  }
58  catch (Exception $e)
59  {
60  throw new RuntimeException('Error reading feed.');
61  }
62 
63  // Setup the appopriate feed parser for the feed.
64  $parser = $this->_fetchFeedParser($reader->name, $reader);
65 
66  return $parser->parse();
67  }
68 
69  /**
70  * Method to register a JFeedParser class for a given root tag name.
71  *
72  * @param string $tagName The root tag name for which to register the parser class.
73  * @param string $className The JFeedParser class name to register for a root tag name.
74  * @param boolean $overwrite True to overwrite the parser class if one is already registered.
75  *
76  * @return JFeedFactory
77  *
78  * @since 12.3
79  * @throws InvalidArgumentException
80  */
81  public function registerParser($tagName, $className, $overwrite = false)
82  {
83  // Verify that the class exists.
84  if (!class_exists($className))
85  {
86  throw new InvalidArgumentException('The feed parser class ' . $className . ' does not exist.');
87  }
88 
89  // Validate that the tag name is valid.
90  if (!preg_match('/\A(?!XML)[a-z][\w0-9-]*/i', $tagName))
91  {
92  throw new InvalidArgumentException('The tag name ' . $tagName . ' is not valid.');
93  }
94 
95  // Register the given parser class for the tag name if nothing registered or the overwrite flag set.
96  if (empty($this->parsers[$tagName]) || (bool) $overwrite)
97  {
98  $this->parsers[(string) $tagName] = (string) $className;
99  }
100 
101  return $this;
102  }
103 
104  /**
105  * Method to return a new JFeedParser object based on the registered parsers and a given type.
106  *
107  * @param string $type The name of parser to return.
108  * @param XMLReader $reader The XMLReader instance for the feed.
109  *
110  * @return JFeedParser
111  *
112  * @since 12.3
113  * @throws LogicException
114  */
115  private function _fetchFeedParser($type, XMLReader $reader)
116  {
117  // Look for a registered parser for the feed type.
118  if (empty($this->parsers[$type]))
119  {
120  throw new LogicException('No registered feed parser for type ' . $type . '.');
121  }
122 
123  return new $this->parsers[$type]($reader);
124  }
125 }