3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
20 * @package thrift.transport
23 namespace Thrift\Transport
;
25 use Thrift\Exception\TException
;
26 use Thrift\Factory\TStringFuncFactory
;
29 * Php stream transport. Reads to and writes from the php standard streams
30 * php://input and php://output
32 * @package thrift.transport
34 class TPhpStream
extends TTransport
39 private $inStream_ = null;
41 private $outStream_ = null;
43 private $read_ = false;
45 private $write_ = false;
47 public function __construct($mode)
49 $this->read_
= $mode & self
::MODE_R
;
50 $this->write_
= $mode & self
::MODE_W
;
53 public function open()
56 $this->inStream_
= @fopen
(self
::inStreamName(), 'r');
57 if (!is_resource($this->inStream_
)) {
58 throw new TException('TPhpStream: Could not open php://input');
62 $this->outStream_
= @fopen
('php://output', 'w');
63 if (!is_resource($this->outStream_
)) {
64 throw new TException('TPhpStream: Could not open php://output');
69 public function close()
72 @fclose
($this->inStream_
);
73 $this->inStream_
= null;
76 @fclose
($this->outStream_
);
77 $this->outStream_
= null;
81 public function isOpen()
84 (!$this->read_ ||
is_resource($this->inStream_
)) &&
85 (!$this->write_ ||
is_resource($this->outStream_
));
88 public function read($len)
90 $data = @fread
($this->inStream_
, $len);
91 if ($data === false ||
$data === '') {
92 throw new TException('TPhpStream: Could not read ' . $len . ' bytes');
98 public function write($buf)
100 while (TStringFuncFactory
::create()->strlen($buf) > 0) {
101 $got = @fwrite
($this->outStream_
, $buf);
102 if ($got === 0 ||
$got === false) {
103 throw new TException(
104 'TPhpStream: Could not write ' . TStringFuncFactory
::create()->strlen($buf) . ' bytes'
107 $buf = TStringFuncFactory
::create()->substr($buf, $got);
111 public function flush()
113 @fflush
($this->outStream_
);
116 private static function inStreamName()
118 if (php_sapi_name() == 'cli') {
119 return 'php://stdin';
122 return 'php://input';