ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
GuzzleHttp\Psr7\MultipartStream Class Reference

Stream that when read returns bytes for a streaming multipart or multipart/form-data stream. More...

+ Inheritance diagram for GuzzleHttp\Psr7\MultipartStream:
+ Collaboration diagram for GuzzleHttp\Psr7\MultipartStream:

Public Member Functions

 __construct (array $elements=[], $boundary=null)
 
 getBoundary ()
 Get the boundary. More...
 
 isWritable ()
 Returns whether or not the stream is writable. More...
 
- Public Member Functions inherited from Psr\Http\Message\StreamInterface
 __toString ()
 Reads all data from the stream into a string, from the beginning to end. More...
 
 close ()
 Closes the stream and any underlying resources. More...
 
 detach ()
 Separates any underlying resources from the stream. More...
 
 getSize ()
 Get the size of the stream if known. More...
 
 tell ()
 Returns the current position of the file read/write pointer. More...
 
 eof ()
 Returns true if the stream is at the end of the stream. More...
 
 isSeekable ()
 Returns whether or not the stream is seekable. More...
 
 seek ($offset, $whence=SEEK_SET)
 Seek to a position in the stream. More...
 
 rewind ()
 Seek to the beginning of the stream. More...
 
 write ($string)
 Write data to the stream. More...
 
 isReadable ()
 Returns whether or not the stream is readable. More...
 
 read ($length)
 Read data from the stream. More...
 
 getContents ()
 Returns the remaining contents in a string. More...
 
 getMetadata ($key=null)
 Get stream metadata as an associative array or retrieve a specific key. More...
 

Protected Member Functions

 createStream (array $elements)
 Create the aggregate stream that will be used to upload the POST data. More...
 

Private Member Functions

 getHeaders (array $headers)
 Get the headers needed before transferring the content of a POST file. More...
 
 addElement (AppendStream $stream, array $element)
 
 createElement ($name, StreamInterface $stream, $filename, array $headers)
 
 getHeader (array $headers, $key)
 

Private Attributes

 $boundary
 

Detailed Description

Stream that when read returns bytes for a streaming multipart or multipart/form-data stream.

Definition at line 10 of file MultipartStream.php.

Constructor & Destructor Documentation

◆ __construct()

GuzzleHttp\Psr7\MultipartStream::__construct ( array  $elements = [],
  $boundary = null 
)
Parameters
array$elementsArray of associative arrays, each containing a required "name" key mapping to the form field, name, a required "contents" key mapping to a StreamInterface/resource/string, an optional "headers" associative array of custom headers, and an optional "filename" key mapping to a string to send as the filename in the part.
string$boundaryYou can optionally provide a specific boundary
Exceptions

Definition at line 28 of file MultipartStream.php.

References GuzzleHttp\Psr7\MultipartStream\$boundary, and GuzzleHttp\Psr7\MultipartStream\createStream().

29  {
30  $this->boundary = $boundary ?: sha1(uniqid('', true));
31  $this->stream = $this->createStream($elements);
32  }
createStream(array $elements)
Create the aggregate stream that will be used to upload the POST data.
+ Here is the call graph for this function:

Member Function Documentation

◆ addElement()

GuzzleHttp\Psr7\MultipartStream::addElement ( AppendStream  $stream,
array  $element 
)
private

Definition at line 79 of file MultipartStream.php.

References $key, GuzzleHttp\Psr7\AppendStream\addStream(), GuzzleHttp\Psr7\MultipartStream\createElement(), GuzzleHttp\Psr7\MultipartStream\getHeaders(), and GuzzleHttp\Psr7\stream_for().

Referenced by GuzzleHttp\Psr7\MultipartStream\createStream().

80  {
81  foreach (['contents', 'name'] as $key) {
82  if (!array_key_exists($key, $element)) {
83  throw new \InvalidArgumentException("A '{$key}' key is required");
84  }
85  }
86 
87  $element['contents'] = stream_for($element['contents']);
88 
89  if (empty($element['filename'])) {
90  $uri = $element['contents']->getMetadata('uri');
91  if (substr($uri, 0, 6) !== 'php://') {
92  $element['filename'] = $uri;
93  }
94  }
95 
96  list($body, $headers) = $this->createElement(
97  $element['name'],
98  $element['contents'],
99  isset($element['filename']) ? $element['filename'] : null,
100  isset($element['headers']) ? $element['headers'] : []
101  );
102 
103  $stream->addStream(stream_for($this->getHeaders($headers)));
104  $stream->addStream($body);
105  $stream->addStream(stream_for("\r\n"));
106  }
$stream
PHP stream implementation.
stream_for($resource='', array $options=[])
Create a new stream based on the input type.
Definition: functions.php:78
getHeaders(array $headers)
Get the headers needed before transferring the content of a POST file.
createElement($name, StreamInterface $stream, $filename, array $headers)
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createElement()

GuzzleHttp\Psr7\MultipartStream::createElement (   $name,
StreamInterface  $stream,
  $filename,
array  $headers 
)
private
Returns
array

Definition at line 111 of file MultipartStream.php.

References $filename, $name, GuzzleHttp\Psr7\$stream, $type, GuzzleHttp\Psr7\MultipartStream\getHeader(), Psr\Http\Message\StreamInterface\getSize(), and GuzzleHttp\Psr7\mimetype_from_filename().

Referenced by GuzzleHttp\Psr7\MultipartStream\addElement().

112  {
113  // Set a default content-disposition header if one was no provided
114  $disposition = $this->getHeader($headers, 'content-disposition');
115  if (!$disposition) {
116  $headers['Content-Disposition'] = ($filename === '0' || $filename)
117  ? sprintf('form-data; name="%s"; filename="%s"',
118  $name,
119  basename($filename))
120  : "form-data; name=\"{$name}\"";
121  }
122 
123  // Set a default content-length header if one was no provided
124  $length = $this->getHeader($headers, 'content-length');
125  if (!$length) {
126  if ($length = $stream->getSize()) {
127  $headers['Content-Length'] = (string) $length;
128  }
129  }
130 
131  // Set a default Content-Type if one was not supplied
132  $type = $this->getHeader($headers, 'content-type');
133  if (!$type && ($filename === '0' || $filename)) {
135  $headers['Content-Type'] = $type;
136  }
137  }
138 
139  return [$stream, $headers];
140  }
getHeader(array $headers, $key)
$type
mimetype_from_filename($filename)
Determines the mimetype of a file by looking at its extension.
Definition: functions.php:620
$stream
PHP stream implementation.
$filename
Definition: buildRTE.php:89
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createStream()

GuzzleHttp\Psr7\MultipartStream::createStream ( array  $elements)
protected

Create the aggregate stream that will be used to upload the POST data.

Definition at line 65 of file MultipartStream.php.

References GuzzleHttp\Psr7\$stream, GuzzleHttp\Psr7\MultipartStream\addElement(), and GuzzleHttp\Psr7\stream_for().

Referenced by GuzzleHttp\Psr7\MultipartStream\__construct().

66  {
67  $stream = new AppendStream();
68 
69  foreach ($elements as $element) {
70  $this->addElement($stream, $element);
71  }
72 
73  // Add the trailing boundary with CRLF
74  $stream->addStream(stream_for("--{$this->boundary}--\r\n"));
75 
76  return $stream;
77  }
$stream
PHP stream implementation.
stream_for($resource='', array $options=[])
Create a new stream based on the input type.
Definition: functions.php:78
addElement(AppendStream $stream, array $element)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getBoundary()

GuzzleHttp\Psr7\MultipartStream::getBoundary ( )

Get the boundary.

Returns
string

Definition at line 39 of file MultipartStream.php.

References GuzzleHttp\Psr7\MultipartStream\$boundary.

◆ getHeader()

GuzzleHttp\Psr7\MultipartStream::getHeader ( array  $headers,
  $key 
)
private

Definition at line 142 of file MultipartStream.php.

References $key.

Referenced by GuzzleHttp\Psr7\MultipartStream\createElement().

143  {
144  $lowercaseHeader = strtolower($key);
145  foreach ($headers as $k => $v) {
146  if (strtolower($k) === $lowercaseHeader) {
147  return $v;
148  }
149  }
150 
151  return null;
152  }
$key
Definition: croninfo.php:18
+ Here is the caller graph for this function:

◆ getHeaders()

GuzzleHttp\Psr7\MultipartStream::getHeaders ( array  $headers)
private

Get the headers needed before transferring the content of a POST file.

Definition at line 52 of file MultipartStream.php.

References $key.

Referenced by GuzzleHttp\Psr7\MultipartStream\addElement().

53  {
54  $str = '';
55  foreach ($headers as $key => $value) {
56  $str .= "{$key}: {$value}\r\n";
57  }
58 
59  return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
60  }
$key
Definition: croninfo.php:18
+ Here is the caller graph for this function:

◆ isWritable()

GuzzleHttp\Psr7\MultipartStream::isWritable ( )

Returns whether or not the stream is writable.

Returns
bool

Implements Psr\Http\Message\StreamInterface.

Definition at line 44 of file MultipartStream.php.

45  {
46  return false;
47  }

Field Documentation

◆ $boundary

GuzzleHttp\Psr7\MultipartStream::$boundary
private

The documentation for this class was generated from the following file: