Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Buffer {
- private $buffer = '';
- private $puffs = 0;
- public function start() {
- ob_start($this->getCallback());
- }
- private function buff(string $string) {
- $this->buffer .= $string;
- echo ".";
- }
- public function stop() {
- ob_end_clean();
- }
- public function report(): string {
- return $this->buffer;
- }
- private function getCallback(): callable {
- return function(string $str) {
- $this->buff($str);
- };
- }
- public function puff() {
- $this->stop();
- if ($this->puffs > 3) {
- $this->puffs = 0;
- echo "\r";
- }
- echo '.';
- $this->puffs++;
- $this->start();
- }
- }
- $buffer = new Buffer();
- $buffer->start();
- foreach (range(0, 10) as $id) {
- echo "Hello world {$id}", PHP_EOL;
- $buffer->puff();
- usleep(500000);
- }
- $buffer->stop();
- echo PHP_EOL,"Buffering ended", PHP_EOL;
- echo $buffer->report();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement