Skip to content

Commit

Permalink
Manually implement stream_context_set_options in a php8.1-compatible way
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Dec 3, 2024
1 parent 822cdae commit 321a186
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion generator/config/specialCasesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@
'simplexml_import_dom',
'simplexml_load_file',
'simplexml_load_string',
'fgetcsv', // This function need to return false when iterating on an end of file.
// returns literal "true", which php8.1 doesn't support, so we implement
// this one manually and return "bool"
'stream_context_set_options',
// This function need to return false when iterating on an end of file.
'fgetcsv',
];
27 changes: 27 additions & 0 deletions lib/special_cases.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Safe\Exceptions\OpensslException;
use Safe\Exceptions\PcreException;
use Safe\Exceptions\SimplexmlException;
use Safe\Exceptions\StreamException;
use Safe\Exceptions\FilesystemException;

use const PREG_NO_ERROR;
Expand Down Expand Up @@ -440,3 +441,29 @@ function fgetcsv($stream, ?int $length = null, string $separator = ",", string $
}
return $safeResult;
}

/**
* Sets options on the specified context.
*
* @param resource $context The stream or context resource to apply the options to.
* @param array $options The options to set for context.
*
* options must be an associative
* array of associative arrays in the format
* $array['wrapper']['option'] = $value.
*
* Refer to context options and parameters
* for a listing of stream options.
* @return true Returns TRUE on success.
* @throws StreamException
*
*/
function stream_context_set_options($context, array $options): bool
{
error_clear_last();
$safeResult = \stream_context_set_options($context, $options);
if ($safeResult === false) {
throw StreamException::createFromPhpError();
}
return $safeResult;
}

0 comments on commit 321a186

Please sign in to comment.