View source
<?php
namespace Drupal\amazons3;
use Drupal\amazons3\DrupalAdapter\Bootstrap;
use Drupal\amazons3\Matchable\BasicPath;
use Drupal\amazons3\Matchable\MatchablePaths;
use Drupal\amazons3\Matchable\PresignedPath;
use Guzzle\Common\Collection;
class StreamWrapperConfiguration extends Collection {
use Bootstrap;
public static function fromConfig(array $config = array(), array $defaults = array(), array $required = array()) {
if (empty($defaults)) {
$defaults = self::defaults();
}
if (empty($required)) {
$required = self::required();
}
$data = $config + $defaults;
if ($data['caching']) {
$required[] = 'expiration';
}
if ($missing = array_diff($required, array_keys(array_filter($data, function ($item) {
return !is_null($item) && $item !== '';
})))) {
throw new \InvalidArgumentException('Config is missing the following keys: ' . implode(', ', $missing));
}
if (!$data['domain']) {
$data['domain'] = self::getS3Domain($data['bucket']);
}
return new static($data);
}
protected static function defaults() {
$defaults = array(
'hostname' => NULL,
'bucket' => NULL,
'region' => NULL,
'torrentPaths' => new MatchablePaths(),
'presignedPaths' => new MatchablePaths(),
'saveAsPaths' => new MatchablePaths(),
'cloudFront' => FALSE,
'cloudFrontPrivateKey' => NULL,
'cloudFrontKeyPairId' => NULL,
'domain' => NULL,
'domainScheme' => 'https',
'caching' => FALSE,
'cacheLifetime' => NULL,
'reducedRedundancyPaths' => new MatchablePaths(),
);
return $defaults;
}
protected static function required() {
$required = array(
'bucket',
'region',
);
return $required;
}
protected static function getS3Domain($bucket) {
$domain = StreamWrapper::S3_API_DOMAIN;
if (strpos($bucket, '.') === FALSE) {
$domain = $bucket . '.' . $domain;
}
return $domain;
}
public function getHostname() {
return $this->data['hostname'];
}
public function setHostname($hostname) {
$this->data['hostname'] = $hostname;
}
public function getBucket() {
return $this->data['bucket'];
}
public function setBucket($bucket) {
$this->data['bucket'] = $bucket;
}
public function getTorrentPaths() {
return $this->data['torrentPaths'];
}
public function setTorrentPaths(MatchablePaths $torrentPaths) {
$this->data['torrentPaths'] = $torrentPaths;
}
public function getPresignedPaths() {
return $this->data['presignedPaths'];
}
public function setPresignedPaths(MatchablePaths $presignedPaths) {
$this->data['presignedPaths'] = $presignedPaths;
}
public function getRegion() {
return $this->data['region'];
}
public function setRegion($region) {
$this->data['region'] = $region;
}
public function getSaveAsPaths() {
return $this->data['saveAsPaths'];
}
public function setSaveAsPaths(MatchablePaths $saveAsPaths) {
$this->data['saveAsPaths'] = $saveAsPaths;
}
public function isCloudFront() {
return $this->data['cloudFront'];
}
public function serveWithCloudFront() {
$this->data['cloudFront'] = TRUE;
}
public function setCloudFrontCredentials($path, $keyPairId) {
if (!file_exists($path)) {
throw new \InvalidArgumentException("{$path} does not exist.");
}
$this->data['cloudFrontPrivateKey'] = $path;
$this->data['cloudFrontKeyPairId'] = $keyPairId;
}
public function getCloudFront() {
if (!$this
->isCloudFront()) {
throw new \LogicException('CloudFront is not enabled.');
}
return CloudFrontClient::factory(array(
'private_key' => $this->data['cloudFrontPrivateKey'],
'key_pair_id' => $this->data['cloudFrontKeyPairId'],
));
}
public function serveWithS3() {
$this->data['cloudFront'] = FALSE;
}
public function getDomain() {
return $this->data['domain'];
}
public function setDomain($domain) {
$this->data['domain'] = $domain;
}
public function getDomainScheme() {
return $this->data['domainScheme'];
}
public function setDomainScheme($scheme) {
$this->data['domainScheme'] = $scheme;
}
public function isCaching() {
return (bool) $this->data['caching'];
}
public function enableCaching() {
$this->data['caching'] = TRUE;
}
public function disableCaching() {
$this->data['caching'] = FALSE;
$this->data['expiration'] = NULL;
}
public function setCacheLifetime($expiration) {
if (!$this
->isCaching()) {
throw new \LogicException('Caching must be enabled before setting a expiration.');
}
$this->data['expiration'] = $expiration;
}
public function getCacheLifetime() {
return $this->data['expiration'];
}
public function getReducedRedundancyPaths() {
return $this->data['reducedRedundancyPaths'];
}
public function setReducedRedundancyPaths(MatchablePaths $reducedRedundancyPaths) {
$this->data['reducedRedundancyPaths'] = $reducedRedundancyPaths;
}
public static function fromDrupalVariables() {
$config = self::fromConfig(array(
'bucket' => static::variable_get('amazons3_bucket', NULL),
'region' => static::variable_get('amazons3_region', NULL),
));
$defaults = $config
->defaults();
$config
->setHostname(static::variable_get('amazons3_hostname', $defaults['hostname']));
if (static::variable_get('amazons3_cname', FALSE)) {
$domain = static::variable_get('amazons3_domain', $defaults['domain']);
if (strlen($domain) > 0) {
$config
->setDomain($domain);
}
else {
$config
->setDomain($config
->getBucket());
}
if (static::variable_get('amazons3_cloudfront', $defaults['cloudFront'])) {
$path = static::variable_get('amazons3_cloudfront_private_key', $defaults['cloudFrontPrivateKey']);
$keyPairId = static::variable_get('amazons3_cloudfront_keypair_id', $defaults['cloudFrontKeyPairId']);
$config
->setCloudFrontCredentials($path, $keyPairId);
$config
->serveWithCloudFront();
}
$scheme = static::variable_get('amazons3_domain_scheme', $defaults['domainScheme']);
$config
->setDomainScheme($scheme);
}
else {
$config
->setDomain(static::getS3Domain($config
->getBucket()));
}
if (static::variable_get('amazons3_cache', $defaults['caching'])) {
$config
->enableCaching();
$config
->setCacheLifetime(static::variable_get('amazons3_cache_expiration', NULL));
}
else {
$config
->disableCaching();
}
$torrentPaths = static::variable_get('amazons3_torrents', array());
$paths = BasicPath::factory($torrentPaths);
if (!empty($paths)) {
$config
->setTorrentPaths(new MatchablePaths($paths));
}
$presigned_urls = static::variable_get('amazons3_presigned_urls', array());
$paths = array();
foreach ($presigned_urls as $presigned_url) {
$paths[] = new PresignedPath($presigned_url['pattern'], $presigned_url['timeout']);
}
if (!empty($paths)) {
$config
->setPresignedPaths(new MatchablePaths($paths));
}
$saveAsPaths = static::variable_get('amazons3_saveas', array());
$paths = BasicPath::factory($saveAsPaths);
if (!empty($paths)) {
$config
->setSaveAsPaths(new MatchablePaths($paths));
}
$rrsPaths = static::variable_get('amazons3_rrs', array());
$paths = BasicPath::factory($rrsPaths);
if (!empty($paths)) {
$config
->setReducedRedundancyPaths(new MatchablePaths($paths));
}
return $config;
}
}