1 | <?php |
||
31 | abstract class ScannerBase { |
||
32 | |||
33 | /** |
||
34 | * Scan result |
||
35 | * @var Status |
||
36 | */ |
||
37 | protected $status; |
||
38 | |||
39 | /** |
||
40 | * If scanning was done part by part |
||
41 | * the first detected infected part is stored here |
||
42 | * @var Status |
||
43 | */ |
||
44 | protected $infectedStatus; |
||
45 | |||
46 | /** @var int */ |
||
47 | protected $byteCount; |
||
48 | |||
49 | /** @var resource */ |
||
50 | protected $writeHandle; |
||
51 | |||
52 | /** @var AppConfig */ |
||
53 | protected $appConfig; |
||
54 | |||
55 | /** @var ILogger */ |
||
56 | protected $logger; |
||
57 | |||
58 | /** @var string */ |
||
59 | protected $lastChunk; |
||
60 | |||
61 | /** @var bool */ |
||
62 | protected $isLogUsed = false; |
||
63 | |||
64 | /** @var bool */ |
||
65 | protected $isAborted = false; |
||
66 | |||
67 | /** |
||
68 | * Close used resources |
||
69 | */ |
||
70 | abstract protected function shutdownScanner(); |
||
71 | |||
72 | |||
73 | public function getStatus(){ |
||
74 | if ($this->infectedStatus instanceof Status){ |
||
75 | return $this->infectedStatus; |
||
76 | } |
||
77 | if ($this->status instanceof Status){ |
||
78 | return $this->status; |
||
79 | } |
||
80 | return new Status(); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Synchronous scan |
||
85 | * @param IScannable $item |
||
86 | * @return Status |
||
87 | */ |
||
88 | public function scan(IScannable $item) { |
||
89 | $this->initScanner(); |
||
90 | |||
91 | while (false !== ($chunk = $item->fread())) { |
||
92 | $this->writeChunk($chunk); |
||
93 | } |
||
94 | |||
95 | $this->shutdownScanner(); |
||
96 | return $this->getStatus(); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Async scan - new portion of data is available |
||
101 | * @param string $data |
||
102 | */ |
||
103 | public function onAsyncData($data){ |
||
106 | |||
107 | /** |
||
108 | * Async scan - resource is closed |
||
109 | * @return Status |
||
110 | */ |
||
111 | public function completeAsyncScan(){ |
||
115 | |||
116 | /** |
||
117 | * Open write handle. etc |
||
118 | */ |
||
119 | public function initScanner(){ |
||
126 | |||
127 | /** |
||
128 | * @param string $chunk |
||
129 | */ |
||
130 | protected function writeChunk($chunk){ |
||
135 | |||
136 | /** |
||
137 | * @param string $data |
||
138 | */ |
||
139 | final protected function fwrite($data){ |
||
170 | |||
171 | /** |
||
172 | * @return bool |
||
173 | */ |
||
174 | protected function retry(){ |
||
181 | |||
182 | /** |
||
183 | * @param $data |
||
184 | * @return bool |
||
185 | */ |
||
186 | protected function writeRaw($data){ |
||
196 | |||
197 | /** |
||
198 | * Get a resource to write data into |
||
199 | * @return resource |
||
200 | */ |
||
201 | protected function getWriteHandle(){ |
||
204 | |||
205 | /** |
||
206 | * Prepare chunk (if required) |
||
207 | * @param string $data |
||
208 | * @return string |
||
209 | */ |
||
210 | protected function prepareChunk($data){ |
||
213 | } |
||
214 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: