]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/php/lib/Transport/TPhpStream.php
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / php / lib / Transport / TPhpStream.php
1 <?php
2 /*
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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
18 * under the License.
19 *
20 * @package thrift.transport
21 */
22
23 namespace Thrift\Transport;
24
25 use Thrift\Exception\TException;
26 use Thrift\Factory\TStringFuncFactory;
27
28 /**
29 * Php stream transport. Reads to and writes from the php standard streams
30 * php://input and php://output
31 *
32 * @package thrift.transport
33 */
34 class TPhpStream extends TTransport
35 {
36 const MODE_R = 1;
37 const MODE_W = 2;
38
39 private $inStream_ = null;
40
41 private $outStream_ = null;
42
43 private $read_ = false;
44
45 private $write_ = false;
46
47 public function __construct($mode)
48 {
49 $this->read_ = $mode & self::MODE_R;
50 $this->write_ = $mode & self::MODE_W;
51 }
52
53 public function open()
54 {
55 if ($this->read_) {
56 $this->inStream_ = @fopen(self::inStreamName(), 'r');
57 if (!is_resource($this->inStream_)) {
58 throw new TException('TPhpStream: Could not open php://input');
59 }
60 }
61 if ($this->write_) {
62 $this->outStream_ = @fopen('php://output', 'w');
63 if (!is_resource($this->outStream_)) {
64 throw new TException('TPhpStream: Could not open php://output');
65 }
66 }
67 }
68
69 public function close()
70 {
71 if ($this->read_) {
72 @fclose($this->inStream_);
73 $this->inStream_ = null;
74 }
75 if ($this->write_) {
76 @fclose($this->outStream_);
77 $this->outStream_ = null;
78 }
79 }
80
81 public function isOpen()
82 {
83 return
84 (!$this->read_ || is_resource($this->inStream_)) &&
85 (!$this->write_ || is_resource($this->outStream_));
86 }
87
88 public function read($len)
89 {
90 $data = @fread($this->inStream_, $len);
91 if ($data === false || $data === '') {
92 throw new TException('TPhpStream: Could not read ' . $len . ' bytes');
93 }
94
95 return $data;
96 }
97
98 public function write($buf)
99 {
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'
105 );
106 }
107 $buf = TStringFuncFactory::create()->substr($buf, $got);
108 }
109 }
110
111 public function flush()
112 {
113 @fflush($this->outStream_);
114 }
115
116 private static function inStreamName()
117 {
118 if (php_sapi_name() == 'cli') {
119 return 'php://stdin';
120 }
121
122 return 'php://input';
123 }
124 }