blob: 3a1c80b8f776879483d8656b9455afe558934120 [file] [log] [blame]
Mark Slee99e2b262006-10-10 01:42:29 +00001<?php
David Reissea2cba82009-03-30 21:35:00 +00002/*
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
Mark Slee4902c052007-03-01 00:31:30 +000010 *
David Reissea2cba82009-03-30 21:35:00 +000011 * 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.
Mark Slee4902c052007-03-01 00:31:30 +000019 *
20 * @package thrift.transport
Mark Slee4902c052007-03-01 00:31:30 +000021 */
22
David Reissea2cba82009-03-30 21:35:00 +000023
Mark Slee4902c052007-03-01 00:31:30 +000024/**
Mark Slee99e2b262006-10-10 01:42:29 +000025 * Php stream transport. Reads to and writes from the php standard streams
26 * php://input and php://output
27 *
28 * @package thrift.transport
Mark Slee99e2b262006-10-10 01:42:29 +000029 */
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_) {
David Reissbe648ee2009-03-26 06:14:45 +000050 $this->inStream_ = @fopen(self::inStreamName(), 'r');
Mark Slee99e2b262006-10-10 01:42:29 +000051 if (!is_resource($this->inStream_)) {
Mark Slee76791962007-03-14 02:47:35 +000052 throw new TException('TPhpStream: Could not open php://input');
Mark Slee99e2b262006-10-10 01:42:29 +000053 }
54 }
55 if ($this->write_) {
56 $this->outStream_ = @fopen('php://output', 'w');
57 if (!is_resource($this->outStream_)) {
Mark Slee76791962007-03-14 02:47:35 +000058 throw new TException('TPhpStream: Could not open php://output');
Mark Slee99e2b262006-10-10 01:42:29 +000059 }
60 }
61 }
Mark Slee0cdc6c82007-11-13 10:19:08 +000062
Mark Slee99e2b262006-10-10 01:42:29 +000063 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);
eletuchy260a5fa2008-02-13 22:41:03 +000082 if ($data === FALSE || $data === '') {
Mark Slee76791962007-03-14 02:47:35 +000083 throw new TException('TPhpStream: Could not read '.$len.' bytes');
Mark Slee99e2b262006-10-10 01:42:29 +000084 }
85 return $data;
86 }
87
88 public function write($buf) {
Mark Sleed395d572007-02-27 01:16:55 +000089 while (strlen($buf) > 0) {
Mark Slee99e2b262006-10-10 01:42:29 +000090 $got = @fwrite($this->outStream_, $buf);
91 if ($got === 0 || $got === FALSE) {
Mark Slee76791962007-03-14 02:47:35 +000092 throw new TException('TPhpStream: Could not write '.strlen($buf).' bytes');
Mark Slee99e2b262006-10-10 01:42:29 +000093 }
94 $buf = substr($buf, $got);
95 }
96 }
97
98 public function flush() {
99 @fflush($this->outStream_);
100 }
101
David Reissbe648ee2009-03-26 06:14:45 +0000102 private static function inStreamName() {
103 if (php_sapi_name() == 'cli') {
104 return 'php://stdin';
105 }
106 return 'php://input';
107 }
108
Mark Slee99e2b262006-10-10 01:42:29 +0000109}
110
111?>