-
Notifications
You must be signed in to change notification settings - Fork 0
/
Response.php
79 lines (69 loc) · 1.97 KB
/
Response.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
<?php
namespace RangelReale\nithrift;
use Yii;
/**
* Response of a Thrift request.
* @author Rangel Reale <rangelspam@gmail.com>
*/
class Response extends \yii\web\Response
{
/**
* @var string set response format to Thrift format
*/
public $format = ResponseFormatter::FORMAT_THRIFT;
/**
* @var object the Thrift processor to use
*/
protected $_processor;
/**
* Constructs the object.
* @param object $processor the Thrift processor to use
*/
public function __construct($processor)
{
parent::__construct();
$this->_processor = $processor;
}
/**
* Creates the Thrift transport.
* By default use the Thrift component to create.
* @return \Thrift\Transport\TTransport the transport to use
*/
protected function createTransport()
{
return Yii::$app->get('thrift')->createTransport();
}
/**
* Creates the Thrift protocol.
* By default use the Thrift component to create.
* @param \Thrift\Transport\TTransport $transport
* @return \Thrift\Protocol\TProcotol the protocol to use
*/
protected function createProtocol($transport)
{
return Yii::$app->get('thrift')->createProtocol($transport);
}
/**
* Output the processor using the transport and protocol.
*/
protected function sendContent()
{
$transport = $this->createTransport();
$protocol = $this->createProtocol($transport);
$transport->open();
if (!is_null($this->_processor)) {
$this->_processor->process($protocol, $protocol);
}
$transport->close();
}
/**
* Adds the Thrift formatter to the default formatters.
* @return array the formatters that are supported by default
*/
protected function defaultFormatters()
{
return array_merge(parent::defaultFormatters(), [
'thrift' => 'RangelReale\nithrift\ResponseFormatter',
]);
}
}