| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TJsonService and TJsonResponse class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gamil[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id: TJsonService.php 2574 2008-11-30 23:09:51Z carlgmathisen $
|
|
|
10 |
* @package System.Web.Services
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* TJsonService class provides to end-users javascript content response in
|
|
|
15 |
* JSON format.
|
|
|
16 |
*
|
|
|
17 |
* TJsonService manages a set of {@link TJsonResponse}, each
|
|
|
18 |
* representing specific response with javascript content.
|
|
|
19 |
* The service parameter, referring to the ID of the service, specifies
|
|
|
20 |
* which javascript content to be provided to end-users.
|
|
|
21 |
*
|
|
|
22 |
* To use TJsonService, configure it in application configuration as follows,
|
|
|
23 |
* <code>
|
|
|
24 |
* <service id="json" class="System.Web.Services.TJsonService">
|
|
|
25 |
* <json id="get_article" class="Path.To.JsonResponseClass1" .../>
|
|
|
26 |
* <json id="register_rating" class="Path.To.JsonResponseClass2" .../>
|
|
|
27 |
* </service>
|
|
|
28 |
* </code>
|
|
|
29 |
* where each JSON response is specified via a <json> element.
|
|
|
30 |
* Initial property values can be configured in a <json> element.
|
|
|
31 |
*
|
|
|
32 |
* To retrieve the JSON content provided by "get_article", use the URL
|
|
|
33 |
* <code>index.php?json=get_article</code>
|
|
|
34 |
*
|
|
|
35 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
36 |
* @version $Id: TJsonService.php 2574 2008-11-30 23:09:51Z carlgmathisen $
|
|
|
37 |
* @package System.Web.Services
|
|
|
38 |
* @since 3.1
|
|
|
39 |
*/
|
|
|
40 |
class TJsonService extends TService
|
|
|
41 |
{
|
|
|
42 |
/**
|
|
|
43 |
* @var array registered services
|
|
|
44 |
*/
|
|
|
45 |
private $_services=array();
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Initializes this module.
|
|
|
49 |
* This method is required by the IModule interface.
|
|
|
50 |
* @param TXmlElement configuration for this module, can be null
|
|
|
51 |
*/
|
|
|
52 |
public function init($xml)
|
|
|
53 |
{
|
|
|
54 |
$this->loadJsonServices($xml);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Load the service definitions.
|
|
|
59 |
* @param TXmlElement configuration for this module, can be null
|
|
|
60 |
*/
|
|
|
61 |
protected function loadJsonServices($xml)
|
|
|
62 |
{
|
|
|
63 |
foreach($xml->getElementsByTagName('json') as $config)
|
|
|
64 |
{
|
|
|
65 |
if(($id=$config->getAttribute('id'))!==null)
|
|
|
66 |
$this->_services[$id]=$config;
|
|
|
67 |
else
|
|
|
68 |
throw new TConfigurationException('jsonservice_id_required');
|
|
|
69 |
}
|
|
|
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->_services[$id]))
|
|
|
80 |
{
|
|
|
81 |
$serviceConfig=$this->_services[$id];
|
|
|
82 |
$properties=$serviceConfig->getAttributes();
|
|
|
83 |
if(($class=$properties->remove('class'))!==null)
|
|
|
84 |
{
|
|
|
85 |
$service=Prado::createComponent($class);
|
|
|
86 |
if($service instanceof TJsonResponse)
|
|
|
87 |
$this->createJsonResponse($service,$properties,$serviceConfig);
|
|
|
88 |
else
|
|
|
89 |
throw new TConfigurationException('jsonservice_response_type_invalid',$id);
|
|
|
90 |
}
|
|
|
91 |
else
|
|
|
92 |
throw new TConfigurationException('jsonservice_class_required',$id);
|
|
|
93 |
}
|
|
|
94 |
else
|
|
|
95 |
throw new THttpException(404,'jsonservice_provider_unknown',$id);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Renders content provided by TJsonResponse::getJsonContent() as
|
|
|
100 |
* javascript in JSON format.
|
|
|
101 |
*/
|
|
|
102 |
protected function createJsonResponse($service,$properties,$config)
|
|
|
103 |
{
|
|
|
104 |
// init service properties
|
|
|
105 |
foreach($properties as $name=>$value)
|
|
|
106 |
$service->setSubproperty($name,$value);
|
|
|
107 |
$service->init($config);
|
|
|
108 |
|
|
|
109 |
//send content if not null
|
|
|
110 |
if(($content=$service->getJsonContent())!==null)
|
|
|
111 |
{
|
|
|
112 |
$response = $this->getResponse();
|
|
|
113 |
$response->setContentType('text/javascript');
|
|
|
114 |
$response->setCharset('UTF-8');
|
|
|
115 |
$json = Prado::createComponent('System.Web.Javascripts.TJSON');
|
|
|
116 |
|
|
|
117 |
//send content
|
|
|
118 |
$response->write($json->encode($content));
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
/**
|
|
|
124 |
* TJsonResponse Class
|
|
|
125 |
*
|
|
|
126 |
* TJsonResponse is the base class for all JSON response provider classes.
|
|
|
127 |
*
|
|
|
128 |
* Derived classes must implement {@link getJsonContent()} to return
|
|
|
129 |
* an object or literals to be converted to JSON format. The response
|
|
|
130 |
* will be empty if the returned content is null.
|
|
|
131 |
*
|
|
|
132 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
133 |
* @version $Id: TJsonService.php 2574 2008-11-30 23:09:51Z carlgmathisen $
|
|
|
134 |
* @package System.Web.Services
|
|
|
135 |
* @since 3.1
|
|
|
136 |
*/
|
|
|
137 |
abstract class TJsonResponse extends TApplicationComponent
|
|
|
138 |
{
|
|
|
139 |
private $_id='';
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Initializes the feed.
|
|
|
143 |
* @param TXmlElement configurations specified in {@link TJsonService}.
|
|
|
144 |
*/
|
|
|
145 |
public function init($config)
|
|
|
146 |
{
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* @return string ID of this response
|
|
|
151 |
*/
|
|
|
152 |
public function getID()
|
|
|
153 |
{
|
|
|
154 |
return $this->_id;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* @param string ID of this response
|
|
|
159 |
*/
|
|
|
160 |
public function setID($value)
|
|
|
161 |
{
|
|
|
162 |
$this->_id=$value;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* @return object json response content, null to suppress output.
|
|
|
167 |
*/
|
|
|
168 |
abstract public function getJsonContent();
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
?>
|