TPhpStream.php 3.0 KB

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