Skip to content

Commit

Permalink
psr2 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
easmith committed May 8, 2017
1 parent 20865d4 commit 8575ab2
Show file tree
Hide file tree
Showing 5 changed files with 296 additions and 255 deletions.
80 changes: 41 additions & 39 deletions example.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,62 @@
<?php

require_once ("vendor/autoload.php");
require_once("vendor/autoload.php");

try {
$selectelStorage = new SelectelStorage("User", "Pass");
$selectelStorage = new easmith\selectel\storage\SelectelStorage("user", "pass");

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

echo "Containers list\n";
$containerList = $selectelStorage->listContainers();
print_r($containerList);
echo "\n\nCreate Container:\n";
$container = $selectelStorage->createContainer('selectel', array("X-Container-Meta-Type: public"));
print_r($container->getInfo());

echo "\n\nContainer Info:\n";
$cInfo = $selectelStorage->getContainer($containerList[0])->getInfo();
print_r($cInfo);
echo "Containers list\n";
$containerList = $selectelStorage->listContainers();
print_r($containerList);

echo "\n\nCreate directory:\n";
echo "\n\nContainer Info:\n";
$cInfo = $selectelStorage->getContainer($containerList[0])->getInfo();
print_r($cInfo);

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

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

echo "\n\nPutting File:\n";
$res = $container->putFile(__FILE__, 'example.php');
print_r($res);
echo "\n\nPutting File:\n";
$res = $container->putFile(__FILE__, 'example.php');
print_r($res);

echo "\n\nFiles in directory:\n";
$fileList = $container->listFiles($limit = 10000, $marker = null, $prefix = null, $path = 'php/');
print_r($fileList);
echo "\n\nFiles in directory:\n";
$fileList = $container->listFiles($limit = 10000, $marker = null, $prefix = null, $path = 'php/');
print_r($fileList);

echo "\n\nFile info:\n";
$fileInfo = $container->getFileInfo('example.php');
print_r($fileInfo);
echo "\n\nFile info:\n";
$fileInfo = $container->getFileInfo('example.php');
print_r($fileInfo);

echo "\n\nGetting file (base64):\n";
$file = $container->getFile($fileList[0]);
$file['content'] = base64_encode($file['content']);
print_r($file);
echo "\n\nGetting file (base64):\n";
$file = $container->getFile($fileList[0]);
$file['content'] = base64_encode($file['content']);
print_r($file);

echo "\n\nCopy: \n";
$copyRes = $container->copy('example.php', 'php/test/Examples_copy.php5');
print_r($copyRes);
echo "\n\nCopy: \n";
$copyRes = $container->copy('example.php', 'php/test/Examples_copy.php5');
print_r($copyRes);

echo "\n\nDelete: \n";
$deleteRes = $container->delete('example.php');
print_r($deleteRes);
$deleteRes = $container->delete('php');
$deleteRes = $container->delete('php/test/Examples_copy.php5');
print_r($deleteRes);

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

echo "\n\nAccountMetaTempURL: \n";
$MetaTempURLKeyRes = $container->setAccountMetaTempURLKey("test");
print_r($MetaTempURLKeyRes);

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

153 changes: 85 additions & 68 deletions src/SCurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* @package class_package
* @author Eugene Kuznetcov <[email protected]>
*/
class SCurl {
class SCurl
{

static private $instance = null;

Expand Down Expand Up @@ -43,30 +44,19 @@ class SCurl {
*/
private $params = array();

/**
*
* @param string $url
*
* @return SCurl
*/
static function init($url) {
if (self::$instance == null) {
self::$instance = new SCurl($url);
}
return self::$instance->setUrl($url);
}

/**
* Curl wrapper
*
* @param string $url
*/
private function __construct($url) {
private function __construct($url)
{
$this->setUrl($url);
$this->curlInit();
}

private function curlInit() {
private function curlInit()
{
$this->ch = curl_init($this->url);
curl_setopt($this->ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, false);
Expand All @@ -79,20 +69,43 @@ private function curlInit() {
// curl_setopt($this->ch, CURLOPT_RANGE, "0-100");
}

/**
*
* @param string $url
*
* @return SCurl
*/
static function init($url)
{
if (self::$instance == null) {
self::$instance = new SCurl($url);
}
return self::$instance->setUrl($url);
}

/**
* Set url for request
*
* @param string $url URL
*
* @return SCurl
*/
public function setUrl($url) {
public function setUrl($url)
{
$this->url = $url;
return self::$instance;
}

private function __clone() {

public function putFile($file)
{
if (!file_exists($file))
throw new SelectelStorageException("File '{$file}' does not exist");
$fp = fopen($file, "r");
curl_setopt($this->ch, CURLOPT_INFILE, $fp);
curl_setopt($this->ch, CURLOPT_INFILESIZE, filesize($file));
$this->request('PUT');
fclose($fp);
return self::$instance;
}

/**
Expand All @@ -102,7 +115,8 @@ private function __clone() {
*
* @return SCurl
*/
public function request($method) {
public function request($method)
{
$this->method($method);
$this->params = array();
curl_setopt($this->ch, CURLOPT_URL, $this->url);
Expand All @@ -127,65 +141,45 @@ public function request($method) {
*
* @return SCurl
*/
private function method($method) {
private function method($method)
{
switch ($method) {
case "GET" : {
$this->url .= "?" . http_build_query($this->params);
curl_setopt($this->ch, CURLOPT_HTTPGET, true);
break;
}
$this->url .= "?" . http_build_query($this->params);
curl_setopt($this->ch, CURLOPT_HTTPGET, true);
break;
}
case "HEAD" : {
$this->url .= "?" . http_build_query($this->params);
curl_setopt($this->ch, CURLOPT_NOBODY, true);
break;
}
$this->url .= "?" . http_build_query($this->params);
curl_setopt($this->ch, CURLOPT_NOBODY, true);
break;
}
case "POST" : {
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($this->params));
break;
}
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($this->params));
break;
}
case "PUT" : {
curl_setopt($this->ch, CURLOPT_PUT, true);
break;
}
curl_setopt($this->ch, CURLOPT_PUT, true);
break;
}
default : {
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
break;
}
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
break;
}
}
return self::$instance;
}

public function putFile($file) {
if (!file_exists($file))
throw new SelectelStorageException("File '{$file}' does not exist");
$fp = fopen($file, "r");
curl_setopt($this->ch, CURLOPT_INFILE, $fp);
curl_setopt($this->ch, CURLOPT_INFILESIZE, filesize($file));
$this->request('PUT');
fclose($fp);
return self::$instance;
}

public function putFileContents($contents) {
$fp = fopen("php://temp", "r+");
fputs($fp, $contents);
rewind($fp);
curl_setopt($this->ch, CURLOPT_INFILE, $fp);
curl_setopt($this->ch, CURLOPT_INFILESIZE, strlen($contents));
$this->request('PUT');
fclose($fp);
return self::$instance;
}

/**
* Header Parser
*
* @param array $head
*
* @return array
*/
private function parseHead($head) {
private function parseHead($head)
{
$result = array();
$code = explode("\r\n", $head);
$result['HTTP-Code'] = intval(str_replace("HTTP/1.1", "", $code[0]));
Expand All @@ -196,14 +190,27 @@ private function parseHead($head) {
return $result;
}

public function putFileContents($contents)
{
$fp = fopen("php://temp", "r+");
fputs($fp, $contents);
rewind($fp);
curl_setopt($this->ch, CURLOPT_INFILE, $fp);
curl_setopt($this->ch, CURLOPT_INFILESIZE, strlen($contents));
$this->request('PUT');
fclose($fp);
return self::$instance;
}

/**
* Set headers
*
* @param array $headers
*
* @return SCurl
*/
public function setHeaders($headers) {
public function setHeaders($headers)
{
$headers = array_merge(array("Expect:"), $headers);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
return self::$instance;
Expand All @@ -216,7 +223,8 @@ public function setHeaders($headers) {
*
* @return SCurl
*/
public function setParams($params) {
public function setParams($params)
{
$this->params = $params;
return self::$instance;
}
Expand All @@ -226,7 +234,8 @@ public function setParams($params) {
*
* @return array
*/
public function getResult() {
public function getResult()
{
return $this->result;
}

Expand All @@ -237,7 +246,8 @@ public function getResult() {
*
* @return array
*/
public function getHeaders($header = null) {
public function getHeaders($header = null)
{
if (!is_null($header))
$this->result['header'][$header];
return $this->result['header'];
Expand All @@ -248,7 +258,8 @@ public function getHeaders($header = null) {
*
* @return array
*/
public function getContent() {
public function getContent()
{
return $this->result['content'];
}

Expand All @@ -259,10 +270,16 @@ public function getContent() {
*
* @return array
*/
public function getInfo($info = null) {
public function getInfo($info = null)
{
if (!is_null($info))
$this->result['info'][$info];
return $this->result['info'];
}

private function __clone()
{

}

}
Loading

0 comments on commit 8575ab2

Please sign in to comment.