Skip to content

Instantly share code, notes, and snippets.

@hakre
Last active March 4, 2022 17:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hakre/8ba2c0d49b7baf062acd to your computer and use it in GitHub Desktop.
Save hakre/8ba2c0d49b7baf062acd to your computer and use it in GitHub Desktop.
StreamNotifyPrinter - Print out PHP Stream Notifications to target channel (default: STDERR).
<?php
/**
* StreamNotifyPrinter
*
* @author hakre <http://hakre.wordpress.com>
* @version 0.0.2
* @example http://stackoverflow.com/a/24711469/367456
* @link https://gist.github.com/hakre/8ba2c0d49b7baf062acd
*/
/**
* Class StreamNotifyPrinter
*
* @author hakre <http://hakre.wordpress.com>
* @since 0.0.1
*/
class StreamNotifyPrinter
{
private $constants = [
'STREAM_NOTIFY_RESOLVE',
'STREAM_NOTIFY_AUTH_REQUIRED',
'STREAM_NOTIFY_COMPLETED',
'STREAM_NOTIFY_FAILURE',
'STREAM_NOTIFY_AUTH_RESULT',
'STREAM_NOTIFY_REDIRECTED',
'STREAM_NOTIFY_CONNECT',
'STREAM_NOTIFY_FILE_SIZE_IS',
'STREAM_NOTIFY_MIME_TYPE_IS',
'STREAM_NOTIFY_PROGRESS',
];
private $startTime;
/**
* @var resource stream handle of output channel
*/
private $channel;
/**
* @param resource $channel (optional) file-handle for output, default is STDERR
*/
public function __construct($channel = NULL) {
if ($channel === NULL) {
$channel = STDERR;
}
$this->channel = $channel;
}
/**
* register notifies on context
*
* @param resource $context (optional) to register on, creates a new one if not set
*
* @return resource
*/
public function registerOnContext($context = NULL) {
if ($context === NULL) {
$context = stream_context_create();
}
if (!is_resource($context)) {
throw new InvalidArgumentException('Context must be a resource');
}
$result = stream_context_set_params($context, ["notification" => [$this, 'callback']]);
if (!$result) {
throw new UnexpectedValueException('Unable to set context notification parameter');
}
return $context;
}
private function message($message) {
$now = microtime(true);
$this->startTime || $this->startTime = $now;
$timeStamp = $this->getTimeStamp($now);
$timePassed = sprintf("[%.5f]", $now - $this->startTime);
fputs($this->channel, "$timeStamp $timePassed $message\n");
}
private function dump() {
ob_start();
call_user_func_array('var_dump', func_get_args());
$buffer = ob_get_clean();
$this->message("Dump: " . $buffer);
}
private function getTimeStamp($from = NULL) {
$from || $from = microtime(true);
return date_create_from_format(
'U.u', sprintf('%.f', $from)
)->format('Y-m-d\TH:i:s.uO');
}
/**
* @param $notification_code
* @param $severity
* @param $message
* @param $message_code
* @param $bytes_transferred
* @param $bytes_max
*
* @see http://php.net/manual/en/function.stream-notification-callback.php
*/
public function callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
switch ($notification_code) {
case STREAM_NOTIFY_RESOLVE:
case STREAM_NOTIFY_AUTH_REQUIRED:
case STREAM_NOTIFY_COMPLETED:
case STREAM_NOTIFY_FAILURE:
case STREAM_NOTIFY_AUTH_RESULT:
default:
$this->dump($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max);
break;
case STREAM_NOTIFY_REDIRECTED:
$this->message("Being redirected to: " . $message);
break;
case STREAM_NOTIFY_CONNECT:
$this->message("Connected...");
break;
case STREAM_NOTIFY_FILE_SIZE_IS:
$this->message("Got the filesize: " . $bytes_max);
break;
case STREAM_NOTIFY_MIME_TYPE_IS:
$this->message("Found the mime-type: " . $message);
break;
case STREAM_NOTIFY_PROGRESS:
$this->message("Made some progress, downloaded " . $bytes_transferred . " so far");
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment