| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TFeedService and TFeed class file
|
|
|
4 |
*
|
|
|
5 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
6 |
* @author Knut Urdalen <knut.urdalen@gmail.com>
|
|
|
7 |
* @link http://www.pradosoft.com
|
|
|
8 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
9 |
* @license http://www.pradosoft.com/license/
|
|
|
10 |
* @version $Id$
|
|
|
11 |
* @package System.Web.Services
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* TFeedService class
|
|
|
16 |
*
|
|
|
17 |
* TFeedService provides to end-users feed content.
|
|
|
18 |
*
|
|
|
19 |
* TFeedService manages a set of feeds. The service parameter, referring
|
|
|
20 |
* to the ID of the feed, specifies which feed content to be provided to end-users.
|
|
|
21 |
*
|
|
|
22 |
* To use TFeedService, configure it in application configuration as follows,
|
|
|
23 |
* <code>
|
|
|
24 |
* <service id="feed" class="System.Web.Services.TFeedService">
|
|
|
25 |
* <feed id="ch1" class="Path.To.FeedClass1" .../>
|
|
|
26 |
* <feed id="ch2" class="Path.To.FeedClass2" .../>
|
|
|
27 |
* <feed id="ch3" class="Path.To.FeedClass3" .../>
|
|
|
28 |
* </service>
|
|
|
29 |
* </code>
|
|
|
30 |
* where each <feed> element specifies a feed identified by its "id" value (case-sensitive).
|
|
|
31 |
* The class attribute indicates which PHP class will provide the actual feed
|
|
|
32 |
* content. Note, the class must implement {@link IFeedContentProvider} interface.
|
|
|
33 |
* Other initial properties for the feed class may also be specified in the
|
|
|
34 |
* corresponding <feed> element.
|
|
|
35 |
*
|
|
|
36 |
* To retrieve the feed content identified by "ch2", use the URL
|
|
|
37 |
* <code>/path/to/index.php?feed=ch2</code>
|
|
|
38 |
*
|
|
|
39 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
40 |
* @author Knut Urdalen <knut.urdalen@gmail.com>
|
|
|
41 |
* @package System.Web.Services
|
|
|
42 |
* @since 3.1
|
|
|
43 |
*/
|
|
|
44 |
class TFeedService extends TService
|
|
|
45 |
{
|
|
|
46 |
private $_feeds=array();
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Initializes this module.
|
|
|
50 |
* This method is required by the IModule interface.
|
|
|
51 |
* @param TXmlElement configuration for this module, can be null
|
|
|
52 |
*/
|
|
|
53 |
public function init($config)
|
|
|
54 |
{
|
|
|
55 |
foreach($config->getElementsByTagName('feed') as $feed)
|
|
|
56 |
{
|
|
|
57 |
if(($id=$feed->getAttributes()->remove('id'))!==null)
|
|
|
58 |
$this->_feeds[$id]=$feed;
|
|
|
59 |
else
|
|
|
60 |
throw new TConfigurationException('feedservice_id_required');
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* @return string the requested feed path
|
|
|
66 |
*/
|
|
|
67 |
protected function determineRequestedFeedPath()
|
|
|
68 |
{
|
|
|
69 |
return $this->getRequest()->getServiceParameter();
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Runs the service.
|
|
|
74 |
* This method is invoked by application automatically.
|
|
|
75 |
*/
|
|
|
76 |
public function run()
|
|
|
77 |
{
|
|
|
78 |
$id=$this->getRequest()->getServiceParameter();
|
|
|
79 |
if(isset($this->_feeds[$id]))
|
|
|
80 |
{
|
|
|
81 |
$feedConfig=$this->_feeds[$id];
|
|
|
82 |
$properties=$feedConfig->getAttributes();
|
|
|
83 |
if(($class=$properties->remove('class'))!==null)
|
|
|
84 |
{
|
|
|
85 |
$feed=Prado::createComponent($class);
|
|
|
86 |
if($feed instanceof IFeedContentProvider)
|
|
|
87 |
{
|
|
|
88 |
// init feed properties
|
|
|
89 |
foreach($properties as $name=>$value)
|
|
|
90 |
$feed->setSubproperty($name,$value);
|
|
|
91 |
$feed->init($feedConfig);
|
|
|
92 |
|
|
|
93 |
$content=$feed->getFeedContent();
|
|
|
94 |
//$this->getResponse()->setContentType('application/rss+xml');
|
|
|
95 |
$this->getResponse()->setContentType($feed->getContentType());
|
|
|
96 |
$this->getResponse()->write($content);
|
|
|
97 |
}
|
|
|
98 |
else
|
|
|
99 |
throw new TConfigurationException('feedservice_feedtype_invalid',$id);
|
|
|
100 |
}
|
|
|
101 |
else
|
|
|
102 |
throw new TConfigurationException('feedservice_class_required',$id);
|
|
|
103 |
}
|
|
|
104 |
else
|
|
|
105 |
throw new THttpException(404,'feedservice_feed_unknown',$id);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* IFeedContentProvider interface.
|
|
|
111 |
*
|
|
|
112 |
* IFeedContentProvider interface must be implemented by a feed class who
|
|
|
113 |
* provides feed content.
|
|
|
114 |
*
|
|
|
115 |
* @author Qiang Xue <qiang.xue@gmail.com>
|
|
|
116 |
* @author Knut Urdalen <knut.urdalen@gmail.com>
|
|
|
117 |
* @package System.Web.Services
|
|
|
118 |
* @since 3.1
|
|
|
119 |
*/
|
|
|
120 |
interface IFeedContentProvider
|
|
|
121 |
{
|
|
|
122 |
/**
|
|
|
123 |
* Initializes the feed content provider.
|
|
|
124 |
* This method is invoked (before {@link getFeedContent})
|
|
|
125 |
* when the feed provider is requested by a user.
|
|
|
126 |
* @param TXmlElement configurations specified within the <feed> element
|
|
|
127 |
* corresponding to this feed provider when configuring {@link TFeedService}.
|
|
|
128 |
*/
|
|
|
129 |
public function init($config);
|
|
|
130 |
/**
|
|
|
131 |
* @return string feed content in proper XML format
|
|
|
132 |
*/
|
|
|
133 |
public function getFeedContent();
|
|
|
134 |
/**
|
|
|
135 |
* Sets the content type of the feed content to be sent.
|
|
|
136 |
* Some examples are:
|
|
|
137 |
* RSS 1.0 feed: application/rdf+xml
|
|
|
138 |
* RSS 2.0 feed: application/rss+xml or application/xml or text/xml
|
|
|
139 |
* ATOM feed: application/atom+xml
|
|
|
140 |
* @return string the content type for the feed content.
|
|
|
141 |
* @since 3.1.1
|
|
|
142 |
*/
|
|
|
143 |
public function getContentType();
|
|
|
144 |
}
|
|
|
145 |
|