1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* 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
|
|
|
namespace Icewind\Streams; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Wrapper that retries reads/writes to remote streams that dont deliver/recieve all requested data at once |
12
|
|
|
*/ |
13
|
|
|
class RetryWrapper extends Wrapper { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Wraps a stream with the provided callbacks |
17
|
|
|
* |
18
|
|
|
* @param resource $source |
19
|
|
|
* @return resource |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public static function wrap($source) { |
|
|
|
|
22
|
|
|
$context = stream_context_create(array( |
23
|
|
|
'retry' => array( |
24
|
|
|
'source' => $source |
25
|
|
|
) |
26
|
|
|
)); |
27
|
|
|
return Wrapper::wrapSource($source, $context, 'retry', '\Icewind\Streams\RetryWrapper'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function open() { |
31
|
|
|
$this->loadContext('retry'); |
32
|
|
|
return true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function dir_opendir($path, $options) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function stream_open($path, $mode, $options, &$opened_path) { |
40
|
|
|
return $this->open(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function stream_read($count) { |
|
|
|
|
44
|
|
|
$result = parent::stream_read($count); |
45
|
|
|
|
46
|
|
|
$bytesReceived = strlen($result); |
47
|
|
|
while ($bytesReceived < $count && !$this->stream_eof()) { |
48
|
|
|
$result .= parent::stream_read($count - $bytesReceived); |
49
|
|
|
$bytesReceived = strlen($result); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $result; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
public function stream_write($data) { |
|
|
|
|
56
|
|
|
$bytesToSend = strlen($data); |
57
|
|
|
$result = parent::stream_write($data); |
58
|
|
|
|
59
|
|
|
while ($result < $bytesToSend && !$this->stream_eof()) { |
60
|
|
|
$dataLeft = substr($data, $result); |
61
|
|
|
$result += parent::stream_write($dataLeft); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.