1111 * @package class_package
1212 * @author Eugene Kuznetcov <easmith@mail.ru>
1313 */
14- class SCurl {
14+ class SCurl
15+ {
1516
1617 static private $ instance = null ;
1718
@@ -43,30 +44,19 @@ class SCurl {
4344 */
4445 private $ params = array ();
4546
46- /**
47- *
48- * @param string $url
49- *
50- * @return SCurl
51- */
52- static function init ($ url ) {
53- if (self ::$ instance == null ) {
54- self ::$ instance = new SCurl ($ url );
55- }
56- return self ::$ instance ->setUrl ($ url );
57- }
58-
5947 /**
6048 * Curl wrapper
6149 *
6250 * @param string $url
6351 */
64- private function __construct ($ url ) {
52+ private function __construct ($ url )
53+ {
6554 $ this ->setUrl ($ url );
6655 $ this ->curlInit ();
6756 }
6857
69- private function curlInit () {
58+ private function curlInit ()
59+ {
7060 $ this ->ch = curl_init ($ this ->url );
7161 curl_setopt ($ this ->ch , CURLOPT_ENCODING , 'gzip,deflate ' );
7262 curl_setopt ($ this ->ch , CURLOPT_FOLLOWLOCATION , false );
@@ -79,20 +69,43 @@ private function curlInit() {
7969// curl_setopt($this->ch, CURLOPT_RANGE, "0-100");
8070 }
8171
72+ /**
73+ *
74+ * @param string $url
75+ *
76+ * @return SCurl
77+ */
78+ static function init ($ url )
79+ {
80+ if (self ::$ instance == null ) {
81+ self ::$ instance = new SCurl ($ url );
82+ }
83+ return self ::$ instance ->setUrl ($ url );
84+ }
85+
8286 /**
8387 * Set url for request
8488 *
8589 * @param string $url URL
8690 *
8791 * @return SCurl
8892 */
89- public function setUrl ($ url ) {
93+ public function setUrl ($ url )
94+ {
9095 $ this ->url = $ url ;
9196 return self ::$ instance ;
9297 }
9398
94- private function __clone () {
95-
99+ public function putFile ($ file )
100+ {
101+ if (!file_exists ($ file ))
102+ throw new SelectelStorageException ("File ' {$ file }' does not exist " );
103+ $ fp = fopen ($ file , "r " );
104+ curl_setopt ($ this ->ch , CURLOPT_INFILE , $ fp );
105+ curl_setopt ($ this ->ch , CURLOPT_INFILESIZE , filesize ($ file ));
106+ $ this ->request ('PUT ' );
107+ fclose ($ fp );
108+ return self ::$ instance ;
96109 }
97110
98111 /**
@@ -102,7 +115,8 @@ private function __clone() {
102115 *
103116 * @return SCurl
104117 */
105- public function request ($ method ) {
118+ public function request ($ method )
119+ {
106120 $ this ->method ($ method );
107121 $ this ->params = array ();
108122 curl_setopt ($ this ->ch , CURLOPT_URL , $ this ->url );
@@ -127,65 +141,45 @@ public function request($method) {
127141 *
128142 * @return SCurl
129143 */
130- private function method ($ method ) {
144+ private function method ($ method )
145+ {
131146 switch ($ method ) {
132147 case "GET " : {
133- $ this ->url .= "? " . http_build_query ($ this ->params );
134- curl_setopt ($ this ->ch , CURLOPT_HTTPGET , true );
135- break ;
136- }
148+ $ this ->url .= "? " . http_build_query ($ this ->params );
149+ curl_setopt ($ this ->ch , CURLOPT_HTTPGET , true );
150+ break ;
151+ }
137152 case "HEAD " : {
138- $ this ->url .= "? " . http_build_query ($ this ->params );
139- curl_setopt ($ this ->ch , CURLOPT_NOBODY , true );
140- break ;
141- }
153+ $ this ->url .= "? " . http_build_query ($ this ->params );
154+ curl_setopt ($ this ->ch , CURLOPT_NOBODY , true );
155+ break ;
156+ }
142157 case "POST " : {
143- curl_setopt ($ this ->ch , CURLOPT_POST , true );
144- curl_setopt ($ this ->ch , CURLOPT_POSTFIELDS , http_build_query ($ this ->params ));
145- break ;
146- }
158+ curl_setopt ($ this ->ch , CURLOPT_POST , true );
159+ curl_setopt ($ this ->ch , CURLOPT_POSTFIELDS , http_build_query ($ this ->params ));
160+ break ;
161+ }
147162 case "PUT " : {
148- curl_setopt ($ this ->ch , CURLOPT_PUT , true );
149- break ;
150- }
163+ curl_setopt ($ this ->ch , CURLOPT_PUT , true );
164+ break ;
165+ }
151166 default : {
152- curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , $ method );
153- break ;
154- }
167+ curl_setopt ($ this ->ch , CURLOPT_CUSTOMREQUEST , $ method );
168+ break ;
169+ }
155170 }
156171 return self ::$ instance ;
157172 }
158173
159- public function putFile ($ file ) {
160- if (!file_exists ($ file ))
161- throw new SelectelStorageException ("File ' {$ file }' does not exist " );
162- $ fp = fopen ($ file , "r " );
163- curl_setopt ($ this ->ch , CURLOPT_INFILE , $ fp );
164- curl_setopt ($ this ->ch , CURLOPT_INFILESIZE , filesize ($ file ));
165- $ this ->request ('PUT ' );
166- fclose ($ fp );
167- return self ::$ instance ;
168- }
169-
170- public function putFileContents ($ contents ) {
171- $ fp = fopen ("php://temp " , "r+ " );
172- fputs ($ fp , $ contents );
173- rewind ($ fp );
174- curl_setopt ($ this ->ch , CURLOPT_INFILE , $ fp );
175- curl_setopt ($ this ->ch , CURLOPT_INFILESIZE , strlen ($ contents ));
176- $ this ->request ('PUT ' );
177- fclose ($ fp );
178- return self ::$ instance ;
179- }
180-
181174 /**
182175 * Header Parser
183176 *
184177 * @param array $head
185178 *
186179 * @return array
187180 */
188- private function parseHead ($ head ) {
181+ private function parseHead ($ head )
182+ {
189183 $ result = array ();
190184 $ code = explode ("\r\n" , $ head );
191185 $ result ['HTTP-Code ' ] = intval (str_replace ("HTTP/1.1 " , "" , $ code [0 ]));
@@ -196,14 +190,27 @@ private function parseHead($head) {
196190 return $ result ;
197191 }
198192
193+ public function putFileContents ($ contents )
194+ {
195+ $ fp = fopen ("php://temp " , "r+ " );
196+ fputs ($ fp , $ contents );
197+ rewind ($ fp );
198+ curl_setopt ($ this ->ch , CURLOPT_INFILE , $ fp );
199+ curl_setopt ($ this ->ch , CURLOPT_INFILESIZE , strlen ($ contents ));
200+ $ this ->request ('PUT ' );
201+ fclose ($ fp );
202+ return self ::$ instance ;
203+ }
204+
199205 /**
200206 * Set headers
201207 *
202208 * @param array $headers
203209 *
204210 * @return SCurl
205211 */
206- public function setHeaders ($ headers ) {
212+ public function setHeaders ($ headers )
213+ {
207214 $ headers = array_merge (array ("Expect: " ), $ headers );
208215 curl_setopt ($ this ->ch , CURLOPT_HTTPHEADER , $ headers );
209216 return self ::$ instance ;
@@ -216,7 +223,8 @@ public function setHeaders($headers) {
216223 *
217224 * @return SCurl
218225 */
219- public function setParams ($ params ) {
226+ public function setParams ($ params )
227+ {
220228 $ this ->params = $ params ;
221229 return self ::$ instance ;
222230 }
@@ -226,7 +234,8 @@ public function setParams($params) {
226234 *
227235 * @return array
228236 */
229- public function getResult () {
237+ public function getResult ()
238+ {
230239 return $ this ->result ;
231240 }
232241
@@ -237,7 +246,8 @@ public function getResult() {
237246 *
238247 * @return array
239248 */
240- public function getHeaders ($ header = null ) {
249+ public function getHeaders ($ header = null )
250+ {
241251 if (!is_null ($ header ))
242252 $ this ->result ['header ' ][$ header ];
243253 return $ this ->result ['header ' ];
@@ -248,7 +258,8 @@ public function getHeaders($header = null) {
248258 *
249259 * @return array
250260 */
251- public function getContent () {
261+ public function getContent ()
262+ {
252263 return $ this ->result ['content ' ];
253264 }
254265
@@ -259,10 +270,16 @@ public function getContent() {
259270 *
260271 * @return array
261272 */
262- public function getInfo ($ info = null ) {
273+ public function getInfo ($ info = null )
274+ {
263275 if (!is_null ($ info ))
264276 $ this ->result ['info ' ][$ info ];
265277 return $ this ->result ['info ' ];
266278 }
267279
280+ private function __clone ()
281+ {
282+
283+ }
284+
268285}
0 commit comments