5
5
* Licensed under MIT License.
6
6
*
7
7
* @package L4Dav
8
- * @version 0.4 .0
8
+ * @version 0.5 .0
9
9
* @author Ngmy <y.nagamiya@gmail.com>
10
10
* @license http://opensource.org/licenses/MIT MIT License
11
11
* @copyright (c) 2014, Ngmy <y.nagamiya@gmail.com>
12
12
* @link https://github.com/ngmy/l4-dav
13
13
*/
14
14
15
+ use Ngmy \L4Dav \Service \Http \RequestInterface ;
16
+
15
17
/**
16
18
* A WebDAV client class.
17
19
*
@@ -59,26 +61,32 @@ class L4Dav {
59
61
*/
60
62
protected $ url ;
61
63
64
+ protected $ request ;
65
+
62
66
/**
63
67
* Create a new L4Dav class object.
64
68
*
65
- * @param string $webDavUrl The URL of the WebDAV server.
69
+ * @param Ngmy\L4Dav\Service\Http\RequestInterface $request
70
+ * @param string $webDavUrl The URL of the WebDAV server.
71
+ * @param integer $webDavPort The port of the WebDAV server.
66
72
* @access public
67
- * @throws \ InvalidArgumentException
73
+ * @throws InvalidArgumentException
68
74
* @return void
69
75
*/
70
- public function __construct ($ webDavUrl )
76
+ public function __construct (RequestInterface $ request , $ webDavUrl, $ webDavPort = 80 )
71
77
{
72
78
if (!preg_match ('/([a-z]+):\/\/([a-zA-Z0-9\.]+)(:[0-9]+){0,1}(.*)/ ' , $ webDavUrl , $ m )) {
73
79
throw new \InvalidArgumentException ('Invalid URL format ( ' .$ webDavUrl .') ' );
74
80
}
75
81
76
82
$ this ->schema = $ m [1 ];
77
83
$ this ->host = $ m [2 ];
78
- $ this ->port = isset ($ m [3 ]) ? (int ) ltrim ($ m [3 ], ': ' ) : 80 ;
84
+ $ this ->port = isset ($ m [3 ]) ? (int ) ltrim ($ m [3 ], ': ' ) : $ webDavPort ;
79
85
$ this ->path = isset ($ m [4 ]) ? rtrim ($ m [4 ], '/ ' ).'/ ' : '/ ' ;
80
86
81
87
$ this ->url = $ this ->schema .':// ' .$ this ->host .$ this ->path ;
88
+
89
+ $ this ->request = $ request ;
82
90
}
83
91
84
92
/**
@@ -99,7 +107,10 @@ public function get($srcPath, $destPath)
99
107
CURLOPT_RETURNTRANSFER => true ,
100
108
);
101
109
102
- $ result = $ this ->executeWebRequest ('GET ' , $ this ->url .$ srcPath , array (), $ options );
110
+ $ result = $ this ->request ->method ('GET ' )
111
+ ->url ($ this ->url .$ srcPath )
112
+ ->options ($ options )
113
+ ->send ();
103
114
104
115
fclose ($ fh );
105
116
@@ -126,7 +137,10 @@ public function put($srcPath, $destPath)
126
137
CURLOPT_INFILESIZE => $ filesize ,
127
138
);
128
139
129
- $ result = $ this ->executeWebRequest ('PUT ' , $ this ->url .$ destPath , array (), $ options );
140
+ $ result = $ this ->request ->method ('PUT ' )
141
+ ->url ($ this ->url .$ destPath )
142
+ ->options ($ options )
143
+ ->send ();
130
144
131
145
fclose ($ fh );
132
146
@@ -144,7 +158,10 @@ public function delete($path)
144
158
{
145
159
$ options = array (CURLOPT_PORT => $ this ->port );
146
160
147
- return $ this ->executeWebRequest ('DELETE ' ,$ this ->url .$ path , array (), $ options );
161
+ return $ this ->request ->method ('DELETE ' )
162
+ ->url ($ this ->url .$ path )
163
+ ->options ($ options )
164
+ ->send ();
148
165
}
149
166
150
167
/**
@@ -160,7 +177,11 @@ public function copy($srcPath, $destPath)
160
177
$ options = array (CURLOPT_PORT => $ this ->port );
161
178
$ headers = array ('Destination ' => $ this ->url .$ destPath );
162
179
163
- return $ this ->executeWebRequest ('COPY ' , $ this ->url .$ srcPath , $ headers , $ options );
180
+ return $ this ->request ->method ('COPY ' )
181
+ ->url ($ this ->url .$ srcPath )
182
+ ->headers ($ headers )
183
+ ->options ($ options )
184
+ ->send ();
164
185
}
165
186
166
187
/**
@@ -176,7 +197,11 @@ public function move($srcPath, $destPath)
176
197
$ options = array (CURLOPT_PORT => $ this ->port );
177
198
$ headers = array ('Destination ' => $ this ->url .$ destPath );
178
199
179
- return $ this ->executeWebRequest ('MOVE ' , $ this ->url .$ srcPath , $ headers , $ options );
200
+ return $ this ->request ->method ('MOVE ' )
201
+ ->url ($ this ->url .$ srcPath )
202
+ ->headers ($ headers )
203
+ ->options ($ options )
204
+ ->send ();
180
205
}
181
206
182
207
/**
@@ -190,7 +215,10 @@ public function mkdir($path)
190
215
{
191
216
$ options = array (CURLOPT_PORT => $ this ->port );
192
217
193
- return $ this ->executeWebRequest ('MKCOL ' , $ this ->url .$ path , array (), $ options );
218
+ return $ this ->request ->method ('MKCOL ' )
219
+ ->url ($ this ->url .$ path )
220
+ ->options ($ options )
221
+ ->send ();
194
222
}
195
223
196
224
/**
@@ -208,7 +236,10 @@ public function exists($path)
208
236
CURLOPT_RETURNTRANSFER => true ,
209
237
);
210
238
211
- $ response = $ this ->executeWebRequest ('GET ' , $ this ->url .$ path , array (), $ options );
239
+ $ response = $ this ->request ->method ('GET ' )
240
+ ->url ($ this ->url .$ path )
241
+ ->options ($ options )
242
+ ->send ();
212
243
213
244
if ($ response ->getStatus () < 200 || $ response ->getStatus () > 300 ) {
214
245
return false ;
@@ -229,7 +260,11 @@ public function ls($path)
229
260
$ options = array (CURLOPT_PORT => $ this ->port );
230
261
$ headers = array ('Depth ' => 1 );
231
262
232
- $ response = $ this ->executeWebRequest ('PROPFIND ' , $ this ->url .$ path , $ headers , $ options );
263
+ $ response = $ this ->request ->method ('PROPFIND ' )
264
+ ->url ($ this ->url .$ path )
265
+ ->headers ($ headers )
266
+ ->options ($ options )
267
+ ->send ();
233
268
234
269
if ($ response ->getStatus () < 200 || $ response ->getStatus () > 300 ) {
235
270
return array ();
@@ -243,26 +278,4 @@ public function ls($path)
243
278
}
244
279
}
245
280
246
- /**
247
- * Execute the request to the WebDAV server.
248
- *
249
- * @param string $method The HTTP method.
250
- * @param string $url The request URL.
251
- * @param array $headers The HTTP headers.
252
- * @param array $options The cURL options.
253
- * @access protected
254
- * @return \Ngmy\L4Dav\Response Returns a Response class object.
255
- */
256
- protected function executeWebRequest ($ method , $ url , $ headers , $ options )
257
- {
258
- $ curl = new cURL ;
259
-
260
- $ result = $ curl ->newRequest ($ method , $ url )
261
- ->setHeaders ($ headers )
262
- ->setOptions ($ options )
263
- ->send ();
264
-
265
- return $ result ;
266
- }
267
-
268
281
}
0 commit comments