blob: 42823ff33722470c4b076a160576f31ee67cf8ae [file] [log] [blame]
Roger Meier21c0a852012-09-05 19:47:14 +00001<?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
23namespace Thrift\Transport;
24
Roger Meier21c0a852012-09-05 19:47:14 +000025use Thrift\Exception\TException;
26use 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 */
Roger Thomas6fb59232014-11-04 10:09:23 +000034class TPhpStream extends TTransport
35{
Robert Lub03ca012018-01-18 19:06:39 +080036 const MODE_R = 1;
37 const MODE_W = 2;
Roger Meier21c0a852012-09-05 19:47:14 +000038
Robert Lub03ca012018-01-18 19:06:39 +080039 private $inStream_ = null;
Roger Meier21c0a852012-09-05 19:47:14 +000040
Robert Lub03ca012018-01-18 19:06:39 +080041 private $outStream_ = null;
Roger Meier21c0a852012-09-05 19:47:14 +000042
Robert Lub03ca012018-01-18 19:06:39 +080043 private $read_ = false;
Roger Meier21c0a852012-09-05 19:47:14 +000044
Robert Lub03ca012018-01-18 19:06:39 +080045 private $write_ = false;
Roger Meier21c0a852012-09-05 19:47:14 +000046
Robert Lub03ca012018-01-18 19:06:39 +080047 public function __construct($mode)
48 {
49 $this->read_ = $mode & self::MODE_R;
50 $this->write_ = $mode & self::MODE_W;
Roger Meier21c0a852012-09-05 19:47:14 +000051 }
Roger Thomas6fb59232014-11-04 10:09:23 +000052
Robert Lub03ca012018-01-18 19:06:39 +080053 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 }
Roger Meier21c0a852012-09-05 19:47:14 +000067 }
Roger Thomas6fb59232014-11-04 10:09:23 +000068
Robert Lub03ca012018-01-18 19:06:39 +080069 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 }
Roger Meier21c0a852012-09-05 19:47:14 +000080
Robert Lub03ca012018-01-18 19:06:39 +080081 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 }
Roger Meier21c0a852012-09-05 19:47:14 +0000124}