| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 1 | <?php | 
| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | 
| Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 10 |  * | 
| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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. | 
| Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 19 |  * | 
 | 20 |  * @package thrift.transport | 
| Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 21 |  */ | 
 | 22 |  | 
| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 23 |  | 
| Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 24 | /** | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 25 |  * Php stream transport. Reads to and writes from the php standard streams | 
 | 26 |  * php://input and php://output | 
 | 27 |  * | 
 | 28 |  * @package thrift.transport | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 29 |  */ | 
 | 30 | class 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 Reiss | be648ee | 2009-03-26 06:14:45 +0000 | [diff] [blame] | 50 |       $this->inStream_ = @fopen(self::inStreamName(), 'r'); | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 51 |       if (!is_resource($this->inStream_)) { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 52 |         throw new TException('TPhpStream: Could not open php://input'); | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 53 |       } | 
 | 54 |     } | 
 | 55 |     if ($this->write_) { | 
 | 56 |       $this->outStream_ = @fopen('php://output', 'w'); | 
 | 57 |       if (!is_resource($this->outStream_)) { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 58 |         throw new TException('TPhpStream: Could not open php://output'); | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 59 |       } | 
 | 60 |     } | 
 | 61 |   } | 
| Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 62 |  | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 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); | 
| eletuchy | 260a5fa | 2008-02-13 22:41:03 +0000 | [diff] [blame] | 82 |     if ($data === FALSE || $data === '') { | 
| Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 83 |       throw new TException('TPhpStream: Could not read '.$len.' bytes'); | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 84 |     } | 
 | 85 |     return $data; | 
 | 86 |   } | 
 | 87 |  | 
 | 88 |   public function write($buf) { | 
| Jake Farrell | b03a59c | 2011-11-29 16:45:51 +0000 | [diff] [blame^] | 89 |     while (TStringFuncFactory::create()->strlen($buf) > 0) { | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 90 |       $got = @fwrite($this->outStream_, $buf); | 
 | 91 |       if ($got === 0 || $got === FALSE) { | 
| Jake Farrell | b03a59c | 2011-11-29 16:45:51 +0000 | [diff] [blame^] | 92 |         throw new TException('TPhpStream: Could not write '.TStringFuncFactory::create()->strlen($buf).' bytes'); | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 93 |       } | 
| Jake Farrell | b03a59c | 2011-11-29 16:45:51 +0000 | [diff] [blame^] | 94 |       $buf = TStringFuncFactory::create()->substr($buf, $got); | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 95 |     } | 
 | 96 |   } | 
 | 97 |  | 
 | 98 |   public function flush() { | 
 | 99 |     @fflush($this->outStream_); | 
 | 100 |   } | 
 | 101 |  | 
| David Reiss | be648ee | 2009-03-26 06:14:45 +0000 | [diff] [blame] | 102 |   private static function inStreamName() { | 
 | 103 |     if (php_sapi_name() == 'cli') { | 
 | 104 |       return 'php://stdin'; | 
 | 105 |     } | 
 | 106 |     return 'php://input'; | 
 | 107 |   } | 
 | 108 |  | 
| Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 109 | } |