Skip to content

Commit

Permalink
Use robust, consistent Psr7\Utils::tryFopen where possible (#2876)
Browse files Browse the repository at this point in the history
Co-Authored-By: Mponos George <gmponos@gmail.com>

Co-authored-by: Mponos George <gmponos@gmail.com>
  • Loading branch information
GrahamCampbell and gmponos committed Mar 21, 2021
1 parent 6179dd0 commit f38085e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/psr7.rst
Expand Up @@ -424,7 +424,7 @@ and can optionally expose other custom data.
use GuzzleHttp\Psr7;
$resource = fopen('/path/to/file', 'r');
$resource = Psr7\Utils::tryFopen('/path/to/file', 'r');
$stream = Psr7\Utils::streamFor($resource);
echo $stream->getMetadata('uri');
// /path/to/file
Expand Down
10 changes: 7 additions & 3 deletions docs/quickstart.rst
Expand Up @@ -342,17 +342,19 @@ resource returned from ``fopen``, or an instance of a

.. code-block:: php
use GuzzleHttp\Psr7;
// Provide the body as a string.
$r = $client->request('POST', 'http://httpbin.org/post', [
'body' => 'raw data'
]);
// Provide an fopen resource.
$body = fopen('/path/to/file', 'r');
$body = Psr7\Utils::tryFopen('/path/to/file', 'r');
$r = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);
// Use the Utils::streamFor method to create a PSR-7 stream.
$body = \GuzzleHttp\Psr7\Utils::streamFor('hello!');
$body = Psr7\Utils::streamFor('hello!');
$r = $client->request('POST', 'http://httpbin.org/post', ['body' => $body]);
An easy way to upload JSON data and set the appropriate header is using the
Expand Down Expand Up @@ -406,6 +408,8 @@ associative arrays, where each associative array contains the following keys:

.. code-block:: php
use GuzzleHttp\Psr7;
$response = $client->request('POST', 'http://httpbin.org/post', [
'multipart' => [
[
Expand All @@ -414,7 +418,7 @@ associative arrays, where each associative array contains the following keys:
],
[
'name' => 'file_name',
'contents' => fopen('/path/to/file', 'r')
'contents' => Psr7\Utils::tryFopen('/path/to/file', 'r')
],
[
'name' => 'other_file',
Expand Down
14 changes: 8 additions & 6 deletions docs/request-options.rst
Expand Up @@ -203,7 +203,7 @@ This setting can be set to any of the following types:
.. code-block:: php
// You can send requests that use a stream resource as the body.
$resource = fopen('http://httpbin.org', 'r');
$resource = \GuzzleHttp\Psr7\Utils::tryFopen('http://httpbin.org', 'r');
$client->request('PUT', '/put', ['body' => $resource]);
- ``Psr\Http\Message\StreamInterface``
Expand Down Expand Up @@ -646,6 +646,8 @@ the following key value pairs:

.. code-block:: php
use GuzzleHttp\Psr7;
$client->request('POST', '/post', [
'multipart' => [
[
Expand All @@ -655,11 +657,11 @@ the following key value pairs:
],
[
'name' => 'baz',
'contents' => fopen('/path/to/file', 'r')
'contents' => Psr7\Utils::tryFopen('/path/to/file', 'r')
],
[
'name' => 'qux',
'contents' => fopen('/path/to/file', 'r'),
'contents' => Psr7\Utils::tryFopen('/path/to/file', 'r'),
'filename' => 'custom_filename.txt'
],
]
Expand Down Expand Up @@ -907,16 +909,16 @@ Pass a resource returned from ``fopen()`` to write the response to a PHP stream:

.. code-block:: php
$resource = fopen('/path/to/file', 'w');
$resource = \GuzzleHttp\Psr7\Utils::tryFopen('/path/to/file', 'w');
$client->request('GET', '/stream/20', ['sink' => $resource]);
Pass a ``Psr\Http\Message\StreamInterface`` object to stream the response
body to an open PSR-7 stream.

.. code-block:: php
$resource = fopen('/path/to/file', 'w');
$stream = GuzzleHttp\Psr7\Utils::streamFor($resource);
$resource = \GuzzleHttp\Psr7\Utils::tryFopen('/path/to/file', 'w');
$stream = \GuzzleHttp\Psr7\Utils::streamFor($resource);
$client->request('GET', '/stream/20', ['save_to' => $stream]);
.. note::
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/CurlFactory.php
Expand Up @@ -393,7 +393,7 @@ private function applyHandlerOptions(EasyHandle $easy, array &$conf): void

if (!isset($options['sink'])) {
// Use a default temp stream if no sink was set.
$options['sink'] = \fopen('php://temp', 'w+');
$options['sink'] = \GuzzleHttp\Psr7\Utils::tryFopen('php://temp', 'w+');
}
$sink = $options['sink'];
if (!\is_string($sink)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Handler/StreamHandler.php
Expand Up @@ -141,7 +141,7 @@ private function createSink(StreamInterface $stream, array $options): StreamInte
return $stream;
}

$sink = $options['sink'] ?? \fopen('php://temp', 'r+');
$sink = $options['sink'] ?? Psr7\Utils::tryFopen('php://temp', 'r+');

return \is_string($sink) ? new Psr7\LazyOpenStream($sink, 'w+') : Psr7\Utils::streamFor($sink);
}
Expand Down Expand Up @@ -304,7 +304,7 @@ static function () use ($context, $params) {

return $this->createResource(
function () use ($uri, &$http_response_header, $contextResource, $context, $options, $request) {
$resource = \fopen((string) $uri, 'r', false, $contextResource);
$resource = @\fopen((string) $uri, 'r', false, $contextResource);
$this->lastHeaders = $http_response_header;

if (false === $resource) {
Expand Down
7 changes: 1 addition & 6 deletions src/Utils.php
Expand Up @@ -71,12 +71,7 @@ public static function debugResource($value = null)
return \STDOUT;
}

$resource = \fopen('php://output', 'w');
if (false === $resource) {
throw new \RuntimeException('Can not open php output for writing to debug the resource.');
}

return $resource;
return \GuzzleHttp\Psr7\Utils::tryFopen('php://output', 'w');
}

/**
Expand Down

0 comments on commit f38085e

Please sign in to comment.