carno-php /
config
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Config watching |
||||
| 4 | * User: moyo |
||||
| 5 | * Date: 18/10/2017 |
||||
| 6 | * Time: 3:30 PM |
||||
| 7 | */ |
||||
| 8 | |||||
| 9 | namespace Carno\Config\Chips; |
||||
| 10 | |||||
| 11 | use Closure; |
||||
| 12 | use Throwable; |
||||
| 13 | |||||
| 14 | trait Watching |
||||
| 15 | { |
||||
| 16 | /** |
||||
| 17 | * @var array |
||||
| 18 | */ |
||||
| 19 | private $observer = []; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * @var array |
||||
| 23 | */ |
||||
| 24 | private $replicator = []; |
||||
| 25 | |||||
| 26 | /** |
||||
| 27 | * @var array |
||||
| 28 | */ |
||||
| 29 | private $watching = []; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * @param string $key |
||||
| 33 | * @param Closure $observer |
||||
| 34 | * @return string |
||||
| 35 | */ |
||||
| 36 | public function watching(string $key, Closure $observer) : string |
||||
| 37 | { |
||||
| 38 | $wid = spl_object_hash($observer); |
||||
| 39 | |||||
| 40 | if ($key === '*') { |
||||
| 41 | $this->replicator[$wid] = $observer; |
||||
| 42 | } else { |
||||
| 43 | $this->observer[$key][$wid] = $observer; |
||||
| 44 | $this->watching[$wid][] = $key; |
||||
| 45 | // trigger notify immediate if value already exists |
||||
| 46 | if ($this->has($key)) { |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 47 | $this->cbPerforming($observer, $key, $this->get($key)); |
||||
|
0 ignored issues
–
show
It seems like
get() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 48 | } |
||||
| 49 | } |
||||
| 50 | |||||
| 51 | return $wid; |
||||
| 52 | } |
||||
| 53 | |||||
| 54 | /** |
||||
| 55 | * @param string $wid |
||||
| 56 | */ |
||||
| 57 | public function unwatch(string $wid) : void |
||||
| 58 | { |
||||
| 59 | foreach ($this->watching[$wid] ?? [] as $key) { |
||||
| 60 | unset($this->observer[$key][$wid]); |
||||
| 61 | } |
||||
| 62 | unset($this->watching[$wid]); |
||||
| 63 | unset($this->replicator[$wid]); |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | /** |
||||
| 67 | * @param string $key |
||||
| 68 | * @param $new |
||||
| 69 | */ |
||||
| 70 | protected function wkChanged(string $key, $new) : void |
||||
| 71 | { |
||||
| 72 | foreach ($this->observer[$key] ?? [] as $watcher) { |
||||
| 73 | $this->cbPerforming($watcher, $key, $new); |
||||
| 74 | } |
||||
| 75 | |||||
| 76 | foreach ($this->replicator as $receiver) { |
||||
| 77 | $this->cbPerforming($receiver, $key, $new); |
||||
| 78 | } |
||||
| 79 | } |
||||
| 80 | |||||
| 81 | /** |
||||
| 82 | * @param Closure $program |
||||
| 83 | * @param string $key |
||||
| 84 | * @param $value |
||||
| 85 | */ |
||||
| 86 | private function cbPerforming(Closure $program, string $key, $value) : void |
||||
| 87 | { |
||||
| 88 | try { |
||||
| 89 | call_user_func_array($program, [$value, $key]); |
||||
| 90 | } catch (Throwable $e) { |
||||
| 91 | // do some thing |
||||
| 92 | } |
||||
| 93 | } |
||||
| 94 | } |
||||
| 95 |