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