annotate vendor/zendframework/zend-diactoros/src/PhpInputStream.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children c2387f117808
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@12 3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
Chris@12 4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
Chris@0 5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
Chris@0 6 */
Chris@0 7
Chris@0 8 namespace Zend\Diactoros;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Caching version of php://input
Chris@0 12 */
Chris@0 13 class PhpInputStream extends Stream
Chris@0 14 {
Chris@0 15 /**
Chris@0 16 * @var string
Chris@0 17 */
Chris@0 18 private $cache = '';
Chris@0 19
Chris@0 20 /**
Chris@0 21 * @var bool
Chris@0 22 */
Chris@0 23 private $reachedEof = false;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * @param string|resource $stream
Chris@0 27 */
Chris@0 28 public function __construct($stream = 'php://input')
Chris@0 29 {
Chris@0 30 parent::__construct($stream, 'r');
Chris@0 31 }
Chris@0 32
Chris@0 33 /**
Chris@0 34 * {@inheritdoc}
Chris@0 35 */
Chris@0 36 public function __toString()
Chris@0 37 {
Chris@0 38 if ($this->reachedEof) {
Chris@0 39 return $this->cache;
Chris@0 40 }
Chris@0 41
Chris@0 42 $this->getContents();
Chris@0 43 return $this->cache;
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * {@inheritdoc}
Chris@0 48 */
Chris@0 49 public function isWritable()
Chris@0 50 {
Chris@0 51 return false;
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * {@inheritdoc}
Chris@0 56 */
Chris@0 57 public function read($length)
Chris@0 58 {
Chris@0 59 $content = parent::read($length);
Chris@0 60 if (! $this->reachedEof) {
Chris@0 61 $this->cache .= $content;
Chris@0 62 }
Chris@0 63
Chris@0 64 if ($this->eof()) {
Chris@0 65 $this->reachedEof = true;
Chris@0 66 }
Chris@0 67
Chris@0 68 return $content;
Chris@0 69 }
Chris@0 70
Chris@0 71 /**
Chris@0 72 * {@inheritdoc}
Chris@0 73 */
Chris@0 74 public function getContents($maxLength = -1)
Chris@0 75 {
Chris@0 76 if ($this->reachedEof) {
Chris@0 77 return $this->cache;
Chris@0 78 }
Chris@0 79
Chris@0 80 $contents = stream_get_contents($this->resource, $maxLength);
Chris@0 81 $this->cache .= $contents;
Chris@0 82
Chris@0 83 if ($maxLength === -1 || $this->eof()) {
Chris@0 84 $this->reachedEof = true;
Chris@0 85 }
Chris@0 86
Chris@0 87 return $contents;
Chris@0 88 }
Chris@0 89 }