blob: 3a1c80b8f776879483d8656b9455afe558934120 [file] [log] [blame]
Gavin McDonald0b75e1a2010-10-28 02:12:01 +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
23
24/**
25 * Php stream transport. Reads to and writes from the php standard streams
26 * php://input and php://output
27 *
28 * @package thrift.transport
29 */
30class TPhpStream extends TTransport {
31
32 const MODE_R = 1;
33 const MODE_W = 2;
34
35 private $inStream_ = null;
36
37 private $outStream_ = null;
38
39 private $read_ = false;
40
41 private $write_ = false;
42
43 public function __construct($mode) {
44 $this->read_ = $mode & self::MODE_R;
45 $this->write_ = $mode & self::MODE_W;
46 }
47
48 public function open() {
49 if ($this->read_) {
50 $this->inStream_ = @fopen(self::inStreamName(), 'r');
51 if (!is_resource($this->inStream_)) {
52 throw new TException('TPhpStream: Could not open php://input');
53 }
54 }
55 if ($this->write_) {
56 $this->outStream_ = @fopen('php://output', 'w');
57 if (!is_resource($this->outStream_)) {
58 throw new TException('TPhpStream: Could not open php://output');
59 }
60 }
61 }
62
63 public function close() {
64 if ($this->read_) {
65 @fclose($this->inStream_);
66 $this->inStream_ = null;
67 }
68 if ($this->write_) {
69 @fclose($this->outStream_);
70 $this->outStream_ = null;
71 }
72 }
73
74 public function isOpen() {
75 return
76 (!$this->read_ || is_resource($this->inStream_)) &&
77 (!$this->write_ || is_resource($this->outStream_));
78 }
79
80 public function read($len) {
81 $data = @fread($this->inStream_, $len);
82 if ($data === FALSE || $data === '') {
83 throw new TException('TPhpStream: Could not read '.$len.' bytes');
84 }
85 return $data;
86 }
87
88 public function write($buf) {
89 while (strlen($buf) > 0) {
90 $got = @fwrite($this->outStream_, $buf);
91 if ($got === 0 || $got === FALSE) {
92 throw new TException('TPhpStream: Could not write '.strlen($buf).' bytes');
93 }
94 $buf = substr($buf, $got);
95 }
96 }
97
98 public function flush() {
99 @fflush($this->outStream_);
100 }
101
102 private static function inStreamName() {
103 if (php_sapi_name() == 'cli') {
104 return 'php://stdin';
105 }
106 return 'php://input';
107 }
108
109}
110
111?>