Advertisement
Guest User

Untitled

a guest
Sep 8th, 2015
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.20 KB | None | 0 0
  1. <?php
  2. function fake_provider($code)
  3. {
  4.     if ($code==='cat') return 'meow';
  5.     elseif ($code==='dog') return 'woof';
  6.     elseif ($code==='snake') return 'hiss';
  7. }
  8.  
  9. class Req implements ArrayAccess
  10. {
  11.     public $codes, $completed=false, $result;
  12.     public function __construct(...$codes)
  13.     {
  14.         $this->codes=$codes;
  15.     }
  16.    
  17.     public function complete()
  18.     {
  19.         while (!$this->completed()) $this->progress();
  20.     }
  21.    
  22.     public function progress()
  23.     {
  24.         $code=array_pop($this->codes);
  25.         $this->result[$code]=fake_provider($code);
  26.     }
  27.    
  28.     public function completed() { return empty($this->codes); }
  29.    
  30.     public function offsetExists($offset) { if ($this->completed()) return array_key_exists($offset, $this->result); else $this->premature_call(); }
  31.     public function offsetGet($offset) { if ($this->completed()) return $this->result[$offset]; else $this->premature_call(); }
  32.     public function offsetSet ( $offset , $value ) { $this->set_is_forbidden(); }
  33.     public function offsetUnset ( $offset ) { $this->set_is_forbidden(); }
  34.     public function premature_call() { throw new Exception('premature req call'); }
  35.     public function set_is_forbidden() { throw new Exception('set is forbidden'); }
  36. }
  37.  
  38. function process_values()
  39. {
  40.     $need=new Req('cat', 'snake');
  41.     $tries=0;
  42.     echo("phase once\n");
  43.     while (!$need->completed())
  44.     {
  45.         $tries++;
  46.         echo "try #$tries\n";
  47.         yield $need;
  48.     }
  49.     echo("phase two\n");
  50.     yield "cat goes $need[cat] and snake goes $need[snake]";
  51. }
  52.  
  53. for ($gen=process_values(); $gen->valid(); $gen->next())
  54. {
  55.     $response=$gen->current();
  56.     echo("response is: ".print_r($response, true)."\n");
  57.     if ($response instanceof Req) $response->progress();
  58. }
  59.  
  60. echo('result is: '.print_r($response, true));
  61.  
  62. /*
  63. ----
  64. Output is:
  65.  
  66. phase once
  67. try #1
  68. response is: Req Object
  69. (
  70.     [codes] => Array
  71.         (
  72.             [0] => cat
  73.             [1] => snake
  74.         )
  75.  
  76.     [completed] =>
  77.     [result] =>
  78. )
  79.  
  80. try #2
  81. response is: Req Object
  82. (
  83.     [codes] => Array
  84.         (
  85.             [0] => cat
  86.         )
  87.  
  88.     [completed] =>
  89.     [result] => Array
  90.         (
  91.             [snake] => hiss
  92.         )
  93.  
  94. )
  95.  
  96. phase two
  97. response is: cat goes meow and snake goes hiss
  98. result is: cat goes meow and snake goes hiss
  99. */
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement