-
Notifications
You must be signed in to change notification settings - Fork 1
/
HessianFactory.php
executable file
·219 lines (199 loc) · 7.21 KB
/
HessianFactory.php
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
213
214
215
216
217
218
219
<?php
/*
* This file is part of the HessianPHP package.
* (c) 2004-2010 Manuel G�mez
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
include_once 'HessianInterfaces.php';
include_once 'HessianExceptions.php';
include_once 'HessianParsing.php';
include_once 'HessianOptions.php';
include_once 'HessianUtils.php';
include_once 'HessianCallbackHandler.php';
include_once 'HessianReferenceMap.php';
include_once 'HessianTypeMap.php';
include_once 'HessianStream.php';
define('HESSIAN_PHP_VERSION', '2.0');
/**
* Default implementation of an object factory
*/
class HessianObjectFactory implements IHessianObjectFactory{
var $options;
public function setOptions(HessianOptions $options){
$this->options = $options;
}
public function getObject($type){
if(!class_exists($type)) {
if(isset($this->options->strictType) && $this->options->strictType)
throw new Exception("Type $type cannot be found for object instantiation, check your type mappings");
$obj = new stdClass();
$obj->__type = $type;
return $obj;
}
return new $type();
}
}
/**
* Default Datetime adapter that works with the built-in Datetime class of PHP5
* @author vsayajin
*/
class HessianDatetimeAdapter{
public static function toObject($ts, $parser){
$date = date('c', $ts);
//$date = gmdate('c', $ts);
return new Datetime($date);
}
public static function writeTime($date, $writer){
$ts = $date->format('U');
$stream = $writer->writeDate($ts);
return new HessianStreamResult($stream);
}
}
/**
* Handles que creation of components for assembling Hessian clients and servers
* It contains the basic assembly configuration for these components.
* @author vsayajin
*
*/
class HessianFactory{
var $protocols = array();
var $transports = array();
static $cacheRules = array();
function __construct(){
$this->protocols = array(
'2'=>array($this,'loadVersion2'), '1'=>array($this,'loadVersion1')
);
$this->transports = array(
'CURL' => 'HessianCURLTransport',
'curl' => 'HessianCURLTransport',
'http' => 'HessianHttpStreamTransport'
);
}
/**
* Returns a specialized HessianParser object based on the options object
* @param HessianStream $stream input stream
* @param HessianOptions $options configuration options
*/
function getParser($stream, $options){
$version = $options->version;
if($options->detectVersion && $stream)
$version = $this->detectVersion($stream, $options);
$callback = $this->getConfig($version);
$parser = call_user_func_array($callback, array('parser', $stream, $options));
if($options->objectFactory instanceof IHessianObjectFactory){
$parser->objectFactory = $options->objectFactory;
} else {
$parser->objectFactory = new HessianObjectFactory();
}
return $parser;
}
/**
* Returns a specialized HessianWriter object based on the options object
* @param HessianStream $stream output stream
* @param HessianOptions $options configuration options
*/
function getWriter($stream, $options){
$version = $options->version;
if($options->detectVersion && $stream)
$version = $this->detectVersion($stream, $options);
$callback = $this->getConfig($version);
$writer = call_user_func_array($callback, array('writer', $stream, $options));
return $writer;
}
/**
* Creates a parsing helper object (rules resolver) that uses a protocol
* rule file to parse the incomin stream. It caches the rules for further
* use.
* @param Integer $version Protocol version
* @param array $config local component configuration
*/
public function getRulesResolver($version, $rulesPath=''){
if(isset(self::$cacheRules[$version]))
return self::$cacheRules[$version];
$resolver = new HessianRuleResolver($rulesPath);
self::$cacheRules[$version] = $resolver;
return $resolver;
}
/**
* Receives a stream and iterates over que registered protocol handlers
* in order to detect which version of Hessian is it
* @param HessianStream $stream
* @return integer Protocol version detected
*/
function detectVersion($stream, $options){
foreach($this->protocols as $version => $callback){
$res = call_user_func_array($callback, array('detect', $stream, $options));
if($res)
return $version;
}
throw new Exception("Cannot detect protocol version on stream");
}
function getConfig($version){
if(!isset($this->protocols[$version]))
throw new Exception("No configuration for version $version protocol");
return $this->protocols[$version];
}
function getTransport(HessianOptions $options){
$type = $options->transport;
if(is_object($type))
return $type;
if(!isset($this->transports[$type]))
throw new HessianException("The transport of type $type cannot be found");
$class = $this->transports[$type];
$trans = new $class();
$trans->testAvailable();
return $trans;
}
function loadVersion2($mode, $stream, $options){
if($mode == 'parser'){
include_once 'Hessian2/Hessian2ServiceParser.php';
$resolver = $this->getRulesResolver(2, 'Hessian2/hessian2rules.php');
$parser = new Hessian2ServiceParser($resolver, $stream, $options);
$filters['date'] = array('HessianDatetimeAdapter','toObject');
$filters = array_merge($filters, $options->parseFilters);
$parser->setFilters(new HessianCallbackHandler($filters));
return $parser;
}
if($mode == 'writer'){
include_once 'Hessian2/Hessian2ServiceWriter.php';
include_once 'Hessian2/Hessian2IteratorWriter.php';
$writer = new Hessian2ServiceWriter($options);
$filters['@DateTime'] = array('HessianDatetimeAdapter','writeTime');
$filters['@Iterator'] = array( new Hessian2IteratorWriter(), 'write');
$filters = array_merge($filters, $options->writeFilters);
$writer->setFilters(new HessianCallbackHandler($filters));
return $writer;
}
if($mode == 'detect'){
include_once 'Hessian2/Hessian2ServiceParser.php';
return Hessian2ServiceParser::detectVersion($stream);
}
}
function loadVersion1($mode, $stream, $options){
if($mode == 'parser'){
include_once 'Hessian1/Hessian1ServiceParser.php';
$resolver = $this->getRulesResolver(1, 'Hessian1/hessian1rules.php');
$parser = new Hessian1ServiceParser($resolver, $stream, $options);
$filters['date'] = array('HessianDatetimeAdapter','toObject');
$filters = array_merge($filters, $options->parseFilters);
$parser->setFilters(new HessianCallbackHandler($filters));
return $parser;
}
if($mode == 'writer'){
include_once 'Hessian1/Hessian1ServiceWriter.php';
include_once 'Hessian1/Hessian1IteratorWriter.php';
$writer = new Hessian1ServiceWriter($options);
$filters['@DateTime'] = array('HessianDatetimeAdapter','writeTime');
$filters['@Iterator'] = array( new Hessian1IteratorWriter(), 'write');
$filters = array_merge($filters, $options->writeFilters);
$writer->setFilters(new HessianCallbackHandler($filters));
return $writer;
}
if($mode == 'detect'){
include_once 'Hessian1/Hessian1ServiceParser.php';
return Hessian1ServiceParser::detectVersion($stream);
}
}
}