1. Go to this page and download the library: Download shyru/timedilation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace IO\Util;
/**
* Provides buffering of arbitrary data and flushing to registered consumers
* in an interval that is bigger than the bufferTime.
*/
class Buffer
{
private $bufferTime;
private $buffer;
private $callbacks;
private $lastFlush;
/**
* Constructs a new buffer.
*
* @param int $_bufferTime The buffer time in seconds.
*/
function __construct($_bufferTime=10)
{
$this->bufferTime=$_bufferTime;
$this->lastFlush=time();
}
/**
* Registers a new consumer callback that should be called
* whenever at least 10 seconds passed since the last time the data was flushed.
*
* @param callable $_callback The callback that should be called to receive the flushed data
*/
function registerConsumer($_callback)
{
$this->callbacks[]=$_callback;
}
/**
* Append some data to the buffer.
* If the last flush of data was more then bufferTime seconds ago, the buffer will be flushed to all registered
* consumers.
*
* @param mixed $_data The data that should be buffered.
*/
function append($_data)
{
$this->buffer[]=$_data;
if ($this->lastFlush+$this->bufferTime<time())
{ //at least 10 seconds passed since the last flush, now flush our buffer
foreach ($this->callbacks as $callback)
{
call_user_func($callback,$this->buffer);
}
$this->buffer=array();
$this->lastFlush=time();
}
}
}
namespace TimeDilation;
TimeMachine::infectNamespace("IO\\Util"); //infect the namespace so that we can control time
tainer=new \stdClass;
$flushContainer->data=array();
$buffer=new \IO\Util\Buffer();
$buffer->registerConsumer(function($_data) use ($flushContainer) {
$flushContainer->data=$_data;
});
$buffer->append("TimeMachine");
TimeMachine::fastForward(4); //fast forward time by 4 seconds
$buffer->append("rocks");
TimeMachine::fastForward(7); //fast forward time by 7 seconds
$buffer->append("the world!");
//since we forwarded time more than 10 seconds our consumer should now have been called,
//lets check this
$this->assertEquals(3,count($flushContainer->data));
$this->assertEquals("TimeMachine",$flushContainer->data[0]);
$this->assertEquals("rocks",$flushContainer->data[1]);
$this->assertEquals("the world!",$flushContainer->data[2]);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.