1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Robin Appelman <[email protected]> |
4
|
|
|
* This file is licensed under the Licensed under the MIT license: |
5
|
|
|
* http://opensource.org/licenses/MIT |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Icewind\SMB\Wrapped; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Icewind\SMB\Change; |
13
|
|
|
use Icewind\SMB\Exception\Exception; |
14
|
|
|
use Icewind\SMB\Exception\RevisionMismatchException; |
15
|
|
|
use Icewind\SMB\INotifyHandler; |
16
|
|
|
|
17
|
|
|
class NotifyHandler implements INotifyHandler { |
18
|
|
|
/** |
19
|
|
|
* @var Connection |
20
|
|
|
*/ |
21
|
|
|
private $connection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $path; |
27
|
|
|
|
28
|
|
|
private $listening = true; |
29
|
|
|
|
30
|
|
|
// see error.h |
31
|
|
|
const EXCEPTION_MAP = [ |
32
|
|
|
ErrorCodes::RevisionMismatch => RevisionMismatchException::class, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Connection $connection |
37
|
|
|
* @param string $path |
38
|
|
|
*/ |
39
|
15 |
|
public function __construct(Connection $connection, $path) { |
40
|
15 |
|
$this->connection = $connection; |
41
|
15 |
|
$this->path = $path; |
42
|
15 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get all changes detected since the start of the notify process or the last call to getChanges |
46
|
|
|
* |
47
|
|
|
* @return Change[] |
48
|
|
|
*/ |
49
|
12 |
|
public function getChanges() { |
50
|
12 |
|
if (!$this->listening) { |
51
|
3 |
|
return []; |
52
|
|
|
} |
53
|
9 |
|
stream_set_blocking($this->connection->getOutputStream(), 0); |
54
|
9 |
|
$lines = []; |
55
|
9 |
|
while (($line = $this->connection->readLine())) { |
56
|
9 |
|
$this->checkForError($line); |
57
|
9 |
|
$lines[] = $line; |
58
|
|
|
} |
59
|
9 |
|
stream_set_blocking($this->connection->getOutputStream(), 1); |
60
|
9 |
|
return array_values(array_filter(array_map([$this, 'parseChangeLine'], $lines))); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Listen actively to all incoming changes |
65
|
|
|
* |
66
|
|
|
* Note that this is a blocking process and will cause the process to block forever if not explicitly terminated |
67
|
|
|
* |
68
|
|
|
* @param callable $callback |
69
|
|
|
*/ |
70
|
6 |
|
public function listen($callback) { |
71
|
6 |
|
if ($this->listening) { |
72
|
6 |
|
$this->connection->read(function ($line) use ($callback) { |
73
|
6 |
|
$this->checkForError($line); |
74
|
6 |
|
$change = $this->parseChangeLine($line); |
75
|
6 |
|
if ($change) { |
76
|
6 |
|
return $callback($change); |
77
|
|
|
} |
78
|
6 |
|
}); |
79
|
|
|
} |
80
|
6 |
|
} |
81
|
|
|
|
82
|
12 |
|
private function parseChangeLine($line) { |
83
|
12 |
|
$code = (int)substr($line, 0, 4); |
84
|
12 |
|
if ($code === 0) { |
85
|
9 |
|
return null; |
86
|
|
|
} |
87
|
12 |
|
$subPath = str_replace('\\', '/', substr($line, 5)); |
88
|
12 |
|
if ($this->path === '') { |
89
|
9 |
|
return new Change($code, $subPath); |
90
|
|
|
} else { |
91
|
3 |
|
return new Change($code, $this->path . '/' . $subPath); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
12 |
|
private function checkForError($line) { |
96
|
12 |
|
if (substr($line, 0, 16) === 'notify returned ') { |
97
|
|
|
$error = substr($line, 16); |
98
|
|
|
throw Exception::fromMap(array_merge(self::EXCEPTION_MAP, Parser::EXCEPTION_MAP), $error, 'Notify is not supported with the used smb version'); |
99
|
|
|
} |
100
|
12 |
|
} |
101
|
|
|
|
102
|
15 |
|
public function stop() { |
103
|
15 |
|
$this->listening = false; |
104
|
15 |
|
$this->connection->close(); |
105
|
15 |
|
} |
106
|
|
|
|
107
|
15 |
|
public function __destruct() { |
108
|
15 |
|
$this->stop(); |
109
|
15 |
|
} |
110
|
|
|
} |
111
|
|
|
|