File: //proc/self/cwd/nueva/modules/btecommercecopilot/vendor/clue/stream-filter/src/CallbackFilter.php
<?php
namespace Clue\StreamFilter;
/**
* @internal
* @see append()
* @see prepend()
*/
class CallbackFilter extends \php_user_filter
{
private $callback;
private $closed = true;
private $supportsClose = false;
/** @return bool */
#[\ReturnTypeWillChange]
public function onCreate()
{
$this->closed = false;
if (!\is_callable($this->params)) {
throw new \InvalidArgumentException('No valid callback parameter given to stream_filter_(append|prepend)');
}
$this->callback = $this->params;
$ref = new \ReflectionFunction($this->callback);
$this->supportsClose = ($ref->getNumberOfRequiredParameters() === 0);
return true;
}
#[\ReturnTypeWillChange]
public function onClose()
{
$this->closed = true;
if ($this->supportsClose) {
$this->supportsClose = false;
try {
\call_user_func($this->callback);
} catch (\Exception $ignored) {
}
}
$this->callback = null;
}
#[\ReturnTypeWillChange]
public function filter($in, $out, &$consumed, $closing)
{
$data = '';
while ($bucket = \stream_bucket_make_writeable($in)) {
$consumed += $bucket->datalen;
$data .= $bucket->data;
}
if ($this->closed) {
return \PSFS_FEED_ME;
}
if ($data !== '') {
try {
$data = \call_user_func($this->callback, $data);
} catch (\Exception $e) {
$this->onClose();
\trigger_error('Error invoking filter: ' . $e->getMessage(), \E_USER_WARNING);
return \PSFS_ERR_FATAL;
}
}
if ($closing) {
$this->closed = true;
if ($this->supportsClose) {
$this->supportsClose = false;
try {
$data .= \call_user_func($this->callback);
} catch (\Exception $e) {
\trigger_error('Error ending filter: ' . $e->getMessage(), \E_USER_WARNING);
return \PSFS_ERR_FATAL;
}
}
}
if ($data !== '') {
$bucket = @\stream_bucket_new($this->stream, $data);
if ($bucket !== false) {
\stream_bucket_append($out, $bucket);
}
}
return \PSFS_PASS_ON;
}
}