You are here

public static function StreamWrapperConfiguration::fromDrupalVariables in AmazonS3 7.2

Set the stream wrapper configuration using Drupal variables.

Return value

StreamWrapperConfiguration A StreamWrapperConfiguration object.

Throws

\InvalidArgumentException Thrown when the StreamWrapper configuration is invalid, such as when a bucket is not defined.

6 calls to StreamWrapperConfiguration::fromDrupalVariables()
amazons3_file_entity_upload_destination_uri_alter in ./amazons3.module
Implements hook_file_entity_upload_destination_uri_alter().
amazons3_requirements in ./amazons3.install
Implements hook_requirements().
amazons3_stream_wrappers in ./amazons3.module
Implements hook_stream_wrappers().
amazons3_upload_location in ./amazons3.module
Return a URI for use in an #upload_location or similar form element.
amazons3_uri_add_bucket in ./amazons3.module
Add the default bucket and return a string URL.

... See full list

File

src/StreamWrapperConfiguration.php, line 387

Class

StreamWrapperConfiguration
Class to manage S3 stream wrapper configuration.

Namespace

Drupal\amazons3

Code

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']));

  // CNAME support for customizing S3 URLs.
  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()));
  }

  // Check whether local file caching is turned on.
  if (static::variable_get('amazons3_cache', $defaults['caching'])) {
    $config
      ->enableCaching();
    $config
      ->setCacheLifetime(static::variable_get('amazons3_cache_expiration', NULL));
  }
  else {
    $config
      ->disableCaching();
  }

  // Torrent list.
  $torrentPaths = static::variable_get('amazons3_torrents', array());
  $paths = BasicPath::factory($torrentPaths);
  if (!empty($paths)) {
    $config
      ->setTorrentPaths(new MatchablePaths($paths));
  }

  // Presigned url-list.
  $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));
  }

  // Force "save as" list.
  $saveAsPaths = static::variable_get('amazons3_saveas', array());
  $paths = BasicPath::factory($saveAsPaths);
  if (!empty($paths)) {
    $config
      ->setSaveAsPaths(new MatchablePaths($paths));
  }

  // Reduced Redundancy Storage.
  $rrsPaths = static::variable_get('amazons3_rrs', array());
  $paths = BasicPath::factory($rrsPaths);
  if (!empty($paths)) {
    $config
      ->setReducedRedundancyPaths(new MatchablePaths($paths));
  }
  return $config;
}