View source
<?php
namespace Drupal\uikit_components;
class MimeStreamWrapper {
const WRAPPER_NAME = 'mime';
public $context;
private static $isRegistered = FALSE;
private $callBackFunction;
private $eof = FALSE;
private $fp;
private $path;
private $fileStat;
private function getStat() {
if ($fStat = fstat($this->fp)) {
return $fStat;
}
$size = 100;
if ($headers = get_headers($this->path, TRUE)) {
$head = array_change_key_case($headers, CASE_LOWER);
$size = (int) $head['content-length'];
}
$blocks = ceil($size / 512);
return [
'dev' => 16777220,
'ino' => 15764,
'mode' => 33188,
'nlink' => 1,
'uid' => 10000,
'gid' => 80,
'rdev' => 0,
'size' => $size,
'atime' => 0,
'mtime' => 0,
'ctime' => 0,
'blksize' => 4096,
'blocks' => $blocks,
];
}
public function setPath($path) {
$this->path = $path;
$this->fp = fopen($this->path, 'rb') or die('Cannot open file: ' . $this->path);
$this->fileStat = $this
->getStat();
}
public function read($count) {
return fread($this->fp, $count);
}
public function getStreamPath() {
return str_replace([
'ftp://',
'http://',
'https://',
], self::WRAPPER_NAME . '://', $this->path);
}
public function getContext() {
if (!self::$isRegistered) {
stream_wrapper_register(self::WRAPPER_NAME, get_class());
self::$isRegistered = TRUE;
}
return stream_context_create([
self::WRAPPER_NAME => [
'cb' => [
$this,
'read',
],
'fileStat' => $this->fileStat,
],
]);
}
public function stream_open($path, $mode, $options, &$opened_path) {
if (!preg_match('/^r[bt]?$/', $mode) || !$this->context) {
return FALSE;
}
$opt = stream_context_get_options($this->context);
if (!is_array($opt[self::WRAPPER_NAME]) || !isset($opt[self::WRAPPER_NAME]['cb']) || !is_callable($opt[self::WRAPPER_NAME]['cb'])) {
return FALSE;
}
$this->callBackFunction = $opt[self::WRAPPER_NAME]['cb'];
$this->fileStat = $opt[self::WRAPPER_NAME]['fileStat'];
return TRUE;
}
public function stream_read($count) {
if ($this->eof || !$count) {
return '';
}
if (($s = call_user_func($this->callBackFunction, $count)) == '') {
$this->eof = TRUE;
}
return $s;
}
public function stream_eof() {
return $this->eof;
}
public function stream_stat() {
return $this->fileStat;
}
public function stream_cast($castAs) {
$read = NULL;
$write = NULL;
$except = NULL;
return @stream_select($read, $write, $except, $castAs);
}
}