Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.03 KB | None | 0 0
  1. <?php
  2.  
  3. class Buffer {
  4.  
  5.     private $buffer = '';
  6.  
  7.     private $puffs = 0;
  8.  
  9.     public function start() {
  10.         ob_start($this->getCallback());
  11.     }
  12.  
  13.     private function buff(string $string) {
  14.         $this->buffer .= $string;
  15.         echo ".";
  16.     }
  17.  
  18.     public function stop() {
  19.         ob_end_clean();
  20.     }
  21.  
  22.     public function report(): string {
  23.         return $this->buffer;
  24.     }
  25.  
  26.     private function getCallback(): callable  {
  27.         return function(string $str) {
  28.             $this->buff($str);
  29.         };
  30.     }
  31.  
  32.     public function puff() {
  33.         $this->stop();
  34.         if ($this->puffs > 3) {
  35.             $this->puffs = 0;
  36.             echo "\r";
  37.         }
  38.         echo '.';
  39.         $this->puffs++;
  40.         $this->start();
  41.     }
  42. }
  43.  
  44. $buffer = new Buffer();
  45.  
  46. $buffer->start();
  47.  
  48. foreach (range(0, 10) as $id) {
  49.     echo "Hello world {$id}", PHP_EOL;
  50.     $buffer->puff();
  51.     usleep(500000);
  52. }
  53.  
  54. $buffer->stop();
  55.  
  56. echo PHP_EOL,"Buffering ended", PHP_EOL;
  57.  
  58. echo $buffer->report();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement