1 | <?php |
||
12 | class NotifyHandler implements INotifyHandler { |
||
13 | /** |
||
14 | * @var Connection |
||
15 | */ |
||
16 | private $connection; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $path; |
||
22 | |||
23 | private $listening = true; |
||
24 | |||
25 | /** |
||
26 | * @param Connection $connection |
||
27 | * @param string $path |
||
28 | */ |
||
29 | 15 | public function __construct(Connection $connection, $path) { |
|
33 | |||
34 | /** |
||
35 | * Get all changes detected since the start of the notify process or the last call to getChanges |
||
36 | * |
||
37 | * @return Change[] |
||
38 | */ |
||
39 | 12 | public function getChanges() { |
|
51 | |||
52 | /** |
||
53 | * Listen actively to all incoming changes |
||
54 | * |
||
55 | * Note that this is a blocking process and will cause the process to block forever if not explicitly terminated |
||
56 | * |
||
57 | * @param callable $callback |
||
58 | */ |
||
59 | 6 | public function listen($callback) { |
|
60 | 6 | if ($this->listening) { |
|
61 | 6 | $this->connection->read(function ($line) use ($callback) { |
|
62 | 6 | $change = $this->parseChangeLine($line); |
|
63 | 6 | if ($change) { |
|
64 | 6 | return $callback($change); |
|
65 | } |
||
66 | 6 | }); |
|
67 | 6 | } |
|
68 | 6 | } |
|
69 | |||
70 | 12 | private function parseChangeLine($line) { |
|
82 | |||
83 | 15 | public function stop() { |
|
87 | |||
88 | 15 | public function __destruct() { |
|
91 | } |
||
92 |