| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 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 | |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 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 | */ |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 34 | class TPhpStream extends TTransport |
| 35 | { |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 36 | const MODE_R = 1; |
| 37 | const MODE_W = 2; |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 38 | |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 39 | private $inStream_ = null; |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 40 | |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 41 | private $outStream_ = null; |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 42 | |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 43 | private $read_ = false; |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 44 | |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 45 | private $write_ = false; |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 46 | |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 47 | public function __construct($mode) |
| 48 | { |
| 49 | $this->read_ = $mode & self::MODE_R; |
| 50 | $this->write_ = $mode & self::MODE_W; |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 51 | } |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 52 | |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 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 | } |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 67 | } |
| Roger Thomas | 6fb5923 | 2014-11-04 10:09:23 +0000 | [diff] [blame] | 68 | |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 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 | } |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 80 | |
| Robert Lu | b03ca01 | 2018-01-18 19:06:39 +0800 | [diff] [blame] | 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 | } |
| Roger Meier | 21c0a85 | 2012-09-05 19:47:14 +0000 | [diff] [blame] | 124 | } |