blob: 9b8de8281490c2ff8d78b379249fe8114789cbcb [file] [log] [blame]
Mark Slee99e2b262006-10-10 01:42:29 +00001<?php
2
3/**
Mark Slee4902c052007-03-01 00:31:30 +00004 * Copyright (c) 2006- Facebook
5 * Distributed under the Thrift Software License
6 *
7 * See accompanying file LICENSE or visit the Thrift site at:
8 * http://developers.facebook.com/thrift/
9 *
10 * @package thrift.transport
Mark Slee4902c052007-03-01 00:31:30 +000011 */
12
13/**
Mark Slee99e2b262006-10-10 01:42:29 +000014 * Php stream transport. Reads to and writes from the php standard streams
15 * php://input and php://output
16 *
17 * @package thrift.transport
Mark Slee99e2b262006-10-10 01:42:29 +000018 */
19class TPhpStream extends TTransport {
20
21 const MODE_R = 1;
22 const MODE_W = 2;
23
24 private $inStream_ = null;
25
26 private $outStream_ = null;
27
28 private $read_ = false;
29
30 private $write_ = false;
31
32 public function __construct($mode) {
33 $this->read_ = $mode & self::MODE_R;
34 $this->write_ = $mode & self::MODE_W;
35 }
36
37 public function open() {
38 if ($this->read_) {
David Reissbe648ee2009-03-26 06:14:45 +000039 $this->inStream_ = @fopen(self::inStreamName(), 'r');
Mark Slee99e2b262006-10-10 01:42:29 +000040 if (!is_resource($this->inStream_)) {
Mark Slee76791962007-03-14 02:47:35 +000041 throw new TException('TPhpStream: Could not open php://input');
Mark Slee99e2b262006-10-10 01:42:29 +000042 }
43 }
44 if ($this->write_) {
45 $this->outStream_ = @fopen('php://output', 'w');
46 if (!is_resource($this->outStream_)) {
Mark Slee76791962007-03-14 02:47:35 +000047 throw new TException('TPhpStream: Could not open php://output');
Mark Slee99e2b262006-10-10 01:42:29 +000048 }
49 }
50 }
Mark Slee0cdc6c82007-11-13 10:19:08 +000051
Mark Slee99e2b262006-10-10 01:42:29 +000052 public function close() {
53 if ($this->read_) {
54 @fclose($this->inStream_);
55 $this->inStream_ = null;
56 }
57 if ($this->write_) {
58 @fclose($this->outStream_);
59 $this->outStream_ = null;
60 }
61 }
62
63 public function isOpen() {
64 return
65 (!$this->read_ || is_resource($this->inStream_)) &&
66 (!$this->write_ || is_resource($this->outStream_));
67 }
68
69 public function read($len) {
70 $data = @fread($this->inStream_, $len);
eletuchy260a5fa2008-02-13 22:41:03 +000071 if ($data === FALSE || $data === '') {
Mark Slee76791962007-03-14 02:47:35 +000072 throw new TException('TPhpStream: Could not read '.$len.' bytes');
Mark Slee99e2b262006-10-10 01:42:29 +000073 }
74 return $data;
75 }
76
77 public function write($buf) {
Mark Sleed395d572007-02-27 01:16:55 +000078 while (strlen($buf) > 0) {
Mark Slee99e2b262006-10-10 01:42:29 +000079 $got = @fwrite($this->outStream_, $buf);
80 if ($got === 0 || $got === FALSE) {
Mark Slee76791962007-03-14 02:47:35 +000081 throw new TException('TPhpStream: Could not write '.strlen($buf).' bytes');
Mark Slee99e2b262006-10-10 01:42:29 +000082 }
83 $buf = substr($buf, $got);
84 }
85 }
86
87 public function flush() {
88 @fflush($this->outStream_);
89 }
90
David Reissbe648ee2009-03-26 06:14:45 +000091 private static function inStreamName() {
92 if (php_sapi_name() == 'cli') {
93 return 'php://stdin';
94 }
95 return 'php://input';
96 }
97
Mark Slee99e2b262006-10-10 01:42:29 +000098}
99
100?>