Permalink
Browse files

Superseded #302 Remove S3 PEAR dependency (#748)

  • Loading branch information...
siad007 authored and mrook committed Aug 24, 2017
1 parent 48be70f commit c2ecc59bf0f37db02d178e9a035cf4936e4c530b
@@ -47,8 +47,8 @@
protected $_options = [];
/**
* @param $var
* @param $val
* @param string $var
* @param mixed $val
*/
public function __set($var, $val)
{
@@ -80,7 +80,7 @@ public function __get($var)
}
/**
* @param $var
* @param string $var
* @return bool
*/
public function __isset($var)
@@ -89,8 +89,8 @@ public function __isset($var)
}
/**
* @param $key
* @throws BuildException
* @param string $key
* @throws BuildException if $key is an empty string
*/
public function setKey($key)
{
@@ -101,6 +101,11 @@ public function setKey($key)
$this->key = $key;
}
/**
* @return string
*
* @throws BuildException if key is not set
*/
public function getKey()
{
if (!($key = $this->key)) {
@@ -111,8 +116,8 @@ public function getKey()
}
/**
* @param $secret
* @throws BuildException
* @param string $secret
* @throws BuildException if $secret is a empty string
*/
public function setSecret($secret)
{
@@ -123,6 +128,11 @@ public function setSecret($secret)
$this->secret = $secret;
}
/**
* @return string
*
* @throws BuildException if secret is not set
*/
public function getSecret()
{
if (!($secret = $this->secret)) {
@@ -1,8 +1,5 @@
<?php
/*
* $Id$
*
/**
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -20,8 +17,6 @@
* <http://phing.info>.
*/
require_once dirname(__DIR__) . "/Amazon.php";
/**
* Abstract Service_Amazon_S3 class.
*
@@ -38,30 +33,40 @@
*
* (default value: null)
*
* @var Services_Amazon_S3
* @see Services_Amazon_S3
* @var Aws\S3\S3Client
*/
protected $_client = null;
/**
* We only instantiate the client once per task call
*
* @return Services_Amazon_S3
* @return Aws\S3\S3Client
*
* @throws \BuildException
*/
public function getClient()
{
require_once "Services/Amazon/S3.php";
if ($this->_client === null) {
$this->_client = Services_Amazon_S3::getAccount($this->getKey(), $this->getSecret());
try {
$s3Client = new Aws\S3\S3Client(
[
'key' => $this->getKey(),
'secret' => $this->getSecret(),
]
);
} catch (InvalidArgumentException $e) {
throw new BuildException($e);
}
$this->_client = $s3Client;
}
return $this->_client;
}
/**
* @param $bucket
* @throws BuildException
* @param string $bucket
* @throws BuildException if $bucket is a empty string
*/
public function setBucket($bucket)
{
@@ -73,7 +78,9 @@ public function setBucket($bucket)
}
/**
* @throws BuildException
* @return string
*
* @throws BuildException if bucket is not set
*/
public function getBucket()
{
@@ -87,12 +94,15 @@ public function getBucket()
/**
* Returns an instance of Services_Amazon_S3_Resource_Object
*
* @param mixed $object
* @return Services_Amazon_S3_Resource_Object
* @param mixed $object
*
* @return Aws\Result
*
* @throws \BuildException
*/
public function getObjectInstance($object)
{
return $this->getBucketInstance()->getObject($object);
return $this->getClientInstance()->getObject($object);
}
/**
@@ -109,57 +119,36 @@ public function isObjectAvailable($object)
/**
* Returns an instance of Services_Amazon_S3_Resource_Bucket
*
* @return Services_Amazon_S3_Resource_Bucket
* @return \Aws\S3\S3Client
*/
public function getBucketInstance()
public function getClientInstance()
{
return $this->getClient()->getBucket($this->getBucket());
return $this->getClient();
}
/**
* Check if the current bucket is available
*
* @return bool
*/
public function isBucketAvailable()
{
return (bool) $this->getBucketInstance($this->getBucket())->load();
}
/**
* Get the contents of an object (by it's name)
*
* @param string $object
* @throws BuildException
* @return mixed
* @throws \BuildException
*/
public function getObjectContents($object)
public function isBucketAvailable()
{
if (!$this->isBucketAvailable($this->getBucket())) {
throw new BuildException('Bucket doesn\'t exist or wrong permissions');
}
$bucket = $this->getClient()->getBucket($this->getBucket());
if (!$this->isObjectAvailable($object)) {
throw new BuildException('Object not available: ' . $object);
}
$object = $this->getObjectInstance($object);
$object->load();
return $object->data;
return $this->getClientInstance()->doesBucketExist($this->getBucket());
}
/**
* Create a bucket
*
* @return bool
*
* @throws \BuildException
*/
public function createBucket()
{
$bucket = $this->getBucketInstance();
$bucket->name = $this->getBucket();
$bucket->save();
$client = $this->getClientInstance();
$client->createBucket(['Bucket' => $this->getBucket()]);
return $this->isBucketAvailable();
}
@@ -117,6 +117,13 @@ public function execute()
$target = rtrim($target, '/') . '/' . $this->getObject();
}
file_put_contents($target, $this->getObjectContents($this->getObject()));
$client = $this->getClientInstance();
$client->getObject(
[
'Bucket' => $this->getBucket(),
'Key' => $this->getObject(),
'SaveAs' => $target
]
);
}
}
Oops, something went wrong.

0 comments on commit c2ecc59

Please sign in to comment.