These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace VCR\LibraryHooks; |
||
4 | |||
5 | use VCR\Request; |
||
6 | use VCR\Response; |
||
7 | use VCR\Util\Assertion; |
||
8 | use VCR\Util\HttpUtil; |
||
9 | use VCR\Util\StreamHelper; |
||
10 | |||
11 | /** |
||
12 | * Library hook for streamWrapper functions using stream_wrapper_register(). |
||
13 | */ |
||
14 | class StreamWrapperHook implements LibraryHook |
||
15 | { |
||
16 | /** |
||
17 | * @var \Closure Callback which will be executed when a request is intercepted. |
||
18 | */ |
||
19 | protected static $requestCallback; |
||
20 | |||
21 | /** |
||
22 | * @var integer Position in the current response body. |
||
23 | */ |
||
24 | protected $position; |
||
25 | |||
26 | /** |
||
27 | * @var string Current status of this hook, either enabled or disabled. |
||
28 | */ |
||
29 | protected $status = self::DISABLED; |
||
30 | |||
31 | /** |
||
32 | * @var Response |
||
33 | */ |
||
34 | protected $response; |
||
35 | |||
36 | /** |
||
37 | * @var resource Current stream context. |
||
38 | */ |
||
39 | public $context; |
||
40 | |||
41 | /** |
||
42 | * @inheritDoc |
||
43 | */ |
||
44 | 49 | public function enable(\Closure $requestCallback) |
|
45 | { |
||
46 | 49 | Assertion::isCallable($requestCallback, 'No valid callback for handling requests defined.'); |
|
47 | 49 | self::$requestCallback = $requestCallback; |
|
48 | 49 | stream_wrapper_unregister('http'); |
|
49 | 49 | stream_wrapper_register('http', __CLASS__, STREAM_IS_URL); |
|
50 | |||
51 | 49 | stream_wrapper_unregister('https'); |
|
52 | 49 | stream_wrapper_register('https', __CLASS__, STREAM_IS_URL); |
|
53 | |||
54 | 49 | $this->status = self::ENABLED; |
|
55 | 49 | } |
|
56 | |||
57 | /** |
||
58 | * @inheritDoc |
||
59 | */ |
||
60 | 35 | public function disable() |
|
61 | { |
||
62 | 35 | self::$requestCallback = null; |
|
63 | 35 | stream_wrapper_restore('http'); |
|
64 | 35 | stream_wrapper_restore('https'); |
|
65 | |||
66 | 35 | $this->status = self::DISABLED; |
|
67 | 35 | } |
|
68 | |||
69 | /** |
||
70 | * @inheritDoc |
||
71 | */ |
||
72 | 14 | public function isEnabled() |
|
73 | { |
||
74 | 14 | return $this->status == self::ENABLED; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * This method is called immediately after the wrapper is initialized (f.e. by fopen() and file_get_contents()). |
||
79 | * |
||
80 | * @link http://www.php.net/manual/en/streamwrapper.stream-open.php |
||
81 | * @param string $path Specifies the URL that was passed to the original function. |
||
82 | * @param string $mode The mode used to open the file, as detailed for fopen(). |
||
83 | * @param int $options Holds additional flags set by the streams API. |
||
84 | * @param string $opened_path If the path is opened successfully, and STREAM_USE_PATH is set. |
||
85 | * |
||
86 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
87 | */ |
||
88 | 21 | public function stream_open($path, $mode, $options, &$opened_path) |
|
0 ignored issues
–
show
|
|||
89 | { |
||
90 | 21 | $request = StreamHelper::createRequestFromStreamContext($this->context, $path); |
|
91 | |||
92 | 21 | $requestCallback = self::$requestCallback; |
|
93 | 21 | $this->response = $requestCallback($request); |
|
94 | |||
95 | 14 | return true; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Read from stream. |
||
100 | * |
||
101 | * @link http://www.php.net/manual/en/streamwrapper.stream-read.php |
||
102 | * @param int $count How many bytes of data from the current position should be returned. |
||
103 | * |
||
104 | * @return string If there are less than count bytes available, return as many as are available. |
||
105 | * If no more data is available, return either FALSE or an empty string. |
||
106 | */ |
||
107 | 14 | public function stream_read($count) |
|
108 | { |
||
109 | 14 | $ret = substr($this->response->getBody(), $this->position, $count); |
|
110 | 14 | $this->position += strlen($ret); |
|
111 | |||
112 | 14 | return $ret; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Write to stream. |
||
117 | * |
||
118 | * @throws \BadMethodCallException If called, because this method is not applicable for this stream. |
||
119 | * @link http://www.php.net/manual/en/streamwrapper.stream-write.php |
||
120 | * |
||
121 | * @param string $data Should be stored into the underlying stream. |
||
122 | * |
||
123 | * @return int |
||
124 | */ |
||
125 | public function stream_write($data) |
||
0 ignored issues
–
show
|
|||
126 | { |
||
127 | throw new \BadMethodCallException('No writing possible'); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Retrieve the current position of a stream. |
||
132 | * |
||
133 | * This method is called in response to fseek() to determine the current position. |
||
134 | * |
||
135 | * @link http://www.php.net/manual/en/streamwrapper.stream-tell.php |
||
136 | * |
||
137 | * @return integer Should return the current position of the stream. |
||
138 | */ |
||
139 | public function stream_tell() |
||
140 | { |
||
141 | return $this->position; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Tests for end-of-file on a file pointer. |
||
146 | * |
||
147 | * @link http://www.php.net/manual/en/streamwrapper.stream-eof.php |
||
148 | * |
||
149 | * @return boolean Should return TRUE if the read/write position is at the end of the stream |
||
150 | * and if no more data is available to be read, or FALSE otherwise. |
||
151 | */ |
||
152 | 7 | public function stream_eof() |
|
153 | { |
||
154 | 7 | return $this->position >= strlen($this->response->getBody()); |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Retrieve information about a file resource. |
||
159 | * |
||
160 | * @link http://www.php.net/manual/en/streamwrapper.stream-stat.php |
||
161 | * |
||
162 | * @return array See stat(). |
||
163 | */ |
||
164 | 7 | public function stream_stat() |
|
165 | { |
||
166 | 7 | return array(); |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * Retrieve information about a file resource. |
||
171 | * |
||
172 | * @link http://www.php.net/manual/en/streamwrapper.url-stat.php |
||
173 | * |
||
174 | * @return array See stat(). |
||
175 | */ |
||
176 | |||
177 | public function url_stat($path, $flags) |
||
0 ignored issues
–
show
|
|||
178 | { |
||
179 | return array(); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Seeks to specific location in a stream. |
||
184 | * |
||
185 | * @param integer $offset The stream offset to seek to. |
||
186 | * @param integer $whence Possible values: |
||
187 | * SEEK_SET - Set position equal to offset bytes. |
||
188 | * SEEK_CUR - Set position to current location plus offset. |
||
189 | * SEEK_END - Set position to end-of-file plus offset. |
||
190 | * @return boolean Return TRUE if the position was updated, FALSE otherwise. |
||
191 | */ |
||
192 | 7 | public function stream_seek($offset, $whence) |
|
193 | { |
||
194 | switch ($whence) { |
||
195 | 7 | case SEEK_SET: |
|
196 | 7 | if ($offset < strlen($this->response->getBody()) && $offset >= 0) { |
|
197 | 7 | $this->position = $offset; |
|
198 | |||
199 | 7 | return true; |
|
200 | } |
||
201 | 7 | break; |
|
202 | 7 | case SEEK_CUR: |
|
203 | 7 | if ($offset >= 0) { |
|
204 | 7 | $this->position += $offset; |
|
205 | |||
206 | 7 | return true; |
|
207 | } |
||
208 | 7 | break; |
|
209 | 7 | case SEEK_END: |
|
210 | 7 | if (strlen($this->response->getBody()) + $offset >= 0) { |
|
211 | 7 | $this->position = strlen($this->response->getBody()) + $offset; |
|
212 | |||
213 | 7 | return true; |
|
214 | } |
||
215 | 5 | } |
|
216 | |||
217 | 7 | return false; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Change stream options. |
||
222 | * |
||
223 | * @link http://www.php.net/manual/en/streamwrapper.stream-metadata.php |
||
224 | * @param string $path The file path or URL to set metadata. |
||
225 | * @param integer $option One of the stream options. |
||
226 | * @param mixed $var Value depending on the option. |
||
227 | * |
||
228 | * @return boolean Returns TRUE on success or FALSE on failure. |
||
229 | */ |
||
230 | public function stream_metadata($path, $option, $var) |
||
0 ignored issues
–
show
|
|||
231 | { |
||
232 | return false; |
||
233 | } |
||
234 | } |
||
235 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.