Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /** |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 4 | * Copyright (c) 2006- Facebook |
| 5 | * Distributed under the Thrift Software License |
| 6 | * |
| 7 | * See accompanying file LICENSE or visit the Thrift site at: |
| 8 | * http://developers.facebook.com/thrift/ |
| 9 | * |
| 10 | * @package thrift.transport |
Mark Slee | 4902c05 | 2007-03-01 00:31:30 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /** |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 14 | * Php stream transport. Reads to and writes from the php standard streams |
| 15 | * php://input and php://output |
| 16 | * |
| 17 | * @package thrift.transport |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 18 | */ |
| 19 | class TPhpStream extends TTransport { |
| 20 | |
| 21 | const MODE_R = 1; |
| 22 | const MODE_W = 2; |
| 23 | |
| 24 | private $inStream_ = null; |
| 25 | |
| 26 | private $outStream_ = null; |
| 27 | |
| 28 | private $read_ = false; |
| 29 | |
| 30 | private $write_ = false; |
| 31 | |
| 32 | public function __construct($mode) { |
| 33 | $this->read_ = $mode & self::MODE_R; |
| 34 | $this->write_ = $mode & self::MODE_W; |
| 35 | } |
| 36 | |
| 37 | public function open() { |
| 38 | if ($this->read_) { |
David Reiss | be648ee | 2009-03-26 06:14:45 +0000 | [diff] [blame^] | 39 | $this->inStream_ = @fopen(self::inStreamName(), 'r'); |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 40 | if (!is_resource($this->inStream_)) { |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 41 | throw new TException('TPhpStream: Could not open php://input'); |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | if ($this->write_) { |
| 45 | $this->outStream_ = @fopen('php://output', 'w'); |
| 46 | if (!is_resource($this->outStream_)) { |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 47 | throw new TException('TPhpStream: Could not open php://output'); |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | } |
Mark Slee | 0cdc6c8 | 2007-11-13 10:19:08 +0000 | [diff] [blame] | 51 | |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 52 | public function close() { |
| 53 | if ($this->read_) { |
| 54 | @fclose($this->inStream_); |
| 55 | $this->inStream_ = null; |
| 56 | } |
| 57 | if ($this->write_) { |
| 58 | @fclose($this->outStream_); |
| 59 | $this->outStream_ = null; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | public function isOpen() { |
| 64 | return |
| 65 | (!$this->read_ || is_resource($this->inStream_)) && |
| 66 | (!$this->write_ || is_resource($this->outStream_)); |
| 67 | } |
| 68 | |
| 69 | public function read($len) { |
| 70 | $data = @fread($this->inStream_, $len); |
eletuchy | 260a5fa | 2008-02-13 22:41:03 +0000 | [diff] [blame] | 71 | if ($data === FALSE || $data === '') { |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 72 | throw new TException('TPhpStream: Could not read '.$len.' bytes'); |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 73 | } |
| 74 | return $data; |
| 75 | } |
| 76 | |
| 77 | public function write($buf) { |
Mark Slee | d395d57 | 2007-02-27 01:16:55 +0000 | [diff] [blame] | 78 | while (strlen($buf) > 0) { |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 79 | $got = @fwrite($this->outStream_, $buf); |
| 80 | if ($got === 0 || $got === FALSE) { |
Mark Slee | 7679196 | 2007-03-14 02:47:35 +0000 | [diff] [blame] | 81 | throw new TException('TPhpStream: Could not write '.strlen($buf).' bytes'); |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 82 | } |
| 83 | $buf = substr($buf, $got); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | public function flush() { |
| 88 | @fflush($this->outStream_); |
| 89 | } |
| 90 | |
David Reiss | be648ee | 2009-03-26 06:14:45 +0000 | [diff] [blame^] | 91 | private static function inStreamName() { |
| 92 | if (php_sapi_name() == 'cli') { |
| 93 | return 'php://stdin'; |
| 94 | } |
| 95 | return 'php://input'; |
| 96 | } |
| 97 | |
Mark Slee | 99e2b26 | 2006-10-10 01:42:29 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | ?> |