按时间顺序提供的数据元素序列的模型

v1.0.0 2016-03-15 10:54 UTC

This package is auto-updated.

Last update: 2024-09-23 10:38:21 UTC


README

按时间顺序提供的数据元素序列的模型

使用方法

从字符串中写入和读取(或查看)字符。

$stream = new \WillWashburn\Stream\Stream();
$stream->write('foo');

$stream->read(3); //foo

$stream->write('bar');
$stream->write('bang');

$stream->read(4); // barb
$stream->peek(3); // ang
$stream->read(3); // ang

我主要在缓存curl请求的响应时使用这个。

$stream = new WillWashburn\Stream\Stream;

curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $str) use (& $stream, $url) {
           
   $stream->write($str);
   
   try {
       // do something with the stream
       $characters = $stream->read(100);
       
       // tell curl to stop the transfer when we find what we're looking for
       if(strpos($characters,'string im looking for') !==false) {
           return -1;
       }
   }
   catch (StreamBufferTooSmallException $e) {
       // The buffer ended, so keep getting more
       return strlen($str);
   }
   
   //  We return -1 to abort the transfer when we have enough buffered
   return -1;
});

安装

使用composer

composer require willwashburn/stream

或者,将 "willwashburn/stream": "~1.0.0" 添加到您的 composer.json 文件中

变更日志

  • v1.0.0 - 基本流模型