Skip to content

Commit 71c9a36

Browse files
author
Евгений Кузнецов
committed
http2
1 parent 7e5d2f7 commit 71c9a36

File tree

5 files changed

+69
-41
lines changed

5 files changed

+69
-41
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"source": "https://github.com/easmith/selectel-storage-php-class"
1515
},
1616
"require": {
17-
"php": ">=5.3.3"
17+
"php": ">=5.4"
1818
},
19-
"version": "1.0.2",
19+
"version": "1.0.3",
2020
"autoload": {
2121
"psr-4": {
2222
"easmith\\selectel\\storage\\": "src"

example.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77

88
echo "\n\nCreate Container:\n";
99
$container = $selectelStorage->createContainer('selectel', array("X-Container-Meta-Type: public"));
10-
print_r($container->getInfo());
10+
// print_r($container->getInfo());
1111

1212
echo "Containers list\n";
1313
$containerList = $selectelStorage->listContainers();
14-
print_r($containerList);
14+
// print_r($containerList);
1515

1616
echo "\n\nContainer Info:\n";
1717
$cInfo = $selectelStorage->getContainer($containerList[0])->getInfo();
18-
print_r($cInfo);
18+
// print_r($cInfo);
1919

2020
echo "\n\nCreate directory:\n";
2121
$container = $selectelStorage->getContainer($containerList[0]);
2222
$container->createDirectory('php/test');
2323

2424
echo "\n\nDirectories:\n";
2525
$dirList = $container->listFiles($limit = 10000, $marker = null, $prefix = null, $path = "");
26-
print_r($dirList);
26+
// print_r($dirList);
2727

2828
echo "\n\nPutting File:\n";
2929
$res = $container->putFile(__FILE__, 'example.php');
@@ -38,8 +38,7 @@
3838
print_r($fileInfo);
3939

4040
echo "\n\nGetting file (base64):\n";
41-
$file = $container->getFile($fileList[0]);
42-
$file['content'] = base64_encode($file['content']);
41+
$file = $container->getFile($fileInfo['name']);
4342
print_r($file);
4443

4544
echo "\n\nCopy: \n";
@@ -55,6 +54,7 @@
5554
echo "\n\nAccountMetaTempURL: \n";
5655
$MetaTempURLKeyRes = $container->setAccountMetaTempURLKey("test");
5756
print_r($MetaTempURLKeyRes);
57+
echo "\n\n";
5858

5959
} catch (Exception $e) {
6060
print_r($e->getTrace());

src/SCurl.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@
1414
class SCurl
1515
{
1616

17-
static private $instance = null;
17+
private static $instance = null;
1818

1919
/**
2020
* Curl resource
2121
*
2222
* @var null|resource
2323
*/
24-
private $ch = null;
24+
private $ch;
2525

2626
/**
2727
* Current URL
2828
*
2929
* @var string
3030
*/
31-
private $url = null;
31+
private $url;
3232

3333
/**
3434
* Last request result
@@ -75,7 +75,7 @@ private function curlInit()
7575
*
7676
* @return SCurl
7777
*/
78-
static function init($url)
78+
public static function init($url)
7979
{
8080
if (self::$instance == null) {
8181
self::$instance = new SCurl($url);
@@ -88,18 +88,24 @@ static function init($url)
8888
*
8989
* @param string $url URL
9090
*
91-
* @return SCurl
91+
* @return SCurl|null
9292
*/
9393
public function setUrl($url)
9494
{
9595
$this->url = $url;
9696
return self::$instance;
9797
}
9898

99+
/**
100+
* @param $file
101+
* @return mixed
102+
* @throws SelectelStorageException
103+
*/
99104
public function putFile($file)
100105
{
101-
if (!file_exists($file))
106+
if (!file_exists($file)) {
102107
throw new SelectelStorageException("File '{$file}' does not exist");
108+
}
103109
$fp = fopen($file, "r");
104110
curl_setopt($this->ch, CURLOPT_INFILE, $fp);
105111
curl_setopt($this->ch, CURLOPT_INFILESIZE, filesize($file));
@@ -182,18 +188,22 @@ private function parseHead($head)
182188
{
183189
$result = array();
184190
$code = explode("\r\n", $head);
185-
preg_match('/HTTP.+ (.+)/', $code[0], $codeMatches);
186-
$result['HTTP-Code'] = (int)$codeMatches[1];
191+
preg_match('/HTTP\/(.+) (\d+)/', $code[0], $codeMatches);
192+
193+
$result['HTTP-Version'] = $codeMatches[1];
194+
$result['HTTP-Code'] = (int)$codeMatches[2];
187195
preg_match_all("/([A-z\-]+)\: (.*)\r\n/", $head, $matches, PREG_SET_ORDER);
188-
foreach ($matches as $match)
196+
197+
foreach ($matches as $match) {
189198
$result[strtolower($match[1])] = $match[2];
199+
}
190200

191201
return $result;
192202
}
193203

194204
public function putFileContents($contents)
195205
{
196-
$fp = fopen("php://temp", "r+");
206+
$fp = fopen("php://temp", "rb+");
197207
fputs($fp, $contents);
198208
rewind($fp);
199209
curl_setopt($this->ch, CURLOPT_INFILE, $fp);
@@ -273,8 +283,9 @@ public function getContent()
273283
*/
274284
public function getInfo($info = null)
275285
{
276-
if (!is_null($info))
286+
if (!is_null($info)) {
277287
$this->result['info'][$info];
288+
}
278289
return $this->result['info'];
279290
}
280291

src/SelectelContainer.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,30 @@ public function __construct($url, $token = array(), $format = null, $info = arra
2424
$this->url = $url . "/";
2525
$this->token = $token;
2626
$this->format = (!in_array($format, $this->formats, true) ? $this->format : $format);
27-
$this->info = (count($info) == 0 ? $this->getInfo(true) : $info);
27+
$this->info = (count($info) === 0 ? $this->getInfo(true) : $info);
2828
}
2929

3030
/**
3131
* Getting container info
3232
*
33-
* @param boolean $refresh Refres? Default false
33+
* @param boolean $refresh Refresh? Default false
3434
*
3535
* @return array
3636
*/
3737
public function getInfo($refresh = false)
3838
{
39-
if (!$refresh)
39+
if (!$refresh) {
4040
return $this->info;
41+
}
4142

4243
$headers = SCurl::init($this->url)
4344
->setHeaders($this->token)
4445
->request("HEAD")
4546
->getHeaders();
4647

47-
if (!in_array($headers["HTTP-Code"], array(204)))
48+
if (!in_array($headers["HTTP-Code"], array(204))) {
4849
return $this->error($headers["HTTP-Code"], __METHOD__);
50+
}
4951

5052
return $this->info = $this->getX($headers);
5153
}
@@ -95,7 +97,7 @@ public function getFileInfo($name)
9597
* @param string $marker Marker
9698
* @param string $prefix Prefix
9799
* @param string $path Path
98-
* @param string $delimiter Delemiter
100+
* @param string $delimiter Delimiter
99101
* @param string $format Format
100102
*
101103
* @return array|string
@@ -117,8 +119,9 @@ public function listFiles($limit = 10000, $marker = null, $prefix = null, $path
117119
->request("GET")
118120
->getContent();
119121

120-
if ($params['format'] == '')
122+
if ($params['format'] == '') {
121123
return explode("\n", trim($res));
124+
}
122125

123126
return trim($res);
124127
}
@@ -133,16 +136,18 @@ public function listFiles($limit = 10000, $marker = null, $prefix = null, $path
133136
*/
134137
public function putFile($localFileName, $remoteFileName = null, $headers = array())
135138
{
136-
if (is_null($remoteFileName))
139+
if (is_null($remoteFileName)) {
137140
$remoteFileName = array_pop(explode(DIRECTORY_SEPARATOR, $localFileName));
141+
}
138142
$headers = array_merge($headers, $this->token);
139143
$info = SCurl::init($this->url . $remoteFileName)
140144
->setHeaders($headers)
141145
->putFile($localFileName)
142146
->getInfo();
143147

144-
if (!in_array($info["http_code"], array(201)))
148+
if (!in_array($info["http_code"], array(201))) {
145149
return $this->error($info["http_code"], __METHOD__);
150+
}
146151

147152
return $info;
148153
}
@@ -161,8 +166,9 @@ public function putFileContents($contents, $remoteFileName = null)
161166
->putFileContents($contents)
162167
->getInfo();
163168

164-
if (!in_array($info["http_code"], array(201)))
169+
if (!in_array($info["http_code"], array(201))) {
165170
return $this->error($info["http_code"], __METHOD__);
171+
}
166172

167173
return $info;
168174
}
@@ -178,8 +184,9 @@ public function putFileContents($contents, $remoteFileName = null)
178184
public function setFileHeaders($name, $headers)
179185
{
180186
$headers = $this->getX($headers, "X-Container-Meta-");
181-
if (get_class($this) != 'SelectelContainer')
187+
if (get_class($this) != 'SelectelContainer') {
182188
return 0;
189+
}
183190

184191
return $this->setMetaInfo($name, $headers);
185192
}

src/SelectelStorage.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,11 @@ public function getInfo()
112112
protected static function getX($headers, $prefix = 'x-')
113113
{
114114
$result = array();
115-
foreach ($headers as $key => $value)
116-
if (stripos($key, $prefix) === 0)
115+
foreach ($headers as $key => $value) {
116+
if (stripos($key, $prefix) === 0) {
117117
$result[$key] = $value;
118+
}
119+
}
118120
return $result;
119121
}
120122

@@ -141,8 +143,9 @@ public function listContainers($limit = 10000, $marker = '', $format = null)
141143
->request("GET")
142144
->getContent();
143145

144-
if ($params['format'] == '')
146+
if ($params['format'] == '') {
145147
return explode("\n", trim($cont));
148+
}
146149

147150
return trim($cont);
148151
}
@@ -164,8 +167,9 @@ public function createContainer($name, $headers = array())
164167
->request("PUT")
165168
->getInfo();
166169

167-
if (!in_array($info["http_code"], array(201, 202)))
170+
if (!in_array($info["http_code"], array(201, 202))) {
168171
return $this->error($info["http_code"], __METHOD__);
172+
}
169173

170174
return $this->getContainer($name);
171175
}
@@ -185,8 +189,9 @@ public function getContainer($name)
185189
->request("HEAD")
186190
->getHeaders();
187191

188-
if (!in_array($headers["HTTP-Code"], array(204)))
192+
if (!in_array($headers["HTTP-Code"], array(204))) {
189193
return $this->error($headers["HTTP-Code"], __METHOD__);
194+
}
190195

191196
return new SelectelContainer($url, $this->token, $this->format, $this->getX($headers));
192197
}
@@ -205,8 +210,9 @@ public function delete($name)
205210
->request("DELETE")
206211
->getInfo();
207212

208-
if (!in_array($info["http_code"], array(204)))
213+
if (!in_array($info["http_code"], array(204))) {
209214
return $this->error($info["http_code"], __METHOD__);
215+
}
210216

211217
return $info;
212218
}
@@ -235,8 +241,9 @@ public function copy($origin, $destin)
235241
public function setContainerHeaders($name, $headers)
236242
{
237243
$headers = $this->getX($headers, "X-Container-Meta-");
238-
if (get_class($this) != 'SelectelStorage')
244+
if (get_class($this) != 'SelectelStorage') {
239245
return 0;
246+
}
240247

241248
return $this->setMetaInfo($name, $headers);
242249
}
@@ -251,20 +258,22 @@ public function setContainerHeaders($name, $headers)
251258
*/
252259
protected function setMetaInfo($name, $headers)
253260
{
254-
if (get_class($this) == 'SelectelStorage')
261+
if (get_class($this) == 'SelectelStorage') {
255262
$headers = $this->getX($headers, "X-Container-Meta-");
256-
elseif (get_class($this) == 'SelectelContainer')
263+
} elseif (get_class($this) == 'SelectelContainer') {
257264
$headers = $this->getX($headers, "X-Container-Meta-");
258-
else
265+
} else {
259266
return 0;
267+
}
260268

261269
$info = SCurl::init($this->url . $name)
262270
->setHeaders($headers)
263271
->request("POST")
264272
->getInfo();
265273

266-
if (!in_array($info["http_code"], array(204)))
274+
if (!in_array($info["http_code"], array(204))) {
267275
return $this->error($info["http_code"], __METHOD__);
276+
}
268277

269278
return $info["http_code"];
270279
}
@@ -322,8 +331,9 @@ public function setAccountMetaTempURLKey($key)
322331
->request("POST")
323332
->getHeaders();
324333

325-
if (!in_array($res["HTTP-Code"], array(202)))
334+
if (!in_array($res["HTTP-Code"], array(202))) {
326335
return $this->error($res ["HTTP-Code"], __METHOD__);
336+
}
327337

328338
return $res["HTTP-Code"];
329339
}

0 commit comments

Comments
 (0)