1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014 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\SMB; |
9
|
|
|
|
10
|
|
|
use Icewind\SMB\Exception\Exception; |
11
|
|
|
use Icewind\SMB\Exception\InvalidRequestException; |
12
|
|
|
use Icewind\Streams\File; |
13
|
|
|
|
14
|
|
|
class NativeStream implements File { |
15
|
|
|
/** |
16
|
|
|
* @var resource |
17
|
|
|
*/ |
18
|
|
|
public $context; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Icewind\SMB\NativeState |
22
|
|
|
*/ |
23
|
|
|
private $state; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var resource |
27
|
|
|
*/ |
28
|
|
|
private $handle; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $eof = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $url; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Wrap a stream from libsmbclient-php into a regular php stream |
42
|
|
|
* |
43
|
|
|
* @param \Icewind\SMB\NativeState $state |
44
|
|
|
* @param resource $smbStream |
45
|
|
|
* @param string $mode |
46
|
|
|
* @param string $url |
47
|
|
|
* @return resource |
48
|
|
|
*/ |
49
|
38 |
|
public static function wrap($state, $smbStream, $mode, $url) { |
50
|
38 |
|
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream'); |
51
|
38 |
|
$context = stream_context_create(array( |
52
|
|
|
'nativesmb' => array( |
53
|
38 |
|
'state' => $state, |
54
|
38 |
|
'handle' => $smbStream, |
55
|
|
|
'url' => $url |
56
|
38 |
|
) |
57
|
38 |
|
)); |
58
|
38 |
|
$fh = fopen('nativesmb://', $mode, false, $context); |
59
|
38 |
|
stream_wrapper_unregister('nativesmb'); |
60
|
38 |
|
return $fh; |
61
|
|
|
} |
62
|
|
|
|
63
|
38 |
|
public function stream_close() { |
64
|
38 |
|
return $this->state->close($this->handle); |
65
|
|
|
} |
66
|
|
|
|
67
|
19 |
|
public function stream_eof() { |
68
|
19 |
|
return $this->eof; |
69
|
|
|
} |
70
|
|
|
|
71
|
38 |
|
public function stream_flush() { |
72
|
38 |
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
38 |
|
public function stream_open($path, $mode, $options, &$opened_path) { |
76
|
38 |
|
$context = stream_context_get_options($this->context); |
77
|
38 |
|
$this->state = $context['nativesmb']['state']; |
78
|
38 |
|
$this->handle = $context['nativesmb']['handle']; |
79
|
38 |
|
$this->url = $context['nativesmb']['url']; |
80
|
38 |
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
19 |
|
public function stream_read($count) { |
84
|
19 |
|
$result = $this->state->read($this->handle, $count); |
85
|
19 |
|
if (strlen($result) < $count) { |
86
|
19 |
|
$this->eof = true; |
87
|
19 |
|
} |
88
|
19 |
|
return $result; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function stream_seek($offset, $whence = SEEK_SET) { |
92
|
1 |
|
$this->eof = false; |
93
|
|
|
try { |
94
|
1 |
|
return $this->state->lseek($this->handle, $offset, $whence) !== false; |
95
|
|
|
} catch (InvalidRequestException $e) { |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
18 |
|
public function stream_stat() { |
101
|
|
|
try { |
102
|
18 |
|
return $this->state->stat($this->url); |
103
|
|
|
} catch (Exception $e) { |
104
|
|
|
return false; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
public function stream_tell() { |
109
|
1 |
|
return $this->state->lseek($this->handle, 0, SEEK_CUR); |
110
|
|
|
} |
111
|
|
|
|
112
|
18 |
|
public function stream_write($data) { |
113
|
18 |
|
return $this->state->write($this->handle, $data); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
public function stream_truncate($size) { |
117
|
1 |
|
return $this->state->ftruncate($this->handle, $size); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
public function stream_set_option($option, $arg1, $arg2) { |
121
|
1 |
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
public function stream_lock($operation) { |
125
|
1 |
|
return false; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.